Skip to content

Commit

Permalink
fix(linter): disabled eslint rules dont count as type checked rules (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Cannon authored and sumitparakh committed Sep 1, 2021
1 parent 4d8740e commit 3638056
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/linter/migrations.json
Expand Up @@ -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": {
Expand Down
Expand Up @@ -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', () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [
Expand All @@ -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
Expand All @@ -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 () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/linter/src/utils/rules-requiring-type-checking.ts
Expand Up @@ -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)];
}
}
}
Expand Down

0 comments on commit 3638056

Please sign in to comment.