-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ast-utils.ts
350 lines (296 loc) · 10.2 KB
/
ast-utils.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { getDecoratorMetadata, addImportToModule, addBootstrapToModule, addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
import { InsertChange, Change } from '@schematics/angular/utility/change';
import { SchematicsException, Rule, Tree } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { toComponentClassName } from './utils';
export interface Node {
getStart();
getFullStart();
getEnd();
}
class RemoveContent implements Node {
constructor(private pos: number, private end: number) {
}
public getStart() {
return this.pos;
}
public getFullStart() {
return this.pos;
}
public getEnd() {
return this.end;
}
}
export function addSymbolToComponentMetadata(
source: ts.SourceFile,
componentPath: string,
metadataField: string,
symbolName: string,
): Change[] {
return addSymbolToDecoratorMetadata(
source,
componentPath,
metadataField,
symbolName,
'Component',
'@angular/core'
);
}
// Almost identical to addSymbolToNgModuleMetadata from @schematics/angular/utility/ast-utils
// the method can be refactored so that it can be used with custom decorators
export function addSymbolToDecoratorMetadata(
source: ts.SourceFile,
componentPath: string,
metadataField: string,
symbolName: string,
decoratorName: string,
decoratorPackage: string,
): Change[] {
const nodes = getDecoratorMetadata(source, decoratorName, decoratorPackage);
let node: any = nodes[0]; // tslint:disable-line:no-any
// Find the decorator declaration.
if (!node) {
return [];
}
// Get all the children property assignment of object literals.
const matchingProperties: ts.ObjectLiteralElement[] =
(node as ts.ObjectLiteralExpression).properties
.filter(prop => prop.kind == ts.SyntaxKind.PropertyAssignment)
// Filter out every fields that's not 'metadataField'. Also handles string literals
// (but not expressions).
.filter((prop: ts.PropertyAssignment) => {
const name = prop.name;
switch (name.kind) {
case ts.SyntaxKind.Identifier:
return (name as ts.Identifier).getText(source) == metadataField;
case ts.SyntaxKind.StringLiteral:
return (name as ts.StringLiteral).text == metadataField;
}
return false;
});
// Get the last node of the array literal.
if (!matchingProperties) {
return [];
}
if (matchingProperties.length == 0) {
// We haven't found the field in the metadata declaration. Insert a new field.
const expr = node as ts.ObjectLiteralExpression;
let position: number;
let toInsert: string;
if (expr.properties.length == 0) {
position = expr.getEnd() - 1;
toInsert = ` ${metadataField}: [${symbolName}],\n`;
} else {
node = expr.properties[expr.properties.length - 1];
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
const matches = text.match(/^\r?\n\s*/);
if (matches.length > 0) {
toInsert = `,${matches[0]}${metadataField}: ${symbolName},`;
} else {
toInsert = `, ${metadataField}: ${symbolName},`;
}
}
return [new InsertChange(componentPath, position, toInsert)];
}
const assignment = matchingProperties[0] as ts.PropertyAssignment;
// If it's not an array, keep the original value.
if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return [];
}
const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
if (arrLiteral.elements.length == 0) {
// Forward the property.
node = arrLiteral;
} else {
node = arrLiteral.elements;
}
if (!node) {
console.log('No app module found. Please add your new class to your component.');
return [];
}
if (Array.isArray(node)) {
const nodeArray = node as {} as Array<ts.Node>;
const symbolsArray = nodeArray.map(node => node.getText());
if (symbolsArray.indexOf(symbolName) === -1) {
return [];
}
node = node[node.length - 1];
}
let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
// We haven't found the field in the metadata declaration. Insert a new
// field.
const expr = node as ts.ObjectLiteralExpression;
if (expr.properties.length == 0) {
position = expr.getEnd() - 1;
toInsert = ` ${metadataField}: [${symbolName}],\n`;
} else {
node = expr.properties[expr.properties.length - 1];
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match('^\r?\r?\n')) {
toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName},]`;
} else {
toInsert = `, ${metadataField}: [${symbolName},]`;
}
}
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
// We found the field but it's empty. Insert it just before the `]`.
position--;
toInsert = `${symbolName}`;
} else {
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match(/^\r?\n/)) {
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName},`;
} else {
toInsert = `, ${symbolName},`;
}
}
return [new InsertChange(componentPath, position, toInsert)];
}
export function findFullImports(importName: string, source: ts.SourceFile):
(ts.ImportDeclaration | ts.ImportSpecifier | RemoveContent)[] {
const allImports = collectDeepNodes<ts.ImportDeclaration>(source, ts.SyntaxKind.ImportDeclaration);
return allImports
// Filter out import statements that are either `import 'XYZ'` or `import * as X from 'XYZ'`.
.filter(({ importClause: clause }) =>
clause && !clause.name && clause.namedBindings &&
clause.namedBindings.kind === ts.SyntaxKind.NamedImports
)
.reduce((
imports: (ts.ImportDeclaration | ts.ImportSpecifier | RemoveContent)[],
importDecl: ts.ImportDeclaration
) => {
const importClause = importDecl.importClause as ts.ImportClause;
const namedImports = importClause.namedBindings as ts.NamedImports;
namedImports.elements.forEach((importSpec: ts.ImportSpecifier) => {
const importId = importSpec.name;
if (!(importId.text === importName)) {
return;
}
if (namedImports.elements.length === 1) {
imports.push(importDecl);
} else {
const toRemove = normalizeNodeToRemove<ts.ImportSpecifier>(importSpec, source);
imports.push(toRemove);
}
});
return imports;
}, []);
}
export function findMetadataValueInArray(source: ts.SourceFile, property: string, value: string):
(ts.Node | RemoveContent)[] {
const decorators = collectDeepNodes<ts.Decorator>(source, ts.SyntaxKind.Decorator)
const valuesNode = decorators
.reduce(
(nodes, decorator) => [
...nodes,
...collectDeepNodes<ts.PropertyAssignment>(decorator, ts.SyntaxKind.PropertyAssignment)
], [])
.find(assignment => {
let isValueForProperty = false;
ts.forEachChild(assignment, (child: ts.Node) => {
if (child.kind === ts.SyntaxKind.Identifier && child.getText() === property) {
isValueForProperty = true;
}
});
return isValueForProperty;
});
if (!valuesNode) {
return [];
}
let arrayLiteral;
ts.forEachChild(valuesNode, (child: ts.Node) => {
if (child.kind === ts.SyntaxKind.ArrayLiteralExpression) {
arrayLiteral = child;
}
});
if (!arrayLiteral) {
return [];
}
const values: (ts.Node | RemoveContent)[] = [];
ts.forEachChild(arrayLiteral, (child: ts.Node) => {
if (child.getText() === value) {
const toRemove = normalizeNodeToRemove(child, source);
values.push(toRemove);
}
});
return values;
}
/**
*
* @param node The node that should be remove
* @param source The source file that we are removing from
* This method ensures that if there's a comma before or after the node,
* it will be removed, too.
*/
function normalizeNodeToRemove<T extends ts.Node>(node: T, source: ts.SourceFile)
: (T | RemoveContent) {
const content = source.getText();
const start = node.getFullStart();
const end = node.getEnd();
const symbolBefore = content.substring(start - 1, start);
const symbolAfter = content.substring(end, end + 1);
if (symbolBefore === ",") {
return new RemoveContent(start - 1, end);
} else if (symbolAfter === ",") {
return new RemoveContent(start, end + 1);
} else {
return node;
}
}
export function addBootstrapToNgModule(modulePath: string, rootComponentName: string): Rule {
return (host: Tree) => {
const content = host.read(modulePath);
if (!content) {
throw new SchematicsException(`File ${modulePath} does not exist.`);
}
const sourceText = content.toString('utf-8');
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
const componentModule = `./${rootComponentName}.component`;
const rootComponentClassName = toComponentClassName(rootComponentName);
const importChanges = addImportToModule(source,
modulePath,
'NativeScriptModule',
'nativescript-angular/nativescript.module');
const bootstrapChanges = addBootstrapToModule(source,
modulePath,
rootComponentClassName,
componentModule);
const declarationChanges = addSymbolToNgModuleMetadata(
source,
modulePath,
'declarations',
rootComponentClassName,
);
const changes = [
...importChanges,
...bootstrapChanges,
...declarationChanges,
];
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T[] {
const nodes: T[] = [];
const helper = (child: ts.Node) => {
if (child.kind === kind) {
nodes.push(child as T);
}
ts.forEachChild(child, helper);
};
ts.forEachChild(node, helper);
return nodes;
}