From 8050495a64c2853e469dbad7a7235b31174b30be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 24 Sep 2024 18:28:35 +0200 Subject: [PATCH] fix(linter): ignore dist and use compat helper for eslint-plugin-react-hooks --- .../eslint-plugin/src/flat-configs/react-jsx.ts | 3 ++- .../src/generators/init/global-eslint-config.ts | 5 +++++ .../generators/lint-project/lint-project.spec.ts | 3 +++ .../generators/utils/flat-config/ast-utils.ts | 16 +++++++++++----- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/flat-configs/react-jsx.ts b/packages/eslint-plugin/src/flat-configs/react-jsx.ts index 262f6826e9c09..f9e96d6c1dc12 100644 --- a/packages/eslint-plugin/src/flat-configs/react-jsx.ts +++ b/packages/eslint-plugin/src/flat-configs/react-jsx.ts @@ -1,3 +1,4 @@ +import { fixupPluginRules } from '@eslint/compat'; import jsxA11yPlugin from 'eslint-plugin-jsx-a11y'; import reactPlugin from 'eslint-plugin-react'; import reactHooksPlugin from 'eslint-plugin-react-hooks'; @@ -16,7 +17,7 @@ import tseslint from 'typescript-eslint'; export default tseslint.config( { plugins: { - 'react-hooks': reactHooksPlugin, + 'react-hooks': fixupPluginRules(reactHooksPlugin), }, rules: reactHooksPlugin.configs.recommended.rules, }, diff --git a/packages/eslint/src/generators/init/global-eslint-config.ts b/packages/eslint/src/generators/init/global-eslint-config.ts index 55577059ddcc1..1e2ad3efe04e7 100644 --- a/packages/eslint/src/generators/init/global-eslint-config.ts +++ b/packages/eslint/src/generators/init/global-eslint-config.ts @@ -112,6 +112,11 @@ export const getGlobalFlatEslintConfiguration = ( generateFlatPredefinedConfig('flat/javascript') ); + content = addBlockToFlatConfigExport( + content, + generateFlatOverride({ ignores: ['**/dist'] }) + ); + if (!rootProject) { content = addBlockToFlatConfigExport( content, diff --git a/packages/eslint/src/generators/lint-project/lint-project.spec.ts b/packages/eslint/src/generators/lint-project/lint-project.spec.ts index d68e952e227e0..46525377f1816 100644 --- a/packages/eslint/src/generators/lint-project/lint-project.spec.ts +++ b/packages/eslint/src/generators/lint-project/lint-project.spec.ts @@ -59,6 +59,9 @@ describe('@nx/eslint:lint-project', () => { ...nx.configs['flat/base'], ...nx.configs['flat/typescript'], ...nx.configs['flat/javascript'], + { + ignores: ['**/dist'], + }, { files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'], rules: { diff --git a/packages/eslint/src/generators/utils/flat-config/ast-utils.ts b/packages/eslint/src/generators/utils/flat-config/ast-utils.ts index 9fbe0fd441e56..601c869774a3d 100644 --- a/packages/eslint/src/generators/utils/flat-config/ast-utils.ts +++ b/packages/eslint/src/generators/utils/flat-config/ast-utils.ts @@ -891,7 +891,9 @@ export function overrideNeedsCompat( * based on a given legacy eslintrc JSON override object */ export function generateFlatOverride( - _override: Partial> + _override: Partial> & { + ignores?: Linter.FlatConfig['ignores']; + } ): ts.ObjectLiteralExpression | ts.SpreadElement { const override = mapFilePaths(_override); @@ -907,6 +909,10 @@ export function generateFlatOverride( files, }; + if (override.ignores) { + flatConfigOverride.ignores = override.ignores; + } + if (override.rules) { flatConfigOverride.rules = override.rules; } @@ -1074,10 +1080,10 @@ export function generateFlatPredefinedConfig( return spread ? ts.factory.createSpreadElement(node) : node; } -export function mapFilePaths( - _override: Partial> -) { - const override: Partial> = { +export function mapFilePaths< + T extends Partial> +>(_override: T) { + const override: T = { ..._override, }; if (override.files) {