From 970115c942d270bcfc5f7c4ef6ae825203f15704 Mon Sep 17 00:00:00 2001 From: James Henry Date: Tue, 23 Aug 2022 14:29:09 +0400 Subject: [PATCH] fix(schematics): prefer sourceRoot if available for root project (#1114) --- .../convert-to-eslint-config.ts | 2 +- packages/schematics/src/utils.ts | 2 +- .../tests/add-eslint-to-project/index.test.ts | 140 ++++++++++++++++++ 3 files changed, 142 insertions(+), 2 deletions(-) diff --git a/packages/schematics/src/convert-tslint-to-eslint/convert-to-eslint-config.ts b/packages/schematics/src/convert-tslint-to-eslint/convert-to-eslint-config.ts index e0d46c1c1b..54cc4abe4b 100644 --- a/packages/schematics/src/convert-tslint-to-eslint/convert-to-eslint-config.ts +++ b/packages/schematics/src/convert-tslint-to-eslint/convert-to-eslint-config.ts @@ -163,7 +163,7 @@ export function convertTSLintDisableCommentsForProject( // Default Angular CLI project at the root of the workspace if (existingProjectConfig.root === '') { - pathRoot = 'src'; + pathRoot = existingProjectConfig.sourceRoot || 'src'; } else { pathRoot = existingProjectConfig.root; } diff --git a/packages/schematics/src/utils.ts b/packages/schematics/src/utils.ts index 7b79494e25..7faf93d469 100644 --- a/packages/schematics/src/utils.ts +++ b/packages/schematics/src/utils.ts @@ -162,7 +162,7 @@ export function addESLintTargetToProject( // Default Angular CLI project at the root of the workspace if (existingProjectConfig.root === '') { - lintFilePatternsRoot = 'src'; + lintFilePatternsRoot = existingProjectConfig.sourceRoot || 'src'; } else { lintFilePatternsRoot = existingProjectConfig.root; } diff --git a/packages/schematics/tests/add-eslint-to-project/index.test.ts b/packages/schematics/tests/add-eslint-to-project/index.test.ts index 3d89b47887..1bb7fdf937 100644 --- a/packages/schematics/tests/add-eslint-to-project/index.test.ts +++ b/packages/schematics/tests/add-eslint-to-project/index.test.ts @@ -302,4 +302,144 @@ describe('add-eslint-to-project', () => { } `); }); + + describe('custom root project sourceRoot', () => { + let tree2: UnitTestTree; + + beforeEach(() => { + tree2 = new UnitTestTree(Tree.empty()); + tree2.create('package.json', JSON.stringify({})); + tree2.create( + 'angular.json', + JSON.stringify({ + $schema: './node_modules/@angular/cli/lib/config/schema.json', + version: 1, + newProjectRoot: 'projects', + projects: { + [rootProjectName]: { + projectType: 'application', + schematics: {}, + root: '', + sourceRoot: 'custom-source-root', + prefix: 'app', + architect: { + build: {}, + serve: {}, + 'extract-i18n': {}, + test: {}, + lint: {}, + }, + }, + [legacyProjectName]: { + projectType: 'application', + schematics: {}, + root: `projects/${legacyProjectName}`, + sourceRoot: `projects/${legacyProjectName}/src`, + prefix: 'app', + architect: { + build: {}, + serve: {}, + 'extract-i18n': {}, + test: {}, + lint: {}, + e2e: {}, + }, + }, + [otherProjectName]: { + projectType: 'application', + schematics: {}, + root: `projects/${otherProjectName}`, + sourceRoot: `projects/${otherProjectName}/src`, + prefix: 'app', + architect: { + build: {}, + serve: {}, + 'extract-i18n': {}, + test: {}, + lint: {}, + }, + }, + }, + }), + ); + }); + + it('should correctly add ESLint to the Angular CLI root project even when it has a custom sourceRoot', async () => { + const options = { + project: rootProjectName, + }; + + await schematicRunner + .runSchematicAsync('add-eslint-to-project', options, tree2) + .toPromise(); + + expect( + readJsonInTree(tree2, 'angular.json').projects[rootProjectName] + .architect.lint, + ).toMatchInlineSnapshot(` + Object { + "builder": "@angular-eslint/builder:lint", + "options": Object { + "lintFilePatterns": Array [ + "custom-source-root/**/*.ts", + "custom-source-root/**/*.html", + ], + }, + } + `); + + expect(readJsonInTree(tree2, '.eslintrc.json')).toMatchInlineSnapshot(` + Object { + "ignorePatterns": Array [ + "projects/**/*", + ], + "overrides": Array [ + Object { + "extends": Array [ + "plugin:@angular-eslint/recommended", + "plugin:@angular-eslint/template/process-inline-templates", + ], + "files": Array [ + "*.ts", + ], + "parserOptions": Object { + "createDefaultProgram": true, + "project": Array [ + "tsconfig.json", + ], + }, + "rules": Object { + "@angular-eslint/component-selector": Array [ + "error", + Object { + "prefix": "app", + "style": "kebab-case", + "type": "element", + }, + ], + "@angular-eslint/directive-selector": Array [ + "error", + Object { + "prefix": "app", + "style": "camelCase", + "type": "attribute", + }, + ], + }, + }, + Object { + "extends": Array [ + "plugin:@angular-eslint/template/recommended", + ], + "files": Array [ + "*.html", + ], + "rules": Object {}, + }, + ], + "root": true, + } + `); + }); + }); });