|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import ts from 'typescript'; |
| 10 | +import { |
| 11 | + confirmAsSerializable, |
| 12 | + ProgramInfo, |
| 13 | + ProjectFile, |
| 14 | + projectFile, |
| 15 | + Replacement, |
| 16 | + Serializable, |
| 17 | + TextUpdate, |
| 18 | + TsurgeFunnelMigration, |
| 19 | +} from '../../utils/tsurge'; |
| 20 | +import {getImportSpecifier} from '../../utils/typescript/imports'; |
| 21 | +import {isReferenceToImport} from '../../utils/typescript/symbol'; |
| 22 | + |
| 23 | +export interface CompilationUnitData { |
| 24 | + locations: Location[]; |
| 25 | +} |
| 26 | + |
| 27 | +/** Information about the `getCurrentNavigation` identifier in `Router.getCurrentNavigation`. */ |
| 28 | +interface Location { |
| 29 | + /** File in which the expression is defined. */ |
| 30 | + file: ProjectFile; |
| 31 | + |
| 32 | + /** Start of the `getCurrentNavigation` identifier. */ |
| 33 | + position: number; |
| 34 | +} |
| 35 | + |
| 36 | +/** Name of the method being replaced. */ |
| 37 | +const METHOD_NAME = 'lastSuccessfulNavigation'; |
| 38 | + |
| 39 | +/** Migration that replaces `Router.lastSuccessfulNavigation` usages with `Router.lastSuccessfulNavigation()`. */ |
| 40 | +export class RouterLastSuccessfulNavigationMigration extends TsurgeFunnelMigration< |
| 41 | + CompilationUnitData, |
| 42 | + CompilationUnitData |
| 43 | +> { |
| 44 | + override async analyze(info: ProgramInfo): Promise<Serializable<CompilationUnitData>> { |
| 45 | + const locations: Location[] = []; |
| 46 | + |
| 47 | + for (const sourceFile of info.sourceFiles) { |
| 48 | + const routerSpecifier = getImportSpecifier(sourceFile, '@angular/router', 'Router'); |
| 49 | + |
| 50 | + if (routerSpecifier === null) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + const typeChecker = info.program.getTypeChecker(); |
| 55 | + sourceFile.forEachChild(function walk(node) { |
| 56 | + if ( |
| 57 | + ts.isPropertyAccessExpression(node) && |
| 58 | + node.name.text === METHOD_NAME && |
| 59 | + isRouterType(typeChecker, node.expression, routerSpecifier) |
| 60 | + ) { |
| 61 | + locations.push({file: projectFile(sourceFile, info), position: node.name.getStart()}); |
| 62 | + } else { |
| 63 | + node.forEachChild(walk); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + return confirmAsSerializable({locations}); |
| 69 | + } |
| 70 | + |
| 71 | + override async migrate(globalData: CompilationUnitData) { |
| 72 | + const replacements = globalData.locations.map(({file, position}) => { |
| 73 | + return new Replacement( |
| 74 | + file, |
| 75 | + new TextUpdate({ |
| 76 | + position: position, |
| 77 | + end: position + METHOD_NAME.length, |
| 78 | + toInsert: 'lastSuccessfulNavigation()', |
| 79 | + }), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + return confirmAsSerializable({replacements}); |
| 84 | + } |
| 85 | + |
| 86 | + override async combine( |
| 87 | + unitA: CompilationUnitData, |
| 88 | + unitB: CompilationUnitData, |
| 89 | + ): Promise<Serializable<CompilationUnitData>> { |
| 90 | + const seen = new Set<string>(); |
| 91 | + const locations: Location[] = []; |
| 92 | + const combined = [...unitA.locations, ...unitB.locations]; |
| 93 | + |
| 94 | + for (const location of combined) { |
| 95 | + const key = `${location.file.id}#${location.position}`; |
| 96 | + if (!seen.has(key)) { |
| 97 | + seen.add(key); |
| 98 | + locations.push(location); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return confirmAsSerializable({locations}); |
| 103 | + } |
| 104 | + |
| 105 | + override async globalMeta( |
| 106 | + combinedData: CompilationUnitData, |
| 107 | + ): Promise<Serializable<CompilationUnitData>> { |
| 108 | + return confirmAsSerializable(combinedData); |
| 109 | + } |
| 110 | + |
| 111 | + override async stats() { |
| 112 | + return confirmAsSerializable({}); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Checks if the given symbol represents a Router type. |
| 118 | + */ |
| 119 | +function isRouterType( |
| 120 | + typeChecker: ts.TypeChecker, |
| 121 | + expression: ts.Expression, |
| 122 | + routerSpecifier: ts.ImportSpecifier, |
| 123 | +): boolean { |
| 124 | + const expressionType = typeChecker.getTypeAtLocation(expression); |
| 125 | + const expressionSymbol = expressionType.getSymbol(); |
| 126 | + if (!expressionSymbol) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + const declarations = expressionSymbol.getDeclarations() ?? []; |
| 131 | + |
| 132 | + for (const declaration of declarations) { |
| 133 | + if (isReferenceToImport(typeChecker, declaration, routerSpecifier)) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return declarations.some((decl) => decl === routerSpecifier); |
| 139 | +} |
0 commit comments