|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { SchematicsException, Tree } from '@angular-devkit/schematics'; |
| 10 | +import ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 11 | +import { insertImport } from '../utility/ast-utils'; |
| 12 | +import { InsertChange } from '../utility/change'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Checks whether the providers from a module are being imported in a `bootstrapApplication` call. |
| 16 | + * @param tree File tree of the project. |
| 17 | + * @param filePath Path of the file in which to check. |
| 18 | + * @param className Class name of the module to search for. |
| 19 | + */ |
| 20 | +export function importsProvidersFrom(tree: Tree, filePath: string, className: string): boolean { |
| 21 | + const sourceFile = ts.createSourceFile( |
| 22 | + filePath, |
| 23 | + tree.readText(filePath), |
| 24 | + ts.ScriptTarget.Latest, |
| 25 | + true, |
| 26 | + ); |
| 27 | + |
| 28 | + const bootstrapCall = findBootstrapApplicationCall(sourceFile); |
| 29 | + const importProvidersFromCall = bootstrapCall ? findImportProvidersFromCall(bootstrapCall) : null; |
| 30 | + |
| 31 | + return ( |
| 32 | + !!importProvidersFromCall && |
| 33 | + importProvidersFromCall.arguments.some((arg) => ts.isIdentifier(arg) && arg.text === className) |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Adds an `importProvidersFrom` call to the `bootstrapApplication` call. |
| 39 | + * @param tree File tree of the project. |
| 40 | + * @param filePath Path to the file that should be updated. |
| 41 | + * @param moduleName Name of the module that should be imported. |
| 42 | + * @param modulePath Path from which to import the module. |
| 43 | + */ |
| 44 | +export function addModuleImportToStandaloneBootstrap( |
| 45 | + tree: Tree, |
| 46 | + filePath: string, |
| 47 | + moduleName: string, |
| 48 | + modulePath: string, |
| 49 | +) { |
| 50 | + const sourceFile = ts.createSourceFile( |
| 51 | + filePath, |
| 52 | + tree.readText(filePath), |
| 53 | + ts.ScriptTarget.Latest, |
| 54 | + true, |
| 55 | + ); |
| 56 | + |
| 57 | + const bootstrapCall = findBootstrapApplicationCall(sourceFile); |
| 58 | + |
| 59 | + if (!bootstrapCall) { |
| 60 | + throw new SchematicsException(`Could not find bootstrapApplication call in ${filePath}`); |
| 61 | + } |
| 62 | + |
| 63 | + const recorder = tree.beginUpdate(filePath); |
| 64 | + const importCall = findImportProvidersFromCall(bootstrapCall); |
| 65 | + const printer = ts.createPrinter(); |
| 66 | + const sourceText = sourceFile.getText(); |
| 67 | + |
| 68 | + // Add imports to the module being added and `importProvidersFrom`. We don't |
| 69 | + // have to worry about duplicates, because `insertImport` handles them. |
| 70 | + [ |
| 71 | + insertImport(sourceFile, sourceText, moduleName, modulePath), |
| 72 | + insertImport(sourceFile, sourceText, 'importProvidersFrom', '@angular/core'), |
| 73 | + ].forEach((change) => { |
| 74 | + if (change instanceof InsertChange) { |
| 75 | + recorder.insertLeft(change.pos, change.toAdd); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + // If there is an `importProvidersFrom` call already, reuse it. |
| 80 | + if (importCall) { |
| 81 | + recorder.insertRight( |
| 82 | + importCall.arguments[importCall.arguments.length - 1].getEnd(), |
| 83 | + `, ${moduleName}`, |
| 84 | + ); |
| 85 | + } else if (bootstrapCall.arguments.length === 1) { |
| 86 | + // Otherwise if there is no options parameter to `bootstrapApplication`, |
| 87 | + // create an object literal with a `providers` array and the import. |
| 88 | + const newCall = ts.factory.updateCallExpression( |
| 89 | + bootstrapCall, |
| 90 | + bootstrapCall.expression, |
| 91 | + bootstrapCall.typeArguments, |
| 92 | + [ |
| 93 | + ...bootstrapCall.arguments, |
| 94 | + ts.factory.createObjectLiteralExpression([createProvidersAssignment(moduleName)], true), |
| 95 | + ], |
| 96 | + ); |
| 97 | + |
| 98 | + recorder.remove(bootstrapCall.getStart(), bootstrapCall.getWidth()); |
| 99 | + recorder.insertRight( |
| 100 | + bootstrapCall.getStart(), |
| 101 | + printer.printNode(ts.EmitHint.Unspecified, newCall, sourceFile), |
| 102 | + ); |
| 103 | + } else { |
| 104 | + const providersLiteral = findProvidersLiteral(bootstrapCall); |
| 105 | + |
| 106 | + if (providersLiteral) { |
| 107 | + // If there's a `providers` array, add the import to it. |
| 108 | + const newProvidersLiteral = ts.factory.updateArrayLiteralExpression(providersLiteral, [ |
| 109 | + ...providersLiteral.elements, |
| 110 | + createImportProvidersFromCall(moduleName), |
| 111 | + ]); |
| 112 | + recorder.remove(providersLiteral.getStart(), providersLiteral.getWidth()); |
| 113 | + recorder.insertRight( |
| 114 | + providersLiteral.getStart(), |
| 115 | + printer.printNode(ts.EmitHint.Unspecified, newProvidersLiteral, sourceFile), |
| 116 | + ); |
| 117 | + } else { |
| 118 | + // Otherwise add a `providers` array to the existing object literal. |
| 119 | + const optionsLiteral = bootstrapCall.arguments[1] as ts.ObjectLiteralExpression; |
| 120 | + const newOptionsLiteral = ts.factory.updateObjectLiteralExpression(optionsLiteral, [ |
| 121 | + ...optionsLiteral.properties, |
| 122 | + createProvidersAssignment(moduleName), |
| 123 | + ]); |
| 124 | + recorder.remove(optionsLiteral.getStart(), optionsLiteral.getWidth()); |
| 125 | + recorder.insertRight( |
| 126 | + optionsLiteral.getStart(), |
| 127 | + printer.printNode(ts.EmitHint.Unspecified, newOptionsLiteral, sourceFile), |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + tree.commitUpdate(recorder); |
| 133 | +} |
| 134 | + |
| 135 | +/** Finds the call to `bootstrapApplication` within a file. */ |
| 136 | +export function findBootstrapApplicationCall(sourceFile: ts.SourceFile): ts.CallExpression | null { |
| 137 | + const localName = findImportLocalName( |
| 138 | + sourceFile, |
| 139 | + 'bootstrapApplication', |
| 140 | + '@angular/platform-browser', |
| 141 | + ); |
| 142 | + |
| 143 | + return localName ? findCall(sourceFile, localName) : null; |
| 144 | +} |
| 145 | + |
| 146 | +/** Find a call to `importProvidersFrom` within a `bootstrapApplication` call. */ |
| 147 | +function findImportProvidersFromCall(bootstrapCall: ts.CallExpression): ts.CallExpression | null { |
| 148 | + const providersLiteral = findProvidersLiteral(bootstrapCall); |
| 149 | + const importProvidersName = findImportLocalName( |
| 150 | + bootstrapCall.getSourceFile(), |
| 151 | + 'importProvidersFrom', |
| 152 | + '@angular/core', |
| 153 | + ); |
| 154 | + |
| 155 | + if (providersLiteral && importProvidersName) { |
| 156 | + for (const element of providersLiteral.elements) { |
| 157 | + // Look for an array element that calls the `importProvidersFrom` function. |
| 158 | + if ( |
| 159 | + ts.isCallExpression(element) && |
| 160 | + ts.isIdentifier(element.expression) && |
| 161 | + element.expression.text === importProvidersName |
| 162 | + ) { |
| 163 | + return element; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return null; |
| 169 | +} |
| 170 | + |
| 171 | +/** Finds the `providers` array literal within a `bootstrapApplication` call. */ |
| 172 | +function findProvidersLiteral(bootstrapCall: ts.CallExpression): ts.ArrayLiteralExpression | null { |
| 173 | + // The imports have to be in the second argument of |
| 174 | + // the function which has to be an object literal. |
| 175 | + if ( |
| 176 | + bootstrapCall.arguments.length > 1 && |
| 177 | + ts.isObjectLiteralExpression(bootstrapCall.arguments[1]) |
| 178 | + ) { |
| 179 | + for (const prop of bootstrapCall.arguments[1].properties) { |
| 180 | + if ( |
| 181 | + ts.isPropertyAssignment(prop) && |
| 182 | + ts.isIdentifier(prop.name) && |
| 183 | + prop.name.text === 'providers' && |
| 184 | + ts.isArrayLiteralExpression(prop.initializer) |
| 185 | + ) { |
| 186 | + return prop.initializer; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return null; |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + * Finds the local name of an imported symbol. Could be the symbol name itself or its alias. |
| 196 | + * @param sourceFile File within which to search for the import. |
| 197 | + * @param name Actual name of the import, not its local alias. |
| 198 | + * @param moduleName Name of the module from which the symbol is imported. |
| 199 | + */ |
| 200 | +function findImportLocalName( |
| 201 | + sourceFile: ts.SourceFile, |
| 202 | + name: string, |
| 203 | + moduleName: string, |
| 204 | +): string | null { |
| 205 | + for (const node of sourceFile.statements) { |
| 206 | + // Only look for top-level imports. |
| 207 | + if ( |
| 208 | + !ts.isImportDeclaration(node) || |
| 209 | + !ts.isStringLiteral(node.moduleSpecifier) || |
| 210 | + node.moduleSpecifier.text !== moduleName |
| 211 | + ) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + |
| 215 | + // Filter out imports that don't have the right shape. |
| 216 | + if ( |
| 217 | + !node.importClause || |
| 218 | + !node.importClause.namedBindings || |
| 219 | + !ts.isNamedImports(node.importClause.namedBindings) |
| 220 | + ) { |
| 221 | + continue; |
| 222 | + } |
| 223 | + |
| 224 | + // Look through the elements of the declaration for the specific import. |
| 225 | + for (const element of node.importClause.namedBindings.elements) { |
| 226 | + if ((element.propertyName || element.name).text === name) { |
| 227 | + // The local name is always in `name`. |
| 228 | + return element.name.text; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return null; |
| 234 | +} |
| 235 | + |
| 236 | +/** |
| 237 | + * Finds a call to a function with a specific name. |
| 238 | + * @param rootNode Node from which to start searching. |
| 239 | + * @param name Name of the function to search for. |
| 240 | + */ |
| 241 | +function findCall(rootNode: ts.Node, name: string): ts.CallExpression | null { |
| 242 | + let result: ts.CallExpression | null = null; |
| 243 | + |
| 244 | + rootNode.forEachChild(function walk(node) { |
| 245 | + if ( |
| 246 | + ts.isCallExpression(node) && |
| 247 | + ts.isIdentifier(node.expression) && |
| 248 | + node.expression.text === name |
| 249 | + ) { |
| 250 | + result = node; |
| 251 | + } |
| 252 | + |
| 253 | + if (!result) { |
| 254 | + node.forEachChild(walk); |
| 255 | + } |
| 256 | + }); |
| 257 | + |
| 258 | + return result; |
| 259 | +} |
| 260 | + |
| 261 | +/** Creates an `importProvidersFrom({{moduleName}})` call. */ |
| 262 | +function createImportProvidersFromCall(moduleName: string): ts.CallExpression { |
| 263 | + return ts.factory.createCallChain( |
| 264 | + ts.factory.createIdentifier('importProvidersFrom'), |
| 265 | + undefined, |
| 266 | + undefined, |
| 267 | + [ts.factory.createIdentifier(moduleName)], |
| 268 | + ); |
| 269 | +} |
| 270 | + |
| 271 | +/** Creates a `providers: [importProvidersFrom({{moduleName}})]` property assignment. */ |
| 272 | +function createProvidersAssignment(moduleName: string): ts.PropertyAssignment { |
| 273 | + return ts.factory.createPropertyAssignment( |
| 274 | + 'providers', |
| 275 | + ts.factory.createArrayLiteralExpression([createImportProvidersFromCall(moduleName)]), |
| 276 | + ); |
| 277 | +} |
0 commit comments