|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import { |
| 3 | + Tree, |
| 4 | + Rule, |
| 5 | + chain, |
| 6 | + SchematicContext, |
| 7 | +} from '@angular-devkit/schematics'; |
| 8 | +import { |
| 9 | + addPackageToPackageJson, |
| 10 | + Change, |
| 11 | + commitChanges, |
| 12 | + createReplaceChange, |
| 13 | + InsertChange, |
| 14 | + visitTSSourceFiles, |
| 15 | +} from '../../schematics-core'; |
| 16 | +import * as os from 'os'; |
| 17 | +import { createRemoveChange } from '../../schematics-core/utility/change'; |
| 18 | + |
| 19 | +export function migrateConcatLatestFromImport(): Rule { |
| 20 | + return (tree: Tree, ctx: SchematicContext) => { |
| 21 | + const changes: Change[] = []; |
| 22 | + addPackageToPackageJson(tree, 'dependencies', '@ngrx/operators', '^18.0.0'); |
| 23 | + |
| 24 | + visitTSSourceFiles(tree, (sourceFile) => { |
| 25 | + const importDeclarations = new Array<ts.ImportDeclaration>(); |
| 26 | + getImportDeclarations(sourceFile, importDeclarations); |
| 27 | + |
| 28 | + const effectsImportsAndDeclaration = importDeclarations |
| 29 | + .map((effectsImportDeclaration) => { |
| 30 | + const effectsImports = getEffectsNamedBinding( |
| 31 | + effectsImportDeclaration |
| 32 | + ); |
| 33 | + if (effectsImports) { |
| 34 | + return { effectsImports, effectsImportDeclaration }; |
| 35 | + } else { |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + }) |
| 39 | + .find(Boolean); |
| 40 | + |
| 41 | + if (!effectsImportsAndDeclaration) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const { effectsImports, effectsImportDeclaration } = |
| 46 | + effectsImportsAndDeclaration; |
| 47 | + |
| 48 | + const operatorsImportDeclaration = importDeclarations.find((node) => |
| 49 | + node.moduleSpecifier.getText().includes('@ngrx/operators') |
| 50 | + ); |
| 51 | + |
| 52 | + const otherEffectsImports = effectsImports.elements |
| 53 | + .filter((element) => element.name.getText() !== 'concatLatestFrom') |
| 54 | + .map((element) => element.name.getText()) |
| 55 | + .join(', '); |
| 56 | + |
| 57 | + // Remove `concatLatestFrom` from @ngrx/effects and leave the other imports |
| 58 | + if (otherEffectsImports) { |
| 59 | + changes.push( |
| 60 | + createReplaceChange( |
| 61 | + sourceFile, |
| 62 | + effectsImportDeclaration, |
| 63 | + effectsImportDeclaration.getText(), |
| 64 | + `import { ${otherEffectsImports} } from '@ngrx/effects';` |
| 65 | + ) |
| 66 | + ); |
| 67 | + } |
| 68 | + // Remove complete @ngrx/effects import because it contains only `concatLatestFrom` |
| 69 | + else { |
| 70 | + changes.push( |
| 71 | + createRemoveChange( |
| 72 | + sourceFile, |
| 73 | + effectsImportDeclaration, |
| 74 | + effectsImportDeclaration.getStart(), |
| 75 | + effectsImportDeclaration.getEnd() + 1 |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + let importAppendedInExistingDeclaration = false; |
| 81 | + if (operatorsImportDeclaration?.importClause?.namedBindings) { |
| 82 | + const bindings = operatorsImportDeclaration.importClause.namedBindings; |
| 83 | + if (ts.isNamedImports(bindings)) { |
| 84 | + // Add import to existing @ngrx/operators |
| 85 | + const updatedImports = [ |
| 86 | + ...bindings.elements.map((element) => element.name.getText()), |
| 87 | + 'concatLatestFrom', |
| 88 | + ]; |
| 89 | + const newOperatorsImport = `import { ${updatedImports.join( |
| 90 | + ', ' |
| 91 | + )} } from '@ngrx/operators';`; |
| 92 | + changes.push( |
| 93 | + createReplaceChange( |
| 94 | + sourceFile, |
| 95 | + operatorsImportDeclaration, |
| 96 | + operatorsImportDeclaration.getText(), |
| 97 | + newOperatorsImport |
| 98 | + ) |
| 99 | + ); |
| 100 | + importAppendedInExistingDeclaration = true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!importAppendedInExistingDeclaration) { |
| 105 | + // Add new @ngrx/operators import line |
| 106 | + const newOperatorsImport = `import { concatLatestFrom } from '@ngrx/operators';`; |
| 107 | + changes.push( |
| 108 | + new InsertChange( |
| 109 | + sourceFile.fileName, |
| 110 | + effectsImportDeclaration.getEnd() + 1, |
| 111 | + `${newOperatorsImport}${os.EOL}` |
| 112 | + ) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + commitChanges(tree, sourceFile.fileName, changes); |
| 117 | + |
| 118 | + if (changes.length) { |
| 119 | + ctx.logger.info( |
| 120 | + `[@ngrx/effects] Updated concatLatestFrom to import from '@ngrx/operators'` |
| 121 | + ); |
| 122 | + } |
| 123 | + }); |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +function getImportDeclarations( |
| 128 | + node: ts.Node, |
| 129 | + imports: ts.ImportDeclaration[] |
| 130 | +): void { |
| 131 | + if (ts.isImportDeclaration(node)) { |
| 132 | + imports.push(node); |
| 133 | + } |
| 134 | + |
| 135 | + ts.forEachChild(node, (childNode) => |
| 136 | + getImportDeclarations(childNode, imports) |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +function getEffectsNamedBinding( |
| 141 | + node: ts.ImportDeclaration |
| 142 | +): ts.NamedImports | null { |
| 143 | + const namedBindings = node?.importClause?.namedBindings; |
| 144 | + if ( |
| 145 | + node.moduleSpecifier.getText().includes('@ngrx/effects') && |
| 146 | + namedBindings && |
| 147 | + ts.isNamedImports(namedBindings) |
| 148 | + ) { |
| 149 | + return namedBindings; |
| 150 | + } |
| 151 | + |
| 152 | + return null; |
| 153 | +} |
| 154 | + |
| 155 | +export default function (): Rule { |
| 156 | + return chain([migrateConcatLatestFromImport()]); |
| 157 | +} |
0 commit comments