From bcb5965ec545ddeaca0af09fe01e3e4a52c07617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Thu, 7 Sep 2023 15:14:45 +0200 Subject: [PATCH] fix(angular): keep dependency-checks enabled for buildable libraries (#19047) --- .../__snapshots__/add-linting.spec.ts.snap | 14 ---- .../src/generators/add-linting/add-linting.ts | 26 +++++-- .../application/application.spec.ts | 7 -- .../convert-tslint-to-eslint.spec.ts.snap | 21 ----- .../src/generators/library/library.spec.ts | 78 +++++++++++++++++-- 5 files changed, 92 insertions(+), 54 deletions(-) diff --git a/packages/angular/src/generators/add-linting/__snapshots__/add-linting.spec.ts.snap b/packages/angular/src/generators/add-linting/__snapshots__/add-linting.spec.ts.snap index 88592bbd2e9ae..4a116b4918647 100644 --- a/packages/angular/src/generators/add-linting/__snapshots__/add-linting.spec.ts.snap +++ b/packages/angular/src/generators/add-linting/__snapshots__/add-linting.spec.ts.snap @@ -9,13 +9,6 @@ exports[`addLinting generator should correctly generate the .eslintrc.json file "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", @@ -65,13 +58,6 @@ exports[`addLinting generator support angular v14 should correctly generate the "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", diff --git a/packages/angular/src/generators/add-linting/add-linting.ts b/packages/angular/src/generators/add-linting/add-linting.ts index bf2842ec9b813..1500a0007b94c 100755 --- a/packages/angular/src/generators/add-linting/add-linting.ts +++ b/packages/angular/src/generators/add-linting/add-linting.ts @@ -2,6 +2,7 @@ import { formatFiles, GeneratorCallback, joinPathFragments, + readProjectConfiguration, runTasksInSerial, Tree, } from '@nx/devkit'; @@ -47,11 +48,6 @@ export async function addLintingGenerator( .includes(`${options.projectRoot}/tsconfig.*?.json`); replaceOverridesInLintConfig(tree, options.projectRoot, [ - { - files: ['*.json'], - parser: 'jsonc-eslint-parser', - rules: {}, - }, { files: ['*.ts'], ...(hasParserOptions @@ -93,6 +89,17 @@ export async function addLintingGenerator( */ rules: {}, }, + ...(isBuildableLibraryProject(tree, options.projectName) + ? [ + { + files: ['*.json'], + parser: 'jsonc-eslint-parser', + rules: { + '@nx/dependency-checks': 'error', + } as any, + }, + ] + : []), ]); } @@ -108,4 +115,13 @@ export async function addLintingGenerator( return runTasksInSerial(...tasks); } +function isBuildableLibraryProject(tree: Tree, projectName: string): boolean { + const projectConfig = readProjectConfiguration(tree, projectName); + return ( + projectConfig.projectType === 'library' && + projectConfig.targets?.build && + !!projectConfig.targets.build + ); +} + export default addLintingGenerator; diff --git a/packages/angular/src/generators/application/application.spec.ts b/packages/angular/src/generators/application/application.spec.ts index f4fcde7885d7d..299f297ef7fb3 100644 --- a/packages/angular/src/generators/application/application.spec.ts +++ b/packages/angular/src/generators/application/application.spec.ts @@ -512,13 +512,6 @@ describe('app', () => { "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", diff --git a/packages/angular/src/generators/convert-tslint-to-eslint/__snapshots__/convert-tslint-to-eslint.spec.ts.snap b/packages/angular/src/generators/convert-tslint-to-eslint/__snapshots__/convert-tslint-to-eslint.spec.ts.snap index d6ec3c7961526..793af59f669d0 100644 --- a/packages/angular/src/generators/convert-tslint-to-eslint/__snapshots__/convert-tslint-to-eslint.spec.ts.snap +++ b/packages/angular/src/generators/convert-tslint-to-eslint/__snapshots__/convert-tslint-to-eslint.spec.ts.snap @@ -253,13 +253,6 @@ exports[`convert-tslint-to-eslint should not override .eslint config if migratio "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", @@ -851,13 +844,6 @@ exports[`convert-tslint-to-eslint should work for Angular applications 4`] = ` "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", @@ -1211,13 +1197,6 @@ exports[`convert-tslint-to-eslint should work for Angular libraries 4`] = ` "!**/*", ], "overrides": [ - { - "files": [ - "*.json", - ], - "parser": "jsonc-eslint-parser", - "rules": {}, - }, { "extends": [ "plugin:@nx/angular", diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index 1caea7bd46963..6c7adb1305fa8 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -597,11 +597,6 @@ describe('lib', () => { "extends": ["../../.eslintrc.json"], "ignorePatterns": ["!**/*"], "overrides": [ - { - "files": ["*.json"], - "parser": "jsonc-eslint-parser", - "rules": {} - }, { "files": ["*.ts"], "extends": [ @@ -631,6 +626,13 @@ describe('lib', () => { "files": ["*.html"], "extends": ["plugin:@nx/angular-template"], "rules": {} + }, + { + "files": ["*.json"], + "parser": "jsonc-eslint-parser", + "rules": { + "@nx/dependency-checks": "error" + } } ] } @@ -1158,12 +1160,65 @@ describe('lib', () => { ], "overrides": [ { + "extends": [ + "plugin:@nx/angular", + "plugin:@angular-eslint/template/process-inline-templates", + ], "files": [ - "*.json", + "*.ts", + ], + "rules": { + "@angular-eslint/component-selector": [ + "error", + { + "prefix": "proj", + "style": "kebab-case", + "type": "element", + }, + ], + "@angular-eslint/directive-selector": [ + "error", + { + "prefix": "proj", + "style": "camelCase", + "type": "attribute", + }, + ], + }, + }, + { + "extends": [ + "plugin:@nx/angular-template", + ], + "files": [ + "*.html", ], - "parser": "jsonc-eslint-parser", "rules": {}, }, + ], + } + `); + }); + + it('should add dependency checks to buildable libs', async () => { + // ACT + await runLibraryGeneratorWithOpts({ + linter: Linter.EsLint, + buildable: true, + }); + + // ASSERT + + const eslintConfig = readJson(tree, 'my-lib/.eslintrc.json'); + expect(eslintConfig).toMatchInlineSnapshot(` + { + "extends": [ + "../.eslintrc.json", + ], + "ignorePatterns": [ + "!**/*", + ], + "overrides": [ { "extends": [ "plugin:@nx/angular", @@ -1200,6 +1255,15 @@ describe('lib', () => { ], "rules": {}, }, + { + "files": [ + "*.json", + ], + "parser": "jsonc-eslint-parser", + "rules": { + "@nx/dependency-checks": "error", + }, + }, ], } `);