|
| 1 | +import { |
| 2 | + insertImport as originalInsertImport, |
| 3 | + findNodes as originalFindNodes, |
| 4 | + findNode as originalFindNode, |
| 5 | + getSourceNodes as originalGetSourceNodes, |
| 6 | + insertAfterLastOccurrence as originalInsertAfterLastOccurrence, |
| 7 | + getContentOfKeyLiteral as originalGetContentOfKeyLiteral, |
| 8 | + getFirstNgModuleName as originalGetFirstNgModuleName, |
| 9 | + getDecoratorMetadata as originalGetDecoratorMetadata, |
| 10 | + getMetadataField as originalGetMetadataField, |
| 11 | + getRouterModuleDeclaration as originalGetRouterModuleDeclaration, |
| 12 | + addSymbolToNgModuleMetadata as originalAddSymbolToNgModuleMetadata, |
| 13 | + addDeclarationToModule as originalAddDeclarationToModule, |
| 14 | + addImportToModule as originalAddImportToModule, |
| 15 | + addProviderToModule as originalAddProviderToModule, |
| 16 | + addExportToModule as originalAddExportToModule, |
| 17 | + addBootstrapToModule as originalAddBootstrapToModule, |
| 18 | + addEntryComponentToModule as originalAddEntryComponentToModule, |
| 19 | + addRouteDeclarationToModule as originalAddRouteDeclarationToModule, |
| 20 | + isImported as originalIsImported |
| 21 | +} from '@schematics/angular/utility/ast-utils'; |
| 22 | +import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 23 | + |
| 24 | +import { Change } from './change'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Add Import `import { symbolName } from fileName` if the import doesn't exit |
| 28 | + * already. Assumes fileToEdit can be resolved and accessed. |
| 29 | + * @param fileToEdit (file we want to add import to) |
| 30 | + * @param symbolName (item to import) |
| 31 | + * @param fileName (path to the file) |
| 32 | + * @param isDefault (if true, import follows style for importing default exports) |
| 33 | + * @return Change |
| 34 | + */ |
| 35 | +export function insertImport( |
| 36 | + source: ts.SourceFile, |
| 37 | + fileToEdit: string, |
| 38 | + symbolName: string, |
| 39 | + fileName: string, |
| 40 | + isDefault = false |
| 41 | +): Change { |
| 42 | + return originalInsertImport(source, fileToEdit, symbolName, fileName, isDefault); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Find all nodes from the AST in the subtree of node of SyntaxKind kind. |
| 47 | + * @param node |
| 48 | + * @param kind |
| 49 | + * @param max The maximum number of items to return. |
| 50 | + * @param recursive Continue looking for nodes of kind recursive until end |
| 51 | + * the last child even when node of kind has been found. |
| 52 | + * @return all nodes of kind, or [] if none is found |
| 53 | + */ |
| 54 | +export function findNodes(node: ts.Node, kind: ts.SyntaxKind, max = Infinity, recursive = false): ts.Node[] { |
| 55 | + return originalFindNodes(node, kind, max, recursive); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Get all the nodes from a source. |
| 60 | + * @param sourceFile The source file object. |
| 61 | + * @returns {Observable<ts.Node>} An observable of all the nodes in the source. |
| 62 | + */ |
| 63 | +export function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[] { |
| 64 | + return originalGetSourceNodes(sourceFile); |
| 65 | +} |
| 66 | + |
| 67 | +export function findNode(node: ts.Node, kind: ts.SyntaxKind, text: string): ts.Node | null { |
| 68 | + return originalFindNode(node, kind, text); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]` |
| 73 | + * or after the last of occurence of `syntaxKind` if the last occurence is a sub child |
| 74 | + * of ts.SyntaxKind[nodes[i].kind] and save the changes in file. |
| 75 | + * |
| 76 | + * @param nodes insert after the last occurence of nodes |
| 77 | + * @param toInsert string to insert |
| 78 | + * @param file file to insert changes into |
| 79 | + * @param fallbackPos position to insert if toInsert happens to be the first occurence |
| 80 | + * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after |
| 81 | + * @return Change instance |
| 82 | + * @throw Error if toInsert is first occurence but fall back is not set |
| 83 | + */ |
| 84 | +export function insertAfterLastOccurrence( |
| 85 | + nodes: ts.Node[], |
| 86 | + toInsert: string, |
| 87 | + file: string, |
| 88 | + fallbackPos: number, |
| 89 | + syntaxKind?: ts.SyntaxKind |
| 90 | +): Change { |
| 91 | + return originalInsertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind); |
| 92 | +} |
| 93 | + |
| 94 | +export function getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string | null { |
| 95 | + return originalGetContentOfKeyLiteral(_source, node); |
| 96 | +} |
| 97 | + |
| 98 | +export function getDecoratorMetadata(source: ts.SourceFile, identifier: string, module: string): ts.Node[] { |
| 99 | + return originalGetDecoratorMetadata(source, identifier, module); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Given a source file with @NgModule class(es), find the name of the first @NgModule class. |
| 104 | + * |
| 105 | + * @param source source file containing one or more @NgModule |
| 106 | + * @returns the name of the first @NgModule, or `undefined` if none is found |
| 107 | + */ |
| 108 | +export function getFirstNgModuleName(source: ts.SourceFile): string | undefined { |
| 109 | + return originalGetFirstNgModuleName(source); |
| 110 | +} |
| 111 | + |
| 112 | +export function getMetadataField(node: ts.ObjectLiteralExpression, metadataField: string): ts.ObjectLiteralElement[] { |
| 113 | + return originalGetMetadataField(node, metadataField); |
| 114 | +} |
| 115 | + |
| 116 | +export function addSymbolToNgModuleMetadata( |
| 117 | + source: ts.SourceFile, |
| 118 | + ngModulePath: string, |
| 119 | + metadataField: string, |
| 120 | + symbolName: string, |
| 121 | + importPath: string | null = null |
| 122 | +): Change[] { |
| 123 | + return originalAddSymbolToNgModuleMetadata(source, ngModulePath, metadataField, symbolName, importPath); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Custom function to insert a declaration (component, pipe, directive) |
| 128 | + * into NgModule declarations. It also imports the component. |
| 129 | + */ |
| 130 | +export function addDeclarationToModule( |
| 131 | + source: ts.SourceFile, |
| 132 | + modulePath: string, |
| 133 | + classifiedName: string, |
| 134 | + importPath: string |
| 135 | +): Change[] { |
| 136 | + return originalAddDeclarationToModule(source, modulePath, classifiedName, importPath); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Custom function to insert an NgModule into NgModule imports. It also imports the module. |
| 141 | + */ |
| 142 | +export function addImportToModule( |
| 143 | + source: ts.SourceFile, |
| 144 | + modulePath: string, |
| 145 | + classifiedName: string, |
| 146 | + importPath: string |
| 147 | +): Change[] { |
| 148 | + return originalAddImportToModule(source, modulePath, classifiedName, importPath); |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Custom function to insert a provider into NgModule. It also imports it. |
| 153 | + */ |
| 154 | +export function addProviderToModule( |
| 155 | + source: ts.SourceFile, |
| 156 | + modulePath: string, |
| 157 | + classifiedName: string, |
| 158 | + importPath: string |
| 159 | +): Change[] { |
| 160 | + return originalAddProviderToModule(source, modulePath, classifiedName, importPath); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Custom function to insert an export into NgModule. It also imports it. |
| 165 | + */ |
| 166 | +export function addExportToModule( |
| 167 | + source: ts.SourceFile, |
| 168 | + modulePath: string, |
| 169 | + classifiedName: string, |
| 170 | + importPath: string |
| 171 | +): Change[] { |
| 172 | + return originalAddExportToModule(source, modulePath, classifiedName, importPath); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Custom function to insert an export into NgModule. It also imports it. |
| 177 | + */ |
| 178 | +export function addBootstrapToModule( |
| 179 | + source: ts.SourceFile, |
| 180 | + modulePath: string, |
| 181 | + classifiedName: string, |
| 182 | + importPath: string |
| 183 | +): Change[] { |
| 184 | + return originalAddBootstrapToModule(source, modulePath, classifiedName, importPath); |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Custom function to insert an entryComponent into NgModule. It also imports it. |
| 189 | + */ |
| 190 | +export function addEntryComponentToModule( |
| 191 | + source: ts.SourceFile, |
| 192 | + modulePath: string, |
| 193 | + classifiedName: string, |
| 194 | + importPath: string |
| 195 | +): Change[] { |
| 196 | + return originalAddEntryComponentToModule(source, modulePath, classifiedName, importPath); |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * Determine if an import already exists. |
| 201 | + */ |
| 202 | +export function isImported(source: ts.SourceFile, classifiedName: string, importPath: string): boolean { |
| 203 | + return originalIsImported(source, classifiedName, importPath); |
| 204 | +} |
| 205 | + |
| 206 | +/** |
| 207 | + * Returns the RouterModule declaration from NgModule metadata, if any. |
| 208 | + */ |
| 209 | +export function getRouterModuleDeclaration(source: ts.SourceFile): ts.Expression | undefined { |
| 210 | + return originalGetRouterModuleDeclaration(source); |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Adds a new route declaration to a router module (i.e. has a RouterModule declaration) |
| 215 | + */ |
| 216 | +export function addRouteDeclarationToModule(source: ts.SourceFile, fileToAdd: string, routeLiteral: string): Change { |
| 217 | + return originalAddRouteDeclarationToModule(source, fileToAdd, routeLiteral); |
| 218 | +} |
0 commit comments