-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
index.ts
157 lines (133 loc) · 6.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @license
* Copyright Google LLC 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 {logging, normalize} from '@angular-devkit/core';
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {EmptyExpr, TmplAstBoundAttribute} from '@angular/compiler';
import {relative} from 'path';
import {NgComponentTemplateVisitor, ResolvedTemplate} from '../../utils/ng_component_template';
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';
import {analyzeResolvedTemplate} from './analyze_template';
type Logger = logging.LoggerApi;
const README_URL =
'https://github.com/angular/angular/blob/master/packages/core/schematics/migrations/router-link-empty-expression/README.md';
interface FixedTemplate {
originalTemplate: ResolvedTemplate;
newContent: string;
emptyRouterlinkExpressions: TmplAstBoundAttribute[];
}
/** Entry point for the RouterLink empty expression migration. */
export default function(): Rule {
return (tree: Tree, context: SchematicContext) => {
const {buildPaths, testPaths} = getProjectTsConfigPaths(tree);
const basePath = process.cwd();
if (!buildPaths.length && !testPaths.length) {
throw new SchematicsException(
'Could not find any tsconfig file. Cannot check templates for empty routerLinks.');
}
for (const tsconfigPath of [...buildPaths, ...testPaths]) {
runEmptyRouterLinkExpressionMigration(tree, tsconfigPath, basePath, context.logger);
}
};
}
/**
* Runs the routerLink migration, changing routerLink="" to routerLink="[]" and notifying developers
* which templates received updates.
*/
function runEmptyRouterLinkExpressionMigration(
tree: Tree, tsconfigPath: string, basePath: string, logger: Logger) {
const {program} = createMigrationProgram(tree, tsconfigPath, basePath);
const typeChecker = program.getTypeChecker();
const templateVisitor = new NgComponentTemplateVisitor(typeChecker);
const sourceFiles =
program.getSourceFiles().filter(sourceFile => canMigrateFile(basePath, sourceFile, program));
// Analyze source files by detecting HTML templates.
sourceFiles.forEach(sourceFile => templateVisitor.visitNode(sourceFile));
const {resolvedTemplates} = templateVisitor;
fixEmptyRouterlinks(resolvedTemplates, tree, logger);
}
function fixEmptyRouterlinks(resolvedTemplates: ResolvedTemplate[], tree: Tree, logger: Logger) {
const basePath = process.cwd();
const collectedFixes: string[] = [];
const fixesByFile = getFixesByFile(resolvedTemplates);
for (const [absFilePath, fixes] of fixesByFile) {
const treeFilePath = relative(normalize(basePath), normalize(absFilePath));
const originalFileContent = tree.read(treeFilePath)?.toString();
if (originalFileContent === undefined) {
logger.error(
`Failed to read file containing template; cannot apply fixes for empty routerLink expressions in ${
treeFilePath}.`);
continue;
}
const updater = tree.beginUpdate(treeFilePath);
for (const fix of fixes) {
const displayFilePath = normalize(relative(basePath, fix.originalTemplate.filePath));
updater.remove(fix.originalTemplate.start, fix.originalTemplate.content.length);
updater.insertLeft(fix.originalTemplate.start, fix.newContent);
for (const n of fix.emptyRouterlinkExpressions) {
const {line, character} =
fix.originalTemplate.getCharacterAndLineOfPosition(n.sourceSpan.start.offset);
collectedFixes.push(`${displayFilePath}@${line + 1}:${character + 1}`);
}
tree.commitUpdate(updater);
}
}
if (collectedFixes.length > 0) {
logger.info('---- RouterLink empty assignment schematic ----');
logger.info('The behavior of empty/`undefined` inputs for `routerLink` has changed');
logger.info('from linking to the current page to instead completely disable the link.');
logger.info(`Read more about this change here: ${README_URL}`);
logger.info('');
logger.info('The following empty `routerLink` inputs were found and fixed:');
collectedFixes.forEach(fix => logger.warn(`⮑ ${fix}`));
}
}
/**
* Returns fixes for nodes in templates which contain empty routerLink assignments, grouped by file.
*/
function getFixesByFile(templates: ResolvedTemplate[]): Map<string, FixedTemplate[]> {
const fixesByFile = new Map<string, FixedTemplate[]>();
for (const template of templates) {
const templateFix = fixEmptyRouterlinksInTemplate(template);
if (templateFix === null) {
continue;
}
const file = template.filePath;
if (fixesByFile.has(file)) {
if (template.inline) {
// External templates may be referenced multiple times in the project
// (e.g. if shared between components), but we only want to record them
// once. On the other hand, an inline template resides in a TS file that
// may contain multiple inline templates.
fixesByFile.get(file)!.push(templateFix);
}
} else {
fixesByFile.set(file, [templateFix]);
}
}
return fixesByFile;
}
function fixEmptyRouterlinksInTemplate(template: ResolvedTemplate): FixedTemplate|null {
const emptyRouterlinkExpressions = analyzeResolvedTemplate(template);
if (!emptyRouterlinkExpressions) {
return null;
}
// Sort backwards so string replacements do not conflict
emptyRouterlinkExpressions.sort((a, b) => b.value.sourceSpan.start - a.value.sourceSpan.start);
let newContent = template.content;
for (const expr of emptyRouterlinkExpressions) {
if (expr.valueSpan) {
newContent = newContent.substr(0, expr.value.sourceSpan.start) + '[]' +
newContent.substr(expr.value.sourceSpan.start);
} else {
newContent = newContent.substr(0, expr.sourceSpan.end.offset) + '="[]"' +
newContent.substr(expr.sourceSpan.end.offset);
}
}
return {originalTemplate: template, newContent, emptyRouterlinkExpressions};
}