Skip to content

Commit ef3bee6

Browse files
cexbrayatclydin
authored andcommitted
fix(@schematics/angular): keep tslint rules ordered
The schematics recently siwtcher to `jsonc-parser` as the JSOn parser, and that introduced a small regression in the tslint.json file where the rules order is not the same as previously. See cexbrayat/angular-cli-diff@10.0.2...10.1.0-next.0 This commit should fix the issue by providing the insertion index when merging the tslint files. A test has also been added to avoid further regressions.
1 parent 0b20676 commit ef3bee6

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

packages/schematics/angular/application/index.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,16 @@ function readTsLintConfig(host: Tree, path: string): JsonAstObject {
9696
function mergeWithRootTsLint(parentHost: Tree) {
9797
return (host: Tree) => {
9898
const tsLintPath = '/tslint.json';
99+
const rulesPath = ['rules'];
99100
if (!host.exists(tsLintPath)) {
100101
return;
101102
}
102103

103-
const rootTslintConfig = new JSONFile(parentHost, tsLintPath);
104-
const appTslintConfig = new JSONFile(host, tsLintPath);
105-
106-
for (const [pKey, pValue] of Object.entries(rootTslintConfig.get([]) as Record<string, JsonValue>)) {
107-
if (typeof pValue !== 'object' || Array.isArray(pValue)) {
108-
appTslintConfig.modify([pKey], pValue);
109-
continue;
110-
}
111-
112-
for (const [key, value] of Object.entries(rootTslintConfig.get([pKey]) as Record<string, JsonObject>)) {
113-
appTslintConfig.modify([pKey, key], value);
114-
}
115-
}
104+
const rootTsLintFile = new JSONFile(parentHost, tsLintPath);
105+
const rootRules = rootTsLintFile.get(rulesPath) as {};
106+
const appRules = new JSONFile(host, tsLintPath).get(rulesPath) as {};
107+
rootTsLintFile.modify(rulesPath, { ...rootRules, ...appRules });
108+
host.overwrite(tsLintPath, rootTsLintFile.content);
116109
};
117110
}
118111

packages/schematics/angular/application/index_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ describe('Application Schematic', () => {
416416
expect(content.extends).toMatch('tslint:recommended');
417417
expect(content.rules['component-selector'][2]).toMatch('app');
418418
expect(content.rules['no-console']).toBeDefined();
419+
// codelyzer rules should be after base tslint rules
420+
expect(Object.keys(content.rules).indexOf('component-selector'))
421+
.toBeGreaterThan(Object.keys(content.rules).indexOf('no-console'));
419422
});
420423

421424
it(`should create correct paths when 'newProjectRoot' is blank`, async () => {

packages/schematics/angular/utility/json-file.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { JsonValue } from '@angular-devkit/core';
1010
import { Tree } from '@angular-devkit/schematics';
1111
import { Node, applyEdits, findNodeAtLocation, getNodeValue, modify, parseTree } from 'jsonc-parser';
1212

13+
export type InsertionIndex = (properties: string[]) => number;
1314
export type JSONPath = (string | number)[];
1415

1516
/** @internal */
1617
export class JSONFile {
17-
private content: string;
18+
content: string;
1819
error: undefined | Error;
1920

2021
constructor(
@@ -50,10 +51,13 @@ export class JSONFile {
5051
return node === undefined ? undefined : getNodeValue(node);
5152
}
5253

53-
modify(jsonPath: JSONPath, value: JsonValue | undefined, getInsertionIndex?: (properties: string[]) => number): void {
54-
if (!getInsertionIndex) {
54+
modify(jsonPath: JSONPath, value: JsonValue | undefined, insertInOrder?: InsertionIndex | false): void {
55+
let getInsertionIndex: InsertionIndex | undefined;
56+
if (insertInOrder === undefined) {
5557
const property = jsonPath.slice(-1)[0];
5658
getInsertionIndex = properties => [...properties, property].sort().findIndex(p => p === property);
59+
} else if (insertInOrder !== false) {
60+
getInsertionIndex = insertInOrder;
5761
}
5862

5963
const edits = modify(

0 commit comments

Comments
 (0)