1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
12 | }) : (function(o, m, k, k2) {
|
13 | if (k2 === undefined) k2 = k;
|
14 | o[k2] = m[k];
|
15 | }));
|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
18 | }) : function(o, v) {
|
19 | o["default"] = v;
|
20 | });
|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
22 | if (mod && mod.__esModule) return mod;
|
23 | var result = {};
|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
25 | __setModuleDefault(result, mod);
|
26 | return result;
|
27 | };
|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
29 | exports.addRouteDeclarationToModule = exports.getRouterModuleDeclaration = exports.getEnvironmentExportName = exports.isImported = exports.addBootstrapToModule = exports.addExportToModule = exports.addProviderToModule = exports.addImportToModule = exports.addDeclarationToModule = exports.addSymbolToNgModuleMetadata = exports.getMetadataField = exports.getDecoratorMetadata = exports.insertAfterLastOccurrence = exports.findNode = exports.getSourceNodes = exports.findNodes = exports.insertImport = void 0;
|
30 | const core_1 = require("@angular-devkit/core");
|
31 | const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
32 | const change_1 = require("./change");
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function insertImport(source, fileToEdit, symbolName, fileName, isDefault = false) {
|
43 | const rootNode = source;
|
44 | const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);
|
45 |
|
46 | const relevantImports = allImports.filter((node) => {
|
47 |
|
48 | const importFiles = node
|
49 | .getChildren()
|
50 | .filter(ts.isStringLiteral)
|
51 | .map((n) => n.text);
|
52 | return importFiles.filter((file) => file === fileName).length === 1;
|
53 | });
|
54 | if (relevantImports.length > 0) {
|
55 | let importsAsterisk = false;
|
56 |
|
57 | const imports = [];
|
58 | relevantImports.forEach((n) => {
|
59 | Array.prototype.push.apply(imports, findNodes(n, ts.SyntaxKind.Identifier));
|
60 | if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
|
61 | importsAsterisk = true;
|
62 | }
|
63 | });
|
64 |
|
65 | if (importsAsterisk) {
|
66 | return new change_1.NoopChange();
|
67 | }
|
68 | const importTextNodes = imports.filter((n) => n.text === symbolName);
|
69 |
|
70 | if (importTextNodes.length === 0) {
|
71 | const fallbackPos = findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
|
72 | findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
|
73 | return insertAfterLastOccurrence(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
|
74 | }
|
75 | return new change_1.NoopChange();
|
76 | }
|
77 |
|
78 | const useStrict = findNodes(rootNode, ts.isStringLiteral).filter((n) => n.text === 'use strict');
|
79 | let fallbackPos = 0;
|
80 | if (useStrict.length > 0) {
|
81 | fallbackPos = useStrict[0].end;
|
82 | }
|
83 | const open = isDefault ? '' : '{ ';
|
84 | const close = isDefault ? '' : ' }';
|
85 |
|
86 | const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
|
87 | const separator = insertAtBeginning ? '' : ';\n';
|
88 | const toInsert = `${separator}import ${open}${symbolName}${close}` +
|
89 | ` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
|
90 | return insertAfterLastOccurrence(allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral);
|
91 | }
|
92 | exports.insertImport = insertImport;
|
93 | function findNodes(node, kindOrGuard, max = Infinity, recursive = false) {
|
94 | if (!node || max == 0) {
|
95 | return [];
|
96 | }
|
97 | const test = typeof kindOrGuard === 'function'
|
98 | ? kindOrGuard
|
99 | : (node) => node.kind === kindOrGuard;
|
100 | const arr = [];
|
101 | if (test(node)) {
|
102 | arr.push(node);
|
103 | max--;
|
104 | }
|
105 | if (max > 0 && (recursive || !test(node))) {
|
106 | for (const child of node.getChildren()) {
|
107 | findNodes(child, test, max, recursive).forEach((node) => {
|
108 | if (max > 0) {
|
109 | arr.push(node);
|
110 | }
|
111 | max--;
|
112 | });
|
113 | if (max <= 0) {
|
114 | break;
|
115 | }
|
116 | }
|
117 | }
|
118 | return arr;
|
119 | }
|
120 | exports.findNodes = findNodes;
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function getSourceNodes(sourceFile) {
|
127 | const nodes = [sourceFile];
|
128 | const result = [];
|
129 | while (nodes.length > 0) {
|
130 | const node = nodes.shift();
|
131 | if (node) {
|
132 | result.push(node);
|
133 | if (node.getChildCount(sourceFile) >= 0) {
|
134 | nodes.unshift(...node.getChildren());
|
135 | }
|
136 | }
|
137 | }
|
138 | return result;
|
139 | }
|
140 | exports.getSourceNodes = getSourceNodes;
|
141 | function findNode(node, kind, text) {
|
142 | if (node.kind === kind && node.getText() === text) {
|
143 |
|
144 | return node;
|
145 | }
|
146 | let foundNode = null;
|
147 | ts.forEachChild(node, (childNode) => {
|
148 | foundNode = foundNode || findNode(childNode, kind, text);
|
149 | });
|
150 | return foundNode;
|
151 | }
|
152 | exports.findNode = findNode;
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | function nodesByPosition(first, second) {
|
158 | return first.getStart() - second.getStart();
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | function insertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind) {
|
174 | let lastItem;
|
175 | for (const node of nodes) {
|
176 | if (!lastItem || lastItem.getStart() < node.getStart()) {
|
177 | lastItem = node;
|
178 | }
|
179 | }
|
180 | if (syntaxKind && lastItem) {
|
181 | lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
|
182 | }
|
183 | if (!lastItem && fallbackPos == undefined) {
|
184 | throw new Error(`tried to insert ${toInsert} as first occurence with no fallback position`);
|
185 | }
|
186 | const lastItemPosition = lastItem ? lastItem.getEnd() : fallbackPos;
|
187 | return new change_1.InsertChange(file, lastItemPosition, toInsert);
|
188 | }
|
189 | exports.insertAfterLastOccurrence = insertAfterLastOccurrence;
|
190 | function _angularImportsFromNode(node) {
|
191 | const ms = node.moduleSpecifier;
|
192 | let modulePath;
|
193 | switch (ms.kind) {
|
194 | case ts.SyntaxKind.StringLiteral:
|
195 | modulePath = ms.text;
|
196 | break;
|
197 | default:
|
198 | return {};
|
199 | }
|
200 | if (!modulePath.startsWith('@angular/')) {
|
201 | return {};
|
202 | }
|
203 | if (node.importClause) {
|
204 | if (node.importClause.name) {
|
205 |
|
206 | return {};
|
207 | }
|
208 | else if (node.importClause.namedBindings) {
|
209 | const nb = node.importClause.namedBindings;
|
210 | if (nb.kind == ts.SyntaxKind.NamespaceImport) {
|
211 |
|
212 | return {
|
213 | [nb.name.text + '.']: modulePath,
|
214 | };
|
215 | }
|
216 | else {
|
217 |
|
218 | const namedImports = nb;
|
219 | return namedImports.elements
|
220 | .map((is) => (is.propertyName ? is.propertyName.text : is.name.text))
|
221 | .reduce((acc, curr) => {
|
222 | acc[curr] = modulePath;
|
223 | return acc;
|
224 | }, {});
|
225 | }
|
226 | }
|
227 | return {};
|
228 | }
|
229 | else {
|
230 |
|
231 | return {};
|
232 | }
|
233 | }
|
234 | function getDecoratorMetadata(source, identifier, module) {
|
235 | const angularImports = findNodes(source, ts.isImportDeclaration)
|
236 | .map((node) => _angularImportsFromNode(node))
|
237 | .reduce((acc, current) => {
|
238 | for (const key of Object.keys(current)) {
|
239 | acc[key] = current[key];
|
240 | }
|
241 | return acc;
|
242 | }, {});
|
243 | return getSourceNodes(source)
|
244 | .filter((node) => {
|
245 | return (node.kind == ts.SyntaxKind.Decorator &&
|
246 | node.expression.kind == ts.SyntaxKind.CallExpression);
|
247 | })
|
248 | .map((node) => node.expression)
|
249 | .filter((expr) => {
|
250 | if (expr.expression.kind == ts.SyntaxKind.Identifier) {
|
251 | const id = expr.expression;
|
252 | return id.text == identifier && angularImports[id.text] === module;
|
253 | }
|
254 | else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
|
255 |
|
256 | const paExpr = expr.expression;
|
257 |
|
258 | if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
|
259 | return false;
|
260 | }
|
261 | const id = paExpr.name.text;
|
262 | const moduleId = paExpr.expression.text;
|
263 | return id === identifier && angularImports[moduleId + '.'] === module;
|
264 | }
|
265 | return false;
|
266 | })
|
267 | .filter((expr) => expr.arguments[0] && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression)
|
268 | .map((expr) => expr.arguments[0]);
|
269 | }
|
270 | exports.getDecoratorMetadata = getDecoratorMetadata;
|
271 | function getMetadataField(node, metadataField) {
|
272 | return (node.properties
|
273 | .filter(ts.isPropertyAssignment)
|
274 |
|
275 |
|
276 | .filter(({ name }) => {
|
277 | return (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === metadataField;
|
278 | }));
|
279 | }
|
280 | exports.getMetadataField = getMetadataField;
|
281 | function addSymbolToNgModuleMetadata(source, ngModulePath, metadataField, symbolName, importPath = null) {
|
282 | const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
|
283 | let node = nodes[0];
|
284 |
|
285 | if (!node) {
|
286 | return [];
|
287 | }
|
288 |
|
289 | const matchingProperties = getMetadataField(node, metadataField);
|
290 | if (matchingProperties.length == 0) {
|
291 |
|
292 | const expr = node;
|
293 | let position;
|
294 | let toInsert;
|
295 | if (expr.properties.length == 0) {
|
296 | position = expr.getEnd() - 1;
|
297 | toInsert = `\n ${metadataField}: [\n${core_1.tags.indentBy(4) `${symbolName}`}\n ]\n`;
|
298 | }
|
299 | else {
|
300 | node = expr.properties[expr.properties.length - 1];
|
301 | position = node.getEnd();
|
302 |
|
303 | const text = node.getFullText(source);
|
304 | const matches = text.match(/^(\r?\n)(\s*)/);
|
305 | if (matches) {
|
306 | toInsert =
|
307 | `,${matches[0]}${metadataField}: [${matches[1]}` +
|
308 | `${core_1.tags.indentBy(matches[2].length + 2) `${symbolName}`}${matches[0]}]`;
|
309 | }
|
310 | else {
|
311 | toInsert = `, ${metadataField}: [${symbolName}]`;
|
312 | }
|
313 | }
|
314 | if (importPath !== null) {
|
315 | return [
|
316 | new change_1.InsertChange(ngModulePath, position, toInsert),
|
317 | insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
|
318 | ];
|
319 | }
|
320 | else {
|
321 | return [new change_1.InsertChange(ngModulePath, position, toInsert)];
|
322 | }
|
323 | }
|
324 | const assignment = matchingProperties[0];
|
325 |
|
326 | if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
|
327 | return [];
|
328 | }
|
329 | const arrLiteral = assignment.initializer;
|
330 | if (arrLiteral.elements.length == 0) {
|
331 |
|
332 | node = arrLiteral;
|
333 | }
|
334 | else {
|
335 | node = arrLiteral.elements;
|
336 | }
|
337 | if (Array.isArray(node)) {
|
338 | const nodeArray = node;
|
339 | const symbolsArray = nodeArray.map((node) => core_1.tags.oneLine `${node.getText()}`);
|
340 | if (symbolsArray.includes(core_1.tags.oneLine `${symbolName}`)) {
|
341 | return [];
|
342 | }
|
343 | node = node[node.length - 1];
|
344 | }
|
345 | let toInsert;
|
346 | let position = node.getEnd();
|
347 | if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
|
348 |
|
349 | position--;
|
350 | toInsert = `\n${core_1.tags.indentBy(4) `${symbolName}`}\n `;
|
351 | }
|
352 | else {
|
353 |
|
354 | const text = node.getFullText(source);
|
355 | const matches = text.match(/^(\r?\n)(\s*)/);
|
356 | if (matches) {
|
357 | toInsert = `,${matches[1]}${core_1.tags.indentBy(matches[2].length) `${symbolName}`}`;
|
358 | }
|
359 | else {
|
360 | toInsert = `, ${symbolName}`;
|
361 | }
|
362 | }
|
363 | if (importPath !== null) {
|
364 | return [
|
365 | new change_1.InsertChange(ngModulePath, position, toInsert),
|
366 | insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
|
367 | ];
|
368 | }
|
369 | return [new change_1.InsertChange(ngModulePath, position, toInsert)];
|
370 | }
|
371 | exports.addSymbolToNgModuleMetadata = addSymbolToNgModuleMetadata;
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | function addDeclarationToModule(source, modulePath, classifiedName, importPath) {
|
377 | return addSymbolToNgModuleMetadata(source, modulePath, 'declarations', classifiedName, importPath);
|
378 | }
|
379 | exports.addDeclarationToModule = addDeclarationToModule;
|
380 |
|
381 |
|
382 |
|
383 | function addImportToModule(source, modulePath, classifiedName, importPath) {
|
384 | return addSymbolToNgModuleMetadata(source, modulePath, 'imports', classifiedName, importPath);
|
385 | }
|
386 | exports.addImportToModule = addImportToModule;
|
387 |
|
388 |
|
389 |
|
390 | function addProviderToModule(source, modulePath, classifiedName, importPath) {
|
391 | return addSymbolToNgModuleMetadata(source, modulePath, 'providers', classifiedName, importPath);
|
392 | }
|
393 | exports.addProviderToModule = addProviderToModule;
|
394 |
|
395 |
|
396 |
|
397 | function addExportToModule(source, modulePath, classifiedName, importPath) {
|
398 | return addSymbolToNgModuleMetadata(source, modulePath, 'exports', classifiedName, importPath);
|
399 | }
|
400 | exports.addExportToModule = addExportToModule;
|
401 |
|
402 |
|
403 |
|
404 | function addBootstrapToModule(source, modulePath, classifiedName, importPath) {
|
405 | return addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
|
406 | }
|
407 | exports.addBootstrapToModule = addBootstrapToModule;
|
408 |
|
409 |
|
410 |
|
411 | function isImported(source, classifiedName, importPath) {
|
412 | const allNodes = getSourceNodes(source);
|
413 | const matchingNodes = allNodes
|
414 | .filter(ts.isImportDeclaration)
|
415 | .filter((imp) => ts.isStringLiteral(imp.moduleSpecifier) && imp.moduleSpecifier.text === importPath)
|
416 | .filter((imp) => {
|
417 | if (!imp.importClause) {
|
418 | return false;
|
419 | }
|
420 | const nodes = findNodes(imp.importClause, ts.isImportSpecifier).filter((n) => n.getText() === classifiedName);
|
421 | return nodes.length > 0;
|
422 | });
|
423 | return matchingNodes.length > 0;
|
424 | }
|
425 | exports.isImported = isImported;
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | function getEnvironmentExportName(source) {
|
432 |
|
433 |
|
434 | let environmentExportName = null;
|
435 | const allNodes = getSourceNodes(source);
|
436 | allNodes
|
437 | .filter(ts.isImportDeclaration)
|
438 | .filter((declaration) => declaration.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral &&
|
439 | declaration.importClause !== undefined)
|
440 | .map((declaration) =>
|
441 |
|
442 |
|
443 | declaration.importClause.getChildAt(0))
|
444 |
|
445 |
|
446 | .filter(ts.isNamedImports)
|
447 | .filter((namedImports) => namedImports.getText().includes('environment'))
|
448 | .forEach((namedImports) => {
|
449 | for (const specifier of namedImports.elements) {
|
450 |
|
451 |
|
452 | const name = specifier.propertyName || specifier.name;
|
453 |
|
454 |
|
455 | if (name.text.includes('environment')) {
|
456 | environmentExportName = specifier.name.text;
|
457 | }
|
458 | }
|
459 | });
|
460 | return environmentExportName;
|
461 | }
|
462 | exports.getEnvironmentExportName = getEnvironmentExportName;
|
463 |
|
464 |
|
465 |
|
466 | function getRouterModuleDeclaration(source) {
|
467 | const result = getDecoratorMetadata(source, 'NgModule', '@angular/core');
|
468 | const node = result[0];
|
469 | const matchingProperties = getMetadataField(node, 'imports');
|
470 | if (!matchingProperties) {
|
471 | return;
|
472 | }
|
473 | const assignment = matchingProperties[0];
|
474 | if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
|
475 | return;
|
476 | }
|
477 | const arrLiteral = assignment.initializer;
|
478 | return arrLiteral.elements
|
479 | .filter((el) => el.kind === ts.SyntaxKind.CallExpression)
|
480 | .find((el) => el.getText().startsWith('RouterModule'));
|
481 | }
|
482 | exports.getRouterModuleDeclaration = getRouterModuleDeclaration;
|
483 |
|
484 |
|
485 |
|
486 | function addRouteDeclarationToModule(source, fileToAdd, routeLiteral) {
|
487 | const routerModuleExpr = getRouterModuleDeclaration(source);
|
488 | if (!routerModuleExpr) {
|
489 | throw new Error(`Couldn't find a route declaration in ${fileToAdd}.`);
|
490 | }
|
491 | const scopeConfigMethodArgs = routerModuleExpr.arguments;
|
492 | if (!scopeConfigMethodArgs.length) {
|
493 | const { line } = source.getLineAndCharacterOfPosition(routerModuleExpr.getStart());
|
494 | throw new Error(`The router module method doesn't have arguments ` + `at line ${line} in ${fileToAdd}`);
|
495 | }
|
496 | let routesArr;
|
497 | const routesArg = scopeConfigMethodArgs[0];
|
498 |
|
499 |
|
500 | if (ts.isArrayLiteralExpression(routesArg)) {
|
501 | routesArr = routesArg;
|
502 | }
|
503 | else {
|
504 | const routesVarName = routesArg.getText();
|
505 | let routesVar;
|
506 | if (routesArg.kind === ts.SyntaxKind.Identifier) {
|
507 | routesVar = source.statements.filter(ts.isVariableStatement).find((v) => {
|
508 | return v.declarationList.declarations[0].name.getText() === routesVarName;
|
509 | });
|
510 | }
|
511 | if (!routesVar) {
|
512 | const { line } = source.getLineAndCharacterOfPosition(routesArg.getStart());
|
513 | throw new Error(`No route declaration array was found that corresponds ` +
|
514 | `to router module at line ${line} in ${fileToAdd}`);
|
515 | }
|
516 | routesArr = findNodes(routesVar, ts.SyntaxKind.ArrayLiteralExpression, 1)[0];
|
517 | }
|
518 | const occurrencesCount = routesArr.elements.length;
|
519 | const text = routesArr.getFullText(source);
|
520 | let route = routeLiteral;
|
521 | let insertPos = routesArr.elements.pos;
|
522 | if (occurrencesCount > 0) {
|
523 | const lastRouteLiteral = [...routesArr.elements].pop();
|
524 | const lastRouteIsWildcard = ts.isObjectLiteralExpression(lastRouteLiteral) &&
|
525 | lastRouteLiteral.properties.some((n) => ts.isPropertyAssignment(n) &&
|
526 | ts.isIdentifier(n.name) &&
|
527 | n.name.text === 'path' &&
|
528 | ts.isStringLiteral(n.initializer) &&
|
529 | n.initializer.text === '**');
|
530 | const indentation = text.match(/\r?\n(\r?)\s*/) || [];
|
531 | const routeText = `${indentation[0] || ' '}${routeLiteral}`;
|
532 |
|
533 |
|
534 | if (lastRouteIsWildcard) {
|
535 | insertPos = lastRouteLiteral.pos;
|
536 | route = `${routeText},`;
|
537 | }
|
538 | else {
|
539 | insertPos = lastRouteLiteral.end;
|
540 | route = `,${routeText}`;
|
541 | }
|
542 | }
|
543 | return new change_1.InsertChange(fileToAdd, insertPos, route);
|
544 | }
|
545 | exports.addRouteDeclarationToModule = addRouteDeclarationToModule;
|