forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
177 lines (152 loc) · 4.55 KB
/
types.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @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 {Attribute, Element, RecursiveVisitor} from '@angular/compiler';
export const ngif = '*ngIf';
export const boundngif = '[ngIf]';
export const nakedngif = 'ngIf';
export const ngfor = '*ngFor';
export const ngswitch = '[ngSwitch]';
export const boundcase = '[ngSwitchCase]';
export const switchcase = '*ngSwitchCase';
export const nakedcase = 'ngSwitchCase';
export const switchdefault = '*ngSwitchDefault';
export const nakeddefault = 'ngSwitchDefault';
const attributesToMigrate = [
ngif,
nakedngif,
boundngif,
ngfor,
ngswitch,
boundcase,
switchcase,
nakedcase,
switchdefault,
nakeddefault,
];
/**
* Represents a range of text within a file. Omitting the end
* means that it's until the end of the file.
*/
type Range = [start: number, end?: number];
export type Offsets = {
pre: number,
post: number,
};
export type Result = {
tmpl: string,
offsets: Offsets,
};
/**
* Represents an error that happened during migration
*/
export type MigrateError = {
type: string,
error: unknown,
};
/**
* Represents an element with a migratable attribute
*/
export class ElementToMigrate {
el: Element;
attr: Attribute;
nestCount = 0;
hasLineBreaks = false;
constructor(el: Element, attr: Attribute) {
this.el = el;
this.attr = attr;
}
getCondition(targetStr: string): string {
const targetLocation = this.attr.value.indexOf(targetStr);
return this.attr.value.slice(0, targetLocation);
}
getTemplateName(targetStr: string, secondStr?: string): string {
const targetLocation = this.attr.value.indexOf(targetStr);
if (secondStr) {
const secondTargetLocation = this.attr.value.indexOf(secondStr);
return this.attr.value.slice(targetLocation + targetStr.length, secondTargetLocation).trim();
}
return this.attr.value.slice(targetLocation + targetStr.length).trim();
}
start(offset: number): number {
return this.el.sourceSpan?.start.offset - offset;
}
end(offset: number): number {
return this.el.sourceSpan?.end.offset - offset;
}
length(): number {
return this.el.sourceSpan?.end.offset - this.el.sourceSpan?.start.offset;
}
}
export class Template {
el: Element;
count: number = 0;
contents: string = '';
children: string = '';
used: boolean = false;
constructor(el: Element) {
this.el = el;
}
generateContents(tmpl: string) {
this.contents = tmpl.slice(this.el.sourceSpan.start.offset, this.el.sourceSpan.end.offset + 1);
this.children = '';
if (this.el.children.length > 0) {
this.children = tmpl.slice(
this.el.children[0].sourceSpan.start.offset,
this.el.children[this.el.children.length - 1].sourceSpan.end.offset);
}
}
}
/** Represents a file that was analyzed by the migration. */
export class AnalyzedFile {
private ranges: Range[] = [];
/** Returns the ranges in the order in which they should be migrated. */
getSortedRanges(): Range[] {
return this.ranges.slice().sort(([aStart], [bStart]) => bStart - aStart);
}
/**
* Adds a text range to an `AnalyzedFile`.
* @param path Path of the file.
* @param analyzedFiles Map keeping track of all the analyzed files.
* @param range Range to be added.
*/
static addRange(path: string, analyzedFiles: Map<string, AnalyzedFile>, range: Range): void {
let analysis = analyzedFiles.get(path);
if (!analysis) {
analysis = new AnalyzedFile();
analyzedFiles.set(path, analysis);
}
const duplicate =
analysis.ranges.find(current => current[0] === range[0] && current[1] === range[1]);
if (!duplicate) {
analysis.ranges.push(range);
}
}
}
/** Finds all elements with control flow structural directives. */
export class ElementCollector extends RecursiveVisitor {
readonly elements: ElementToMigrate[] = [];
readonly templates: Map<string, Template> = new Map();
override visitElement(el: Element): void {
if (el.attrs.length > 0) {
for (const attr of el.attrs) {
if (attributesToMigrate.includes(attr.name)) {
this.elements.push(new ElementToMigrate(el, attr));
}
}
}
if (el.name === 'ng-template') {
for (const attr of el.attrs) {
if (attr.name.startsWith('#')) {
this.elements.push(new ElementToMigrate(el, attr));
this.templates.set(attr.name, new Template(el));
}
}
}
super.visitElement(el, null);
}
}