fix(linter): check existence of eslintrc.json before running migration (#6335)

This commit is contained in:
Jason Jean 2021-07-15 10:40:54 -04:00 committed by GitHub
parent 6ef8afbfd9
commit 35d77a1b2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -5,7 +5,7 @@ import {
writeJson, writeJson,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import remoteESLintProjectConfigIfNoTypeCheckingRules from './remove-eslint-project-config-if-no-type-checking-rules'; import removeESLintProjectConfigIfNoTypeCheckingRules from './remove-eslint-project-config-if-no-type-checking-rules';
const KNOWN_RULE_REQUIRING_TYPE_CHECKING = '@typescript-eslint/await-thenable'; const KNOWN_RULE_REQUIRING_TYPE_CHECKING = '@typescript-eslint/await-thenable';
@ -100,7 +100,7 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type-
}; };
writeJson(tree, 'libs/workspace-lib/.eslintrc.json', projectEslintConfig2); writeJson(tree, 'libs/workspace-lib/.eslintrc.json', projectEslintConfig2);
await remoteESLintProjectConfigIfNoTypeCheckingRules(tree); await removeESLintProjectConfigIfNoTypeCheckingRules(tree);
// No change // No change
expect(readJson(tree, 'apps/react-app/.eslintrc.json')).toEqual( expect(readJson(tree, 'apps/react-app/.eslintrc.json')).toEqual(
@ -157,7 +157,7 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type-
}; };
writeJson(tree, 'libs/workspace-lib/.eslintrc.json', projectEslintConfig2); writeJson(tree, 'libs/workspace-lib/.eslintrc.json', projectEslintConfig2);
await remoteESLintProjectConfigIfNoTypeCheckingRules(tree); await removeESLintProjectConfigIfNoTypeCheckingRules(tree);
// No change - uses rule requiring type-checking // No change - uses rule requiring type-checking
expect(readJson(tree, 'apps/react-app/.eslintrc.json')).toEqual( expect(readJson(tree, 'apps/react-app/.eslintrc.json')).toEqual(
@ -184,4 +184,8 @@ describe('Remove ESLint parserOptions.project config if no rules requiring type-
} }
`); `);
}); });
it('should not error if .eslintrc.json does not exist', async () => {
await removeESLintProjectConfigIfNoTypeCheckingRules(tree);
});
}); });

View File

@ -24,6 +24,10 @@ function updateProjectESLintConfigs(host: Tree) {
export default async function removeESLintProjectConfigIfNoTypeCheckingRules( export default async function removeESLintProjectConfigIfNoTypeCheckingRules(
host: Tree host: Tree
) { ) {
if (!host.exists('.eslintrc.json')) {
return;
}
// If the root level config uses at least one rule requiring type-checking, do not migrate any project configs // If the root level config uses at least one rule requiring type-checking, do not migrate any project configs
const rootESLintConfig = readJson(host, '.eslintrc.json'); const rootESLintConfig = readJson(host, '.eslintrc.json');
if (hasRulesRequiringTypeChecking(rootESLintConfig)) { if (hasRulesRequiringTypeChecking(rootESLintConfig)) {