Skip to content

Commit

Permalink
fix(@schematics/angular): remove tsquery peer dependency warning
Browse files Browse the repository at this point in the history
Replaces the usage of the tsquery package with a conditional based on TypeScript helper functions.  Since a TypeScript-subset is vendored within the package, a TypeScript peer dependency cannot be met even though the functionality is present.

Fixes #14270
  • Loading branch information
clydin authored and alexeagle committed Apr 25, 2019
1 parent ccbac7c commit 5765777
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 49 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
]
},
"dependencies": {
"@phenomnomnominal/tsquery": "3.0.0",
"@types/debug": "^4.1.2",
"@types/node-fetch": "^2.1.6",
"@types/progress": "^2.0.3",
Expand Down
2 changes: 0 additions & 2 deletions packages/schematics/angular/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ ts_library(
"//packages/angular_devkit/schematics",
"//packages/angular_devkit/schematics:tasks",
"//packages/schematics/angular/third_party/github.com/Microsoft/TypeScript",
"@npm//@phenomnomnominal/tsquery",
"@npm//@types/node",
"@npm//rxjs",
"@npm//tslint",
Expand Down Expand Up @@ -95,7 +94,6 @@ ts_library(
"//packages/angular_devkit/schematics",
"//packages/angular_devkit/schematics:testing",
"//packages/schematics/angular/third_party/github.com/Microsoft/TypeScript",
"@npm//@phenomnomnominal/tsquery",
"@npm//@types/node",
"@npm//@types/jasmine",
"@npm//rxjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { tsquery } from '@phenomnomnominal/tsquery';
import {
Replacement,
RuleFailure,
Expand All @@ -16,39 +14,41 @@ import * as ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/ty

// Constants:
const LOAD_CHILDREN_SPLIT = '#';
const NOT_CHILDREN_QUERY = `:not(:has(Identifier[name="children"]))`;
const HAS_LOAD_CHILDREN_QUERY = `:has(Identifier[name="loadChildren"])`;
const LAZY_VALUE_QUERY = `StringLiteral[value=/.*${LOAD_CHILDREN_SPLIT}.*/]`;
const LOAD_CHILDREN_ASSIGNMENT_QUERY =
`PropertyAssignment${NOT_CHILDREN_QUERY}${HAS_LOAD_CHILDREN_QUERY}:has(${LAZY_VALUE_QUERY})`;

const FAILURE_MESSAGE = 'Found magic `loadChildren` string. Use a function with `import` instead.';

export class Rule extends Rules.AbstractRule {
public apply (ast: ts.SourceFile): Array<RuleFailure> {
return tsquery(ast, LOAD_CHILDREN_ASSIGNMENT_QUERY).map(result => {
const [valueNode] = tsquery(result, LAZY_VALUE_QUERY);
let fix = this._promiseReplacement(valueNode.text);

// Try to fix indentation in replacement:
const { character } = ast.getLineAndCharacterOfPosition(result.getStart());
fix = fix.replace(/\n/g, `\n${' '.repeat(character)}`);

const replacement = new Replacement(valueNode.getStart(), valueNode.getWidth(), fix);
const start = result.getStart();
const end = result.getEnd();

return new RuleFailure(ast, start, end, FAILURE_MESSAGE, this.ruleName, replacement);
const ruleName = this.ruleName;
const changes: RuleFailure[] = [];

ts.forEachChild(ast, function analyze(node) {
if (ts.isPropertyAssignment(node) &&
(ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) &&
node.name.text === 'loadChildren' &&
ts.isStringLiteral(node.initializer)) {
const valueNode = node.initializer;
const parts = valueNode.text.split(LOAD_CHILDREN_SPLIT);
const path = parts[0];
const moduleName = parts[1] || 'default';

let fix = `() => import('${path}').then(m => m.${moduleName})`;

// Try to fix indentation in replacement:
const { character } = ast.getLineAndCharacterOfPosition(node.getStart());
fix = fix.replace(/\n/g, `\n${' '.repeat(character)}`);

const replacement = new Replacement(valueNode.getStart(), valueNode.getWidth(), fix);
const start = node.getStart();
const end = node.getEnd();

const change = new RuleFailure(ast, start, end, FAILURE_MESSAGE, ruleName, replacement);
change.setRuleSeverity('warning');
changes.push(change);
}

ts.forEachChild(node, analyze);
});
}

private _promiseReplacement (loadChildren: string): string {
const [path, moduleName] = this._getChunks(loadChildren);

return `() => import('${path}').then(m => m.${moduleName})`;
}

private _getChunks (loadChildren: string): Array<string> {
return loadChildren.split(LOAD_CHILDREN_SPLIT);
return changes;
}
}
1 change: 0 additions & 1 deletion packages/schematics/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
],
"schematics": "./collection.json",
"dependencies": {
"@phenomnomnominal/tsquery": "3.0.0",
"@angular-devkit/core": "0.0.0",
"@angular-devkit/schematics": "0.0.0"
}
Expand Down
16 changes: 1 addition & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,6 @@
resolved "https://registry.yarnpkg.com/@ngtools/json-schema/-/json-schema-1.1.0.tgz#c3a0c544d62392acc2813a42c8a0dc6f58f86922"
integrity sha1-w6DFRNYjkqzCgTpCyKDcb1j4aSI=

"@phenomnomnominal/[email protected]":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@phenomnomnominal/tsquery/-/tsquery-3.0.0.tgz#6f2f4dbf6304ff52b12cc7a5b979f20c3794a22a"
integrity sha512-SW8lKitBHWJ9fAYkJ9kJivuctwNYCh3BUxLdH0+XiR1GPBiu+7qiZzh8p8jqlj1LgVC1TbvfNFroaEsmYlL8Iw==
dependencies:
esquery "^1.0.1"

"@sindresorhus/is@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
Expand Down Expand Up @@ -3392,13 +3385,6 @@ esprima@^4.0.0:
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

esquery@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.0.1.tgz#406c51658b1f5991a5f9b62b1dc25b00e3e5c708"
integrity sha512-SmiyZ5zIWH9VM+SRUReLS5Q8a7GxtRdxEBVZpm98rJM7Sb+A9DVCndXfkeFUd3byderg+EbDkfnevfCwynWaNA==
dependencies:
estraverse "^4.0.0"

esrecurse@^4.1.0:
version "4.2.1"
resolved "https://registry.yarnpkg.com/esrecurse/-/esrecurse-4.2.1.tgz#007a3b9fdbc2b3bb87e4879ea19c92fdbd3942cf"
Expand All @@ -3411,7 +3397,7 @@ estraverse@^1.9.1:
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-1.9.3.tgz#af67f2dc922582415950926091a4005d29c9bb44"
integrity sha1-r2fy3JIlgkFZUJJgkaQAXSnJu0Q=

estraverse@^4.0.0, estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0:
estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13"
integrity sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=
Expand Down

0 comments on commit 5765777

Please sign in to comment.