From c0d6c40ec3c286e6ead050ef6f257d4b5076809e Mon Sep 17 00:00:00 2001 From: Gijs Laarman Date: Wed, 16 Oct 2024 21:35:43 +0200 Subject: [PATCH] fix: esmodules for eslint --- eslint.config.mjs | 13 ++----- eslint/base.js | 39 ------------------- eslint/base.mjs | 23 +++++++++++ ...act-typescript.js => react-typescript.mjs} | 0 eslint/react.js | 29 -------------- eslint/react.mjs | 4 ++ eslint/typescript.js | 28 ------------- eslint/typescript.mjs | 5 +++ package.json | 9 ++++- pnpm-lock.yaml | 3 ++ prettier/{index.js => index.cjs} | 0 11 files changed, 45 insertions(+), 108 deletions(-) delete mode 100644 eslint/base.js create mode 100644 eslint/base.mjs rename eslint/{react-typescript.js => react-typescript.mjs} (100%) delete mode 100644 eslint/react.js create mode 100644 eslint/react.mjs delete mode 100644 eslint/typescript.js create mode 100644 eslint/typescript.mjs rename prettier/{index.js => index.cjs} (100%) diff --git a/eslint.config.mjs b/eslint.config.mjs index accfad4..1add5a7 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,11 +1,4 @@ -import globals from 'globals' -import pluginJs from '@eslint/js' -import tseslint from 'typescript-eslint' +import baseConfig from './eslint/base.mjs' +import typescriptConfig from './eslint/typescript.mjs' -export default [ - { extends: ['./eslint/_base.js'] }, - - { languageOptions: { globals: { ...globals.browser, ...globals.node } } }, - pluginJs.configs.recommended, - ...tseslint.configs.recommended, -] +export default [...baseConfig, ...typescriptConfig] diff --git a/eslint/base.js b/eslint/base.js deleted file mode 100644 index c1c7f0d..0000000 --- a/eslint/base.js +++ /dev/null @@ -1,39 +0,0 @@ -// eslint.config.js -import eslintRecommended from 'eslint:recommended' -import eslintPrettier from 'eslint-config-prettier' -import importRecommended from 'eslint-plugin-import/config/recommended' - -export default [ - // Extend recommended rules - eslintRecommended, - importRecommended, - eslintPrettier, - { - rules: { - // Best Practices - eqeqeq: 'warn', // Enforce strict equality - curly: 'error', // Require curly braces for all control statements - 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], // Ignore unused vars starting with _ - - // Code Quality - 'no-console': 'warn', // Allow console with warning - 'no-debugger': 'error', // Disallow debugger - 'prefer-const': 'error', // Prefer const for non-reassigned vars - - // Import Rules - 'import/no-unresolved': 'error', // Disallow unresolved imports - 'import/order': [ - 'error', - { - groups: ['builtin', 'external', 'internal'], - 'newlines-between': 'always', - }, - ], - - // Styling (delegated to Prettier, but some optional manual additions) - 'no-multiple-empty-lines': ['warn', { max: 1 }], - 'newline-before-return': 'warn', - 'no-trailing-spaces': 'error', - }, - }, -] diff --git a/eslint/base.mjs b/eslint/base.mjs new file mode 100644 index 0000000..5779b9e --- /dev/null +++ b/eslint/base.mjs @@ -0,0 +1,23 @@ +// Base settings + +import js from '@eslint/js' +import eslintConfigPrettier from 'eslint-config-prettier' +import importPlugin from 'eslint-plugin-import' + +export default [ + // Extend recommended rules + { + plugins: { import: importPlugin }, + rules: { + ...importPlugin.configs['recommended'].rules, + }, + }, + { + files: ['**/*.js'], + rules: { + ...js.configs.recommended.rules, + 'no-unused-vars': 'warn', + }, + }, + eslintConfigPrettier, +] diff --git a/eslint/react-typescript.js b/eslint/react-typescript.mjs similarity index 100% rename from eslint/react-typescript.js rename to eslint/react-typescript.mjs diff --git a/eslint/react.js b/eslint/react.js deleted file mode 100644 index 32020e1..0000000 --- a/eslint/react.js +++ /dev/null @@ -1,29 +0,0 @@ -// react.js -import reactRecommended from 'eslint-plugin-react/configs/recommended' -import reactJsxRuntime from 'eslint-plugin-react/configs/jsx-runtime' // For JSX runtime - -export default [ - { - files: ['**/*.jsx', '**/*.tsx'], // Apply to both JSX and TSX files - languageOptions: { - globals: { - React: 'writable', // For environments where React is auto-imported - }, - }, - plugins: { - react: reactRecommended.plugins.react, // Enable React plugin - }, - rules: { - ...reactRecommended.rules, // Apply React recommended rules - ...reactJsxRuntime.rules, // Apply JSX runtime rules if using React 17+ - - // Additional React-Specific Rules - 'react/jsx-uses-react': 'off', // Disable since React 17+ does not require import - 'react/react-in-jsx-scope': 'off', // Disable because React 17+ JSX transforms don't require React in scope - 'react/prop-types': 'off', // Turn off prop-types as TypeScript handles types - 'react/jsx-no-target-blank': 'warn', // Warn about `target="_blank"` without `rel="noreferrer"` - 'react/jsx-no-duplicate-props': 'error', // Disallow duplicate props in JSX - 'react/jsx-key': 'error', // Ensure that arrays or iterators in JSX have `key` prop - }, - }, -] diff --git a/eslint/react.mjs b/eslint/react.mjs new file mode 100644 index 0000000..ab0282b --- /dev/null +++ b/eslint/react.mjs @@ -0,0 +1,4 @@ +// react.js +import pluginReact from 'eslint-plugin-react' + +export default [...pluginReact.configs.flat.all] diff --git a/eslint/typescript.js b/eslint/typescript.js deleted file mode 100644 index f5392ca..0000000 --- a/eslint/typescript.js +++ /dev/null @@ -1,28 +0,0 @@ -// typescript.js -import typescriptEslintRecommended from '@typescript-eslint/eslint-plugin/configs/recommended' -import typescriptParser from '@typescript-eslint/parser' - -export default [ - { - files: ['**/*.ts', '**/*.tsx'], // Apply only to TypeScript files - languageOptions: { - parser: typescriptParser, - parserOptions: { - project: './tsconfig.json', // Point to your tsconfig.json - sourceType: 'module', - }, - }, - rules: { - ...typescriptEslintRecommended.rules, // Extend TypeScript-specific rules - - // TypeScript-Specific Best Practices - '@typescript-eslint/consistent-type-imports': 'error', // Enforce consistent type imports - '@typescript-eslint/no-unused-vars': [ - 'error', - { argsIgnorePattern: '^_' }, - ], // Allow unused vars prefixed with `_` - '@typescript-eslint/explicit-module-boundary-types': 'off', // Turn off explicit return types for module boundaries - 'tsdoc/syntax': 'warn', // https://tsdoc.org/ - }, - }, -] diff --git a/eslint/typescript.mjs b/eslint/typescript.mjs new file mode 100644 index 0000000..0f0c120 --- /dev/null +++ b/eslint/typescript.mjs @@ -0,0 +1,5 @@ +// Typescript + +import tseslint from 'typescript-eslint' + +export default [...tseslint.configs.recommended] diff --git a/package.json b/package.json index cd881ec..9d81acd 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,12 @@ "license": "ISC", "author": "Gijs Laarman", "exports": { - "./eslint/*": "./eslint/*.js", - "./prettier": "./prettier/index.js", + "./eslint/*": { + "import": "./eslint/*.mjs" + }, + "./prettier": { + "require": "./prettier/index.cjs" + }, "./typescript/node20": "./typescript/node20.tsconfig.json", "./stylelint/*": "./stylelint/*.js" }, @@ -31,6 +35,7 @@ "dependencies": { "@typescript-eslint/eslint-plugin": "^8.9.0", "@typescript-eslint/parser": "^8.9.0", + "@typescript-eslint/typescript-estree": "^8.9.0", "eslint-config-prettier": "^9.1.0", "eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-typescript": "^3.6.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fecbc78..1dfedf5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@typescript-eslint/parser': specifier: ^8.9.0 version: 8.9.0(eslint@9.12.0)(typescript@5.6.3) + '@typescript-eslint/typescript-estree': + specifier: ^8.9.0 + version: 8.9.0(typescript@5.6.3) eslint-config-prettier: specifier: ^9.1.0 version: 9.1.0(eslint@9.12.0) diff --git a/prettier/index.js b/prettier/index.cjs similarity index 100% rename from prettier/index.js rename to prettier/index.cjs