diff --git a/packages/linter/migrations.json b/packages/linter/migrations.json index fb850af3f9084..52b482215ca18 100644 --- a/packages/linter/migrations.json +++ b/packages/linter/migrations.json @@ -52,6 +52,12 @@ "version": "12.9.0-beta.0", "description": "Add outputs for caching", "factory": "./src/migrations/update-12-9-0/add-outputs" + }, + "remove-eslint-project-config-if-no-type-checking-rules-again": { + "cli": "nx", + "version": "12.9.0-beta.0", + "description": "Remove ESLint parserOptions.project config if no rules requiring type-checking are in use", + "factory": "./src/migrations/update-12-4-0/remove-eslint-project-config-if-no-type-checking-rules" } }, "packageJsonUpdates": { diff --git a/packages/linter/src/migrations/update-12-4-0/remove-eslint-project-config-if-no-type-checking-rules.spec.ts b/packages/linter/src/migrations/update-12-4-0/remove-eslint-project-config-if-no-type-checking-rules.spec.ts index dc47c2ec618bf..3765365527fc0 100644 --- a/packages/linter/src/migrations/update-12-4-0/remove-eslint-project-config-if-no-type-checking-rules.spec.ts +++ b/packages/linter/src/migrations/update-12-4-0/remove-eslint-project-config-if-no-type-checking-rules.spec.ts @@ -6,7 +6,7 @@ import { } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import removeESLintProjectConfigIfNoTypeCheckingRules from './remove-eslint-project-config-if-no-type-checking-rules'; - +import type { Linter } from 'eslint'; const KNOWN_RULE_REQUIRING_TYPE_CHECKING = '@typescript-eslint/await-thenable'; describe('Remove ESLint parserOptions.project config if no rules requiring type-checking are in use', () => { @@ -31,6 +31,12 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type- projectType: 'library', targets: {}, }); + addProjectConfiguration(tree, 'another-lib', { + root: 'libs/another-lib', + sourceRoot: 'libs/another-lib/src', + projectType: 'library', + targets: {}, + }); }); it('should not update any configs if the root .eslintrc.json contains at least one rule requiring type-checking', async () => { @@ -123,7 +129,7 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type- }; writeJson(tree, '.eslintrc.json', rootEslintConfig); - const projectEslintConfig1 = { + const projectEslintConfig1: Linter.Config = { extends: '../../../.eslintrc.json', ignorePatterns: ['!**/*'], overrides: [ @@ -140,7 +146,7 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type- }; writeJson(tree, 'apps/react-app/.eslintrc.json', projectEslintConfig1); - const projectEslintConfig2 = { + const projectEslintConfig2: Linter.Config = { extends: '../../../.eslintrc.json', ignorePatterns: ['!**/*'], overrides: [ @@ -157,6 +163,23 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type- }; writeJson(tree, 'libs/workspace-lib/.eslintrc.json', projectEslintConfig2); + const projectEslintConfig3: Linter.Config = { + extends: '../../../.eslintrc.json', + ignorePatterns: ['!**/*'], + overrides: [ + { + files: ['*.ts', '*.tsx'], + parserOptions: { + project: 'some-path-to-tsconfig.json', + }, + rules: { + [KNOWN_RULE_REQUIRING_TYPE_CHECKING]: 'off', + }, + }, + ], + }; + writeJson(tree, 'libs/another-lib/.eslintrc.json', projectEslintConfig3); + await removeESLintProjectConfigIfNoTypeCheckingRules(tree); // No change - uses rule requiring type-checking @@ -183,6 +206,28 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type- ], } `); + + // Updated - no more parserOptions.project + expect(readJson(tree, 'libs/another-lib/.eslintrc.json')) + .toMatchInlineSnapshot(` + Object { + "extends": "../../../.eslintrc.json", + "ignorePatterns": Array [ + "!**/*", + ], + "overrides": Array [ + Object { + "files": Array [ + "*.ts", + "*.tsx", + ], + "rules": Object { + "@typescript-eslint/await-thenable": "off", + }, + }, + ], + } + `); }); it('should not error if .eslintrc.json does not exist', async () => { diff --git a/packages/linter/src/utils/rules-requiring-type-checking.ts b/packages/linter/src/utils/rules-requiring-type-checking.ts index 30ac9d28aba44..a5bb99e79b0d1 100644 --- a/packages/linter/src/utils/rules-requiring-type-checking.ts +++ b/packages/linter/src/utils/rules-requiring-type-checking.ts @@ -53,12 +53,20 @@ export function removeParserOptionsProjectIfNotRequired( return json; } +function determineEnabledRules(rules: Linter.RulesRecord): string[] { + return Object.entries(rules) + .filter(([key, value]) => { + return !(typeof value === 'string' && value === 'off'); + }) + .map(([ruleName]) => ruleName); +} + function getAllRulesInConfig(json: Linter.Config): string[] { - let allRules = json.rules ? Object.keys(json.rules) : []; + let allRules = json.rules ? determineEnabledRules(json.rules) : []; if (json.overrides?.length > 0) { for (const o of json.overrides) { if (o.rules) { - allRules = [...allRules, ...Object.keys(o.rules)]; + allRules = allRules = [...allRules, ...determineEnabledRules(o.rules)]; } } }