/* istanbul ignore file */ /** * @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 * as ts from 'typescript'; import { Change, InsertChange, NoopChange, createReplaceChange, ReplaceChange, RemoveChange, createRemoveChange, } from './change'; import { Path } from '@angular-devkit/core'; /** * Find all nodes from the AST in the subtree of node of SyntaxKind kind. * @param node * @param kind * @param max The maximum number of items to return. * @return all nodes of kind, or [] if none is found */ export function findNodes( node: ts.Node, kind: ts.SyntaxKind, max = Infinity ): ts.Node[] { if (!node || max == 0) { return []; } const arr: ts.Node[] = []; if (node.kind === kind) { arr.push(node); max--; } if (max > 0) { for (const child of node.getChildren()) { findNodes(child, kind, max).forEach((node) => { if (max > 0) { arr.push(node); } max--; }); if (max <= 0) { break; } } } return arr; } /** * Get all the nodes from a source. * @param sourceFile The source file object. * @returns {Observable} An observable of all the nodes in the source. */ export function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[] { const nodes: ts.Node[] = [sourceFile]; const result = []; while (nodes.length > 0) { const node = nodes.shift(); if (node) { result.push(node); if (node.getChildCount(sourceFile) >= 0) { nodes.unshift(...node.getChildren()); } } } return result; } /** * Helper for sorting nodes. * @return function to sort nodes in increasing order of position in sourceFile */ function nodesByPosition(first: ts.Node, second: ts.Node): number { return first.pos - second.pos; } /** * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]` * or after the last of occurence of `syntaxKind` if the last occurence is a sub child * of ts.SyntaxKind[nodes[i].kind] and save the changes in file. * * @param nodes insert after the last occurence of nodes * @param toInsert string to insert * @param file file to insert changes into * @param fallbackPos position to insert if toInsert happens to be the first occurence * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after * @return Change instance * @throw Error if toInsert is first occurence but fall back is not set */ export function insertAfterLastOccurrence( nodes: ts.Node[], toInsert: string, file: string, fallbackPos: number, syntaxKind?: ts.SyntaxKind ): Change { let lastItem = nodes.sort(nodesByPosition).pop(); if (!lastItem) { throw new Error(); } if (syntaxKind) { lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop(); } if (!lastItem && fallbackPos == undefined) { throw new Error( `tried to insert ${toInsert} as first occurence with no fallback position` ); } const lastItemPosition: number = lastItem ? lastItem.end : fallbackPos; return new InsertChange(file, lastItemPosition, toInsert); } export function getContentOfKeyLiteral( _source: ts.SourceFile, node: ts.Node ): string | null { if (node.kind == ts.SyntaxKind.Identifier) { return (node as ts.Identifier).text; } else if (node.kind == ts.SyntaxKind.StringLiteral) { return (node as ts.StringLiteral).text; } else { return null; } } function _angularImportsFromNode( node: ts.ImportDeclaration, _sourceFile: ts.SourceFile ): { [name: string]: string } { const ms = node.moduleSpecifier; let modulePath: string; switch (ms.kind) { case ts.SyntaxKind.StringLiteral: modulePath = (ms as ts.StringLiteral).text; break; default: return {}; } if (!modulePath.startsWith('@angular/')) { return {}; } if (node.importClause) { if (node.importClause.name) { // This is of the form `import Name from 'path'`. Ignore. return {}; } else if (node.importClause.namedBindings) { const nb = node.importClause.namedBindings; if (nb.kind == ts.SyntaxKind.NamespaceImport) { // This is of the form `import * as name from 'path'`. Return `name.`. return { [(nb as ts.NamespaceImport).name.text + '.']: modulePath, }; } else { // This is of the form `import {a,b,c} from 'path'` const namedImports = nb as ts.NamedImports; return namedImports.elements .map((is: ts.ImportSpecifier) => is.propertyName ? is.propertyName.text : is.name.text ) .reduce((acc: { [name: string]: string }, curr: string) => { acc[curr] = modulePath; return acc; }, {}); } } return {}; } else { // This is of the form `import 'path';`. Nothing to do. return {}; } } export function getDecoratorMetadata( source: ts.SourceFile, identifier: string, module: string ): ts.Node[] { const angularImports: { [name: string]: string } = findNodes( source, ts.SyntaxKind.ImportDeclaration ) .map((node) => _angularImportsFromNode(node as ts.ImportDeclaration, source) ) .reduce( ( acc: { [name: string]: string }, current: { [name: string]: string } ) => { for (const key of Object.keys(current)) { acc[key] = current[key]; } return acc; }, {} ); return getSourceNodes(source) .filter((node) => { return ( node.kind == ts.SyntaxKind.Decorator && (node as ts.Decorator).expression.kind == ts.SyntaxKind.CallExpression ); }) .map((node) => (node as ts.Decorator).expression as ts.CallExpression) .filter((expr) => { if (expr.expression.kind == ts.SyntaxKind.Identifier) { const id = expr.expression as ts.Identifier; return ( id.getFullText(source) == identifier && angularImports[id.getFullText(source)] === module ); } else if ( expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression ) { // This covers foo.NgModule when importing * as foo. const paExpr = expr.expression as ts.PropertyAccessExpression; // If the left expression is not an identifier, just give up at that point. if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) { return false; } const id = paExpr.name.text; const moduleId = (paExpr.expression as ts.Identifier).getText(source); return id === identifier && angularImports[moduleId + '.'] === module; } return false; }) .filter( (expr) => expr.arguments[0] && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression ) .map((expr) => expr.arguments[0] as ts.ObjectLiteralExpression); } function _addSymbolToNgModuleMetadata( source: ts.SourceFile, ngModulePath: string, metadataField: string, symbolName: string, importPath: string ): Change[] { const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core'); let node: any = nodes[0]; // eslint-disable-line @typescript-eslint/no-explicit-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: any) => { 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}]`; } } const newMetadataProperty = new InsertChange( ngModulePath, position, toInsert ); const newMetadataImport = insertImport( source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath ); return [newMetadataProperty, newMetadataImport]; } const assignment = matchingProperties[0] as ts.PropertyAssignment; // If it's not an array, nothing we can do really. 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; const symbolsArray = nodeArray.map((node) => node.getText()); if (symbolsArray.includes(symbolName)) { return []; } node = node[node.length - 1]; const effectsModule = nodeArray.find( (node) => (node.getText().includes('EffectsModule.forRoot') && symbolName.includes('EffectsModule.forRoot')) || (node.getText().includes('EffectsModule.forFeature') && symbolName.includes('EffectsModule.forFeature')) ); if (effectsModule && symbolName.includes('EffectsModule')) { const effectsArgs = (effectsModule as any).arguments.shift(); if ( effectsArgs && effectsArgs.kind === ts.SyntaxKind.ArrayLiteralExpression ) { const effectsElements = (effectsArgs as ts.ArrayLiteralExpression) .elements; const [, effectsSymbol] = (symbolName).match(/\[(.*)\]/); let epos; if (effectsElements.length === 0) { epos = effectsArgs.getStart() + 1; return [new InsertChange(ngModulePath, epos, effectsSymbol)]; } else { const lastEffect = effectsElements[ effectsElements.length - 1 ] as ts.Expression; epos = lastEffect.getEnd(); // Get the indentation of the last element, if any. const text: any = lastEffect.getFullText(source); let effectInsert: string; if (text.match('^\r?\r?\n')) { effectInsert = `,${text.match(/^\r?\n\s+/)[0]}${effectsSymbol}`; } else { effectInsert = `, ${effectsSymbol}`; } return [new InsertChange(ngModulePath, epos, effectInsert)]; } } else { return []; } } } 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}`; } } const insert = new InsertChange(ngModulePath, position, toInsert); const importInsert: Change = insertImport( source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath ); return [insert, importInsert]; } function _addSymbolToComponentMetadata( source: ts.SourceFile, componentPath: string, metadataField: string, symbolName: string, importPath: string ): Change[] { const nodes = getDecoratorMetadata(source, 'Component', '@angular/core'); let node: any = nodes[0]; // eslint-disable-line @typescript-eslint/no-explicit-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: any) => { 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}]`; } } const newMetadataProperty = new InsertChange( componentPath, position, toInsert ); const newMetadataImport = insertImport( source, componentPath, symbolName.replace(/\..*$/, ''), importPath ); return [newMetadataProperty, newMetadataImport]; } const assignment = matchingProperties[0] as ts.PropertyAssignment; // If it's not an array, nothing we can do really. 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 component found. Please add your new class to your component.' ); return []; } if (Array.isArray(node)) { const nodeArray = node as {} as Array; const symbolsArray = nodeArray.map((node) => node.getText()); if (symbolsArray.includes(symbolName)) { 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}`; } } const insert = new InsertChange(componentPath, position, toInsert); const importInsert: Change = insertImport( source, componentPath, symbolName.replace(/\..*$/, ''), importPath ); return [insert, importInsert]; } /** * Custom function to insert a declaration (component, pipe, directive) * into NgModule declarations. It also imports the component. */ export function addDeclarationToModule( source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToNgModuleMetadata( source, modulePath, 'declarations', classifiedName, importPath ); } /** * Custom function to insert a declaration (component, pipe, directive) * into NgModule declarations. It also imports the component. */ export function addImportToModule( source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToNgModuleMetadata( source, modulePath, 'imports', classifiedName, importPath ); } /** * Custom function to insert a provider into NgModule. It also imports it. */ export function addProviderToModule( source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToNgModuleMetadata( source, modulePath, 'providers', classifiedName, importPath ); } /** * Custom function to insert a provider into Component. It also imports it. */ export function addProviderToComponent( source: ts.SourceFile, componentPath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToComponentMetadata( source, componentPath, 'providers', classifiedName, importPath ); } /** * Custom function to insert an export into NgModule. It also imports it. */ export function addExportToModule( source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToNgModuleMetadata( source, modulePath, 'exports', classifiedName, importPath ); } /** * Custom function to insert an export into NgModule. It also imports it. */ export function addBootstrapToModule( source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string ): Change[] { return _addSymbolToNgModuleMetadata( source, modulePath, 'bootstrap', classifiedName, importPath ); } /** * Add Import `import { symbolName } from fileName` if the import doesn't exit * already. Assumes fileToEdit can be resolved and accessed. * @param fileToEdit (file we want to add import to) * @param symbolName (item to import) * @param fileName (path to the file) * @param isDefault (if true, import follows style for importing default exports) * @return Change */ export function insertImport( source: ts.SourceFile, fileToEdit: string, symbolName: string, fileName: string, isDefault = false ): Change { const rootNode = source; const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration); // get nodes that map to import statements from the file fileName const relevantImports = allImports.filter((node) => { // StringLiteral of the ImportDeclaration is the import file (fileName in this case). const importFiles = node .getChildren() .filter((child) => child.kind === ts.SyntaxKind.StringLiteral) .map((n) => (n as ts.StringLiteral).text); return importFiles.filter((file) => file === fileName).length === 1; }); if (relevantImports.length > 0) { let importsAsterisk = false; // imports from import file const imports: ts.Node[] = []; relevantImports.forEach((n) => { Array.prototype.push.apply( imports, findNodes(n, ts.SyntaxKind.Identifier) ); if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) { importsAsterisk = true; } }); // if imports * from fileName, don't add symbolName if (importsAsterisk) { return new NoopChange(); } const importTextNodes = imports.filter( (n) => (n as ts.Identifier).text === symbolName ); // insert import if it's not there if (importTextNodes.length === 0) { const fallbackPos = findNodes( relevantImports[0], ts.SyntaxKind.CloseBraceToken )[0].getStart() || findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart(); return insertAfterLastOccurrence( imports, `, ${symbolName}`, fileToEdit, fallbackPos ); } return new NoopChange(); } // no such import declaration exists const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter( (n) => n.getText() === 'use strict' ); let fallbackPos = 0; if (useStrict.length > 0) { fallbackPos = useStrict[0].end; } const open = isDefault ? '' : '{ '; const close = isDefault ? '' : ' }'; // if there are no imports or 'use strict' statement, insert import at beginning of file const insertAtBeginning = allImports.length === 0 && useStrict.length === 0; const separator = insertAtBeginning ? '' : ';\n'; const toInsert = `${separator}import ${open}${symbolName}${close}` + ` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`; return insertAfterLastOccurrence( allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral ); } export function replaceImport( sourceFile: ts.SourceFile, path: Path, importFrom: string, importAsIs: string, importToBe: string ): (ReplaceChange | RemoveChange)[] { const imports = sourceFile.statements .filter(ts.isImportDeclaration) .filter( ({ moduleSpecifier }) => moduleSpecifier.getText(sourceFile) === `'${importFrom}'` || moduleSpecifier.getText(sourceFile) === `"${importFrom}"` ); if (imports.length === 0) { return []; } const importText = (specifier: ts.ImportSpecifier) => { if (specifier.name.text) { return specifier.name.text; } // if import is renamed if (specifier.propertyName && specifier.propertyName.text) { return specifier.propertyName.text; } return ''; }; const changes = imports.map((p) => { const namedImports = p?.importClause?.namedBindings as ts.NamedImports; if (!namedImports) { return []; } const importSpecifiers = namedImports.elements; const isAlreadyImported = importSpecifiers .map(importText) .includes(importToBe); const importChanges = importSpecifiers.map((specifier, index) => { const text = importText(specifier); // import is not the one we're looking for, can be skipped if (text !== importAsIs) { return undefined; } // identifier has not been imported, simply replace the old text with the new text if (!isAlreadyImported) { return createReplaceChange( sourceFile, specifier, importAsIs, importToBe ); } const nextIdentifier = importSpecifiers[index + 1]; // identifer is not the last, also clean up the comma if (nextIdentifier) { return createRemoveChange( sourceFile, specifier, specifier.getStart(sourceFile), nextIdentifier.getStart(sourceFile) ); } // there are no imports following, just remove it return createRemoveChange( sourceFile, specifier, specifier.getStart(sourceFile), specifier.getEnd() ); }); return importChanges.filter(Boolean) as (ReplaceChange | RemoveChange)[]; }); return changes.reduce((imports, curr) => imports.concat(curr), []); } export function containsProperty( objectLiteral: ts.ObjectLiteralExpression, propertyName: string ) { return ( objectLiteral && objectLiteral.properties.some( (prop) => ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && prop.name.text === propertyName ) ); }