Skip to content

Commit

Permalink
fix(schematics): prefer sourceRoot if available for root project (ang…
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry authored and jmeinlschmidt committed Sep 18, 2022
1 parent 96daadc commit 970115c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/src/utils.ts
Expand Up @@ -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;
}
Expand Down
140 changes: 140 additions & 0 deletions packages/schematics/tests/add-eslint-to-project/index.test.ts
Expand Up @@ -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,
}
`);
});
});
});

0 comments on commit 970115c

Please sign in to comment.