-
Notifications
You must be signed in to change notification settings - Fork 12k
feat(@schematics/angular): add lazy module path fixer #14181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexeagle
merged 1 commit into
angular:master
from
phenomnomnominal:update-lazy-module-paths-fix
Apr 16, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/schematics/angular/migrations/update-8/rules/noLazyModulePathsRule.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* 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, | ||
Rules, | ||
} from 'tslint'; // tslint:disable-line:no-implicit-dependencies | ||
import * as ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
|
||
// 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); | ||
}); | ||
} | ||
|
||
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/schematics/angular/migrations/update-8/update-lazy-module-paths.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* 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 { | ||
phenomnomnominal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Rule, | ||
SchematicContext, | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
TslintFixTask, | ||
} from '@angular-devkit/schematics/tasks'; | ||
import * as path from 'path'; | ||
|
||
export const updateLazyModulePaths = (): Rule => { | ||
return (_: Tree, context: SchematicContext) => { | ||
context.addTask(new TslintFixTask({ | ||
rulesDirectory: path.join(__dirname, 'rules'), | ||
rules: { | ||
'no-lazy-module-paths': [true], | ||
}, | ||
}, { | ||
includes: '**/*.ts', | ||
silent: false, | ||
})); | ||
}; | ||
}; |
85 changes: 85 additions & 0 deletions
85
packages/schematics/angular/migrations/update-8/update-lazy-module-paths_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* 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 { getSystemPath, normalize, virtualFs } from '@angular-devkit/core'; | ||
import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing'; | ||
import { HostTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
||
describe('Migration to version 8', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
let host: TempScopedNodeJsSyncHost; | ||
|
||
const lazyRoutePath = normalize('src/lazy-route.ts'); | ||
const lazyRoute = virtualFs.stringToFileBuffer(` | ||
import { Route } from '@angular/router'; | ||
const routes: Array<Route> = [ | ||
{ | ||
path: '', | ||
loadChildren: './lazy/lazy.module#LazyModule' | ||
} | ||
]; | ||
`); | ||
|
||
const lazyChildRoute = virtualFs.stringToFileBuffer(` | ||
import { Route } from '@angular/router'; | ||
const routes: Array<Route> = [ | ||
{ | ||
path: '', | ||
children: [{ | ||
path: 'child', | ||
loadChildren: './lazy/lazy.module#LazyModule' | ||
}] | ||
} | ||
]; | ||
`); | ||
|
||
describe('Migration to import() style lazy routes', () => { | ||
beforeEach(async () => { | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
tree.create('/package.json', JSON.stringify({})); | ||
process.chdir(getSystemPath(host.root)); | ||
}); | ||
|
||
it('should replace the module path string', async () => { | ||
await host.write(lazyRoutePath, lazyRoute).toPromise(); | ||
|
||
schematicRunner.runSchematic('migration-08', {}, tree); | ||
await schematicRunner.engine.executePostTasks().toPromise(); | ||
|
||
const routes = await host.read(lazyRoutePath) | ||
.toPromise() | ||
.then(virtualFs.fileBufferToString); | ||
|
||
expect(routes).not.toContain('./lazy/lazy.module#LazyModule'); | ||
expect(routes).toContain( | ||
`loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); | ||
}); | ||
|
||
it('should replace the module path string in a child path', async () => { | ||
await host.write(lazyRoutePath, lazyChildRoute).toPromise(); | ||
|
||
schematicRunner.runSchematic('migration-08', {}, tree); | ||
await schematicRunner.engine.executePostTasks().toPromise(); | ||
|
||
const routes = await host.read(lazyRoutePath) | ||
.toPromise() | ||
.then(virtualFs.fileBufferToString); | ||
|
||
expect(routes).not.toContain('./lazy/lazy.module#LazyModule'); | ||
|
||
expect(routes).toContain( | ||
`loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.