diff --git a/package.json b/package.json index a0c2126e937d..ba3926434399 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/schematics/angular/BUILD b/packages/schematics/angular/BUILD index 5bc09cbbcf8f..cfacc05a342f 100644 --- a/packages/schematics/angular/BUILD +++ b/packages/schematics/angular/BUILD @@ -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", @@ -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", diff --git a/packages/schematics/angular/migrations/update-8/rules/noLazyModulePathsRule.ts b/packages/schematics/angular/migrations/update-8/rules/noLazyModulePathsRule.ts index 24ef937cf9d0..1c15e84884a0 100644 --- a/packages/schematics/angular/migrations/update-8/rules/noLazyModulePathsRule.ts +++ b/packages/schematics/angular/migrations/update-8/rules/noLazyModulePathsRule.ts @@ -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, @@ -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 { - 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 { - return loadChildren.split(LOAD_CHILDREN_SPLIT); + return changes; } } diff --git a/packages/schematics/angular/package.json b/packages/schematics/angular/package.json index e1291b75a864..783b2a424160 100644 --- a/packages/schematics/angular/package.json +++ b/packages/schematics/angular/package.json @@ -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" } diff --git a/yarn.lock b/yarn.lock index c7f2e34ec12a..2bedc0b45472 100644 --- a/yarn.lock +++ b/yarn.lock @@ -270,13 +270,6 @@ resolved "https://registry.yarnpkg.com/@ngtools/json-schema/-/json-schema-1.1.0.tgz#c3a0c544d62392acc2813a42c8a0dc6f58f86922" integrity sha1-w6DFRNYjkqzCgTpCyKDcb1j4aSI= -"@phenomnomnominal/tsquery@3.0.0": - 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" @@ -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" @@ -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=