From 4365c010e66b2806afbe46b6eadc23d04adb8f81 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 6 Sep 2023 12:20:28 -0400 Subject: [PATCH] fix(linter): handle non-JSON eslintrc files when updating overrides (#19026) (cherry picked from commit 8c1f183659d233ba014613314daf70ddd911701d) --- e2e/node/src/node.test.ts | 2 +- .../src/generators/utils/eslint-file.spec.ts | 37 +++++++++++++++++++ .../src/generators/utils/eslint-file.ts | 5 ++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 36fd194093276..d9dd301e8ddd4 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -142,7 +142,7 @@ describe('Node Applications', () => { } ).toString(); expect(additionalResult).toContain('Hello Additional World!'); - }, 60000); + }, 300_000); it('should be able to generate an empty application with variable in .env file', async () => { const originalEnvPort = process.env.PORT; diff --git a/packages/linter/src/generators/utils/eslint-file.spec.ts b/packages/linter/src/generators/utils/eslint-file.spec.ts index 69f0859ae3f6a..0556f3c56a257 100644 --- a/packages/linter/src/generators/utils/eslint-file.spec.ts +++ b/packages/linter/src/generators/utils/eslint-file.spec.ts @@ -2,6 +2,7 @@ import { baseEsLintConfigFile, eslintConfigFileWhitelist, findEslintFile, + lintConfigHasOverride, } from './eslint-file'; import { Tree } from '@nx/devkit'; @@ -36,4 +37,40 @@ describe('@nx/linter:eslint-file', () => { } ); }); + + describe('lintConfigHasOverride', () => { + it('should return true when override exists in eslintrc format', () => { + tree.write( + '.eslintrc.json', + '{"overrides": [{ "files": ["*.ts"], "rules": {} }]}' + ); + expect( + lintConfigHasOverride( + tree, + '.', + (o) => { + return o.files?.includes('*.ts'); + }, + false + ) + ).toBe(true); + }); + + it('should return false when eslintrc is not in JSON format', () => { + tree.write( + '.eslintrc.js', + 'module.exports = {overrides: [{ files: ["*.ts"], rules: {} }]};' + ); + expect( + lintConfigHasOverride( + tree, + '.', + (o) => { + return o.files?.includes('*.ts'); + }, + false + ) + ).toBe(false); + }); + }); }); diff --git a/packages/linter/src/generators/utils/eslint-file.ts b/packages/linter/src/generators/utils/eslint-file.ts index b4c06b5384d53..084e69a9322d9 100644 --- a/packages/linter/src/generators/utils/eslint-file.ts +++ b/packages/linter/src/generators/utils/eslint-file.ts @@ -247,7 +247,10 @@ export function lintConfigHasOverride( root, isBase ? baseEsLintConfigFile : '.eslintrc.json' ); - return readJson(tree, fileName).overrides?.some(lookup) || false; + + return tree.exists(fileName) + ? readJson(tree, fileName).overrides?.some(lookup) || false + : false; } }