|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import { |
| 3 | + Tree, |
| 4 | + Rule, |
| 5 | + chain, |
| 6 | + SchematicContext, |
| 7 | +} from '@angular-devkit/schematics'; |
| 8 | +import { |
| 9 | + Change, |
| 10 | + commitChanges, |
| 11 | + createReplaceChange, |
| 12 | + InsertChange, |
| 13 | + visitTSSourceFiles, |
| 14 | +} from '../../schematics-core'; |
| 15 | +import { createRemoveChange } from '../../schematics-core/utility/change'; |
| 16 | + |
| 17 | +const storeModelsPath = '@ngrx/store/src/models'; |
| 18 | +const filesWithChanges: string[] = []; |
| 19 | + |
| 20 | +export function migrateStoreTypedAction(): Rule { |
| 21 | + return (tree: Tree, ctx: SchematicContext) => { |
| 22 | + visitTSSourceFiles(tree, (sourceFile) => { |
| 23 | + const changes: Change[] = []; |
| 24 | + |
| 25 | + const importDeclarations = new Array<ts.ImportDeclaration>(); |
| 26 | + getImportDeclarations(sourceFile, importDeclarations); |
| 27 | + |
| 28 | + const storeModelsImportsAndDeclaration = importDeclarations |
| 29 | + .map((storeModelsImportDeclaration) => { |
| 30 | + const storeModelsImports = getStoreModelsNamedBindings( |
| 31 | + storeModelsImportDeclaration |
| 32 | + ); |
| 33 | + if (storeModelsImports) { |
| 34 | + return { storeModelsImports, storeModelsImportDeclaration }; |
| 35 | + } else { |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + }) |
| 39 | + .find(Boolean); |
| 40 | + |
| 41 | + if (!storeModelsImportsAndDeclaration) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const { storeModelsImports, storeModelsImportDeclaration } = |
| 46 | + storeModelsImportsAndDeclaration; |
| 47 | + |
| 48 | + const storeImportDeclaration = importDeclarations.find( |
| 49 | + (node) => |
| 50 | + node.moduleSpecifier.getText().includes('@ngrx/store') && |
| 51 | + !node.moduleSpecifier.getText().includes('@ngrx/store/') |
| 52 | + ); |
| 53 | + |
| 54 | + const otherStoreModelImports = storeModelsImports.elements |
| 55 | + .filter((element) => element.name.getText() !== 'TypedAction') |
| 56 | + .map((element) => element.name.getText()) |
| 57 | + .join(', '); |
| 58 | + |
| 59 | + // Remove `TypedAction` from @ngrx/store/src/models and leave the other imports |
| 60 | + if (otherStoreModelImports) { |
| 61 | + changes.push( |
| 62 | + createReplaceChange( |
| 63 | + sourceFile, |
| 64 | + storeModelsImportDeclaration, |
| 65 | + storeModelsImportDeclaration.getText(), |
| 66 | + `import { ${otherStoreModelImports} } from '${storeModelsPath}';` |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + // Remove complete import because it's empty |
| 71 | + else { |
| 72 | + changes.push( |
| 73 | + createRemoveChange( |
| 74 | + sourceFile, |
| 75 | + storeModelsImportDeclaration, |
| 76 | + storeModelsImportDeclaration.getStart(), |
| 77 | + storeModelsImportDeclaration.getEnd() + 1 |
| 78 | + ) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + let importAppendedInExistingDeclaration = false; |
| 83 | + if (storeImportDeclaration?.importClause?.namedBindings) { |
| 84 | + const bindings = storeImportDeclaration.importClause.namedBindings; |
| 85 | + if (ts.isNamedImports(bindings)) { |
| 86 | + // Add import to existing @ngrx/operators |
| 87 | + const updatedImports = new Set([ |
| 88 | + ...bindings.elements.map((element) => element.name.getText()), |
| 89 | + 'Action', |
| 90 | + ]); |
| 91 | + const importStatement = `import { ${[...updatedImports].join( |
| 92 | + ', ' |
| 93 | + )} } from '@ngrx/store';`; |
| 94 | + changes.push( |
| 95 | + createReplaceChange( |
| 96 | + sourceFile, |
| 97 | + storeImportDeclaration, |
| 98 | + storeImportDeclaration.getText(), |
| 99 | + importStatement |
| 100 | + ) |
| 101 | + ); |
| 102 | + importAppendedInExistingDeclaration = true; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (!importAppendedInExistingDeclaration) { |
| 107 | + // Add new @ngrx/operators import line |
| 108 | + const importStatement = `import { Action } from '@ngrx/store';`; |
| 109 | + changes.push( |
| 110 | + new InsertChange( |
| 111 | + sourceFile.fileName, |
| 112 | + storeModelsImportDeclaration.getEnd() + 1, |
| 113 | + `${importStatement}\n` |
| 114 | + ) |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + commitChanges(tree, sourceFile.fileName, changes); |
| 119 | + |
| 120 | + if (changes.length) { |
| 121 | + filesWithChanges.push(sourceFile.fileName); |
| 122 | + ctx.logger.info( |
| 123 | + `[@ngrx/store] ${sourceFile.fileName}: Replaced TypedAction to Action` |
| 124 | + ); |
| 125 | + } |
| 126 | + }); |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +export function migrateStoreTypedActionReferences(): Rule { |
| 131 | + return (tree: Tree, _ctx: SchematicContext) => { |
| 132 | + visitTSSourceFiles(tree, (sourceFile) => { |
| 133 | + if (!filesWithChanges.includes(sourceFile.fileName)) { |
| 134 | + return; |
| 135 | + } |
| 136 | + const changes: Change[] = []; |
| 137 | + const typedActionIdentifiers = new Array<ts.Identifier>(); |
| 138 | + getTypedActionUsages(sourceFile, typedActionIdentifiers); |
| 139 | + |
| 140 | + typedActionIdentifiers.forEach((identifier) => { |
| 141 | + changes.push( |
| 142 | + createReplaceChange( |
| 143 | + sourceFile, |
| 144 | + identifier, |
| 145 | + identifier.getText(), |
| 146 | + 'Action' |
| 147 | + ) |
| 148 | + ); |
| 149 | + }); |
| 150 | + commitChanges(tree, sourceFile.fileName, changes); |
| 151 | + }); |
| 152 | + }; |
| 153 | +} |
| 154 | + |
| 155 | +function getImportDeclarations( |
| 156 | + node: ts.Node, |
| 157 | + imports: ts.ImportDeclaration[] |
| 158 | +): void { |
| 159 | + if (ts.isImportDeclaration(node)) { |
| 160 | + imports.push(node); |
| 161 | + } |
| 162 | + |
| 163 | + ts.forEachChild(node, (childNode) => |
| 164 | + getImportDeclarations(childNode, imports) |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +function getTypedActionUsages( |
| 169 | + node: ts.Node, |
| 170 | + nodeIdentifiers: ts.Identifier[] |
| 171 | +): void { |
| 172 | + if (ts.isIdentifier(node) && node.getText() === 'TypedAction') { |
| 173 | + nodeIdentifiers.push(node); |
| 174 | + } |
| 175 | + |
| 176 | + ts.forEachChild(node, (childNode) => |
| 177 | + getTypedActionUsages(childNode, nodeIdentifiers) |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +function getStoreModelsNamedBindings( |
| 182 | + node: ts.ImportDeclaration |
| 183 | +): ts.NamedImports | null { |
| 184 | + const namedBindings = node?.importClause?.namedBindings; |
| 185 | + if ( |
| 186 | + node.moduleSpecifier.getText().includes(storeModelsPath) && |
| 187 | + namedBindings && |
| 188 | + ts.isNamedImports(namedBindings) |
| 189 | + ) { |
| 190 | + return namedBindings; |
| 191 | + } |
| 192 | + |
| 193 | + return null; |
| 194 | +} |
| 195 | + |
| 196 | +export default function (): Rule { |
| 197 | + return chain([ |
| 198 | + migrateStoreTypedAction(), |
| 199 | + migrateStoreTypedActionReferences(), |
| 200 | + ]); |
| 201 | +} |
0 commit comments