From 7e1cf531caf610ac05d1e3915af7c482e86808f9 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 25 Sep 2024 13:12:24 -0400 Subject: [PATCH] fix(linter): add files entry to angular flat config to avoid applying TS rules to JSON files (#28102) This PR fixes an issue with buildable/publishable Angular libs, where TS rules are being applied to JSON files. ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #28069 --------- Co-authored-by: James Henry --- e2e/angular/src/projects.test.ts | 3 ++ .../eslint-plugin/src/flat-configs/angular.ts | 30 ++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/e2e/angular/src/projects.test.ts b/e2e/angular/src/projects.test.ts index ac2d6f5b49..f191fbe9d1 100644 --- a/e2e/angular/src/projects.test.ts +++ b/e2e/angular/src/projects.test.ts @@ -499,6 +499,9 @@ describe('Angular Projects', () => { `Building entry point '@${proj}/${lib}/${entryPoint}'` ); expect(buildOutput).toContain('Successfully ran target build'); + + expect(() => runCLI(`lint ${lib} --fix`)).not.toThrow(); + expect(() => runCLI(`lint ${childLib} --fix`)).not.toThrow(); }); it('should support generating libraries with a scoped name when --project-name-and-root-format=as-provided', () => { diff --git a/packages/eslint-plugin/src/flat-configs/angular.ts b/packages/eslint-plugin/src/flat-configs/angular.ts index 12880957aa..e0abe64e0c 100644 --- a/packages/eslint-plugin/src/flat-configs/angular.ts +++ b/packages/eslint-plugin/src/flat-configs/angular.ts @@ -13,14 +13,24 @@ import tseslint from 'typescript-eslint'; * This configuration is intended to be combined with other configs from this * package. */ -export default tseslint.config(...angularEslint.configs.tsRecommended, { - languageOptions: { - globals: { - ...globals.browser, - ...globals.es2015, - ...globals.node, +export default tseslint.config( + ...angularEslint.configs.tsRecommended.map((c) => ({ + // Files need to be specified or else typescript-eslint rules will be + // applied to non-TS files. For example, buildable/publishable libs + // add rules to *.json files, and TS rules should not apply to them. + // See: https://github.com/nrwl/nx/issues/28069 + files: ['**/*.ts'], + ...c, + })), + { + languageOptions: { + globals: { + ...globals.browser, + ...globals.es2015, + ...globals.node, + }, }, - }, - processor: angularEslint.processInlineTemplates, - plugins: { '@angular-eslint': angularEslint.tsPlugin }, -}); + processor: angularEslint.processInlineTemplates, + plugins: { '@angular-eslint': angularEslint.tsPlugin }, + } +);