|
| 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 { Rule, Tree } from '@angular-devkit/schematics'; |
| 10 | +import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 11 | +import { hasTopLevelIdentifier, insertImport } from '../ast-utils'; |
| 12 | +import { applyToUpdateRecorder } from '../change'; |
| 13 | + |
| 14 | +/** Generated code that hasn't been interpolated yet. */ |
| 15 | +export interface PendingCode { |
| 16 | + /** Code that will be inserted. */ |
| 17 | + expression: string; |
| 18 | + |
| 19 | + /** Imports that need to be added to the file in which the code is inserted. */ |
| 20 | + imports: PendingImports; |
| 21 | +} |
| 22 | + |
| 23 | +/** Map keeping track of imports and aliases under which they're referred to in an expresion. */ |
| 24 | +type PendingImports = Map<string, Map<string, string>>; |
| 25 | + |
| 26 | +/** Counter used to generate unique IDs. */ |
| 27 | +let uniqueIdCounter = 0; |
| 28 | + |
| 29 | +/** |
| 30 | + * Callback invoked by a Rule that produces the code |
| 31 | + * that needs to be inserted somewhere in the app. |
| 32 | + */ |
| 33 | +export type CodeBlockCallback = (block: CodeBlock) => PendingCode; |
| 34 | + |
| 35 | +/** |
| 36 | + * Utility class used to generate blocks of code that |
| 37 | + * can be inserted by the devkit into a user's app. |
| 38 | + */ |
| 39 | +export class CodeBlock { |
| 40 | + private _imports: PendingImports = new Map<string, Map<string, string>>(); |
| 41 | + |
| 42 | + // Note: the methods here are defined as arrow function so that they can be destructured by |
| 43 | + // consumers without losing their context. This makes the API more concise. |
| 44 | + |
| 45 | + /** Function used to tag a code block in order to produce a `PendingCode` object. */ |
| 46 | + code = (strings: TemplateStringsArray, ...params: unknown[]): PendingCode => { |
| 47 | + return { |
| 48 | + expression: strings.map((part, index) => part + (params[index] || '')).join(''), |
| 49 | + imports: this._imports, |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + /** |
| 54 | + * Used inside of a code block to mark external symbols and which module they should be imported |
| 55 | + * from. When the code is inserted, the required import statements will be produced automatically. |
| 56 | + * @param symbolName Name of the external symbol. |
| 57 | + * @param moduleName Module from which the symbol should be imported. |
| 58 | + */ |
| 59 | + external = (symbolName: string, moduleName: string): string => { |
| 60 | + if (!this._imports.has(moduleName)) { |
| 61 | + this._imports.set(moduleName, new Map()); |
| 62 | + } |
| 63 | + |
| 64 | + const symbolsPerModule = this._imports.get(moduleName) as Map<string, string>; |
| 65 | + |
| 66 | + if (!symbolsPerModule.has(symbolName)) { |
| 67 | + symbolsPerModule.set(symbolName, `@@__SCHEMATIC_PLACEHOLDER_${uniqueIdCounter++}__@@`); |
| 68 | + } |
| 69 | + |
| 70 | + return symbolsPerModule.get(symbolName) as string; |
| 71 | + }; |
| 72 | + |
| 73 | + /** |
| 74 | + * Produces the necessary rules to transform a `PendingCode` object into valid code. |
| 75 | + * @param initialCode Code pending transformed. |
| 76 | + * @param filePath Path of the file in which the code will be inserted. |
| 77 | + */ |
| 78 | + static transformPendingCode(initialCode: PendingCode, filePath: string) { |
| 79 | + const code = { ...initialCode }; |
| 80 | + const rules: Rule[] = []; |
| 81 | + |
| 82 | + code.imports.forEach((symbols, moduleName) => { |
| 83 | + symbols.forEach((placeholder, symbolName) => { |
| 84 | + rules.push((tree: Tree) => { |
| 85 | + const recorder = tree.beginUpdate(filePath); |
| 86 | + const sourceFile = ts.createSourceFile( |
| 87 | + filePath, |
| 88 | + tree.readText(filePath), |
| 89 | + ts.ScriptTarget.Latest, |
| 90 | + true, |
| 91 | + ); |
| 92 | + |
| 93 | + // Note that this could still technically clash if there's a top-level symbol called |
| 94 | + // `${symbolName}_alias`, however this is unlikely. We can revisit this if it becomes |
| 95 | + // a problem. |
| 96 | + const alias = hasTopLevelIdentifier(sourceFile, symbolName, moduleName) |
| 97 | + ? symbolName + '_alias' |
| 98 | + : undefined; |
| 99 | + |
| 100 | + code.expression = code.expression.replace( |
| 101 | + new RegExp(placeholder, 'g'), |
| 102 | + alias || symbolName, |
| 103 | + ); |
| 104 | + |
| 105 | + applyToUpdateRecorder(recorder, [ |
| 106 | + insertImport(sourceFile, filePath, symbolName, moduleName, false, alias), |
| 107 | + ]); |
| 108 | + tree.commitUpdate(recorder); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + return { code, rules }; |
| 114 | + } |
| 115 | +} |
0 commit comments