|
| 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 | + Replacement, |
| 15 | + Serializable, |
| 16 | + TextUpdate, |
| 17 | + TsurgeFunnelMigration, |
| 18 | +} from '../../utils/tsurge'; |
| 19 | + |
| 20 | +export interface UnitAnalysisMetadata { |
| 21 | + replacements: Replacement[]; |
| 22 | +} |
| 23 | + |
| 24 | +export class CanMatchSnapshotRequiredMigration extends TsurgeFunnelMigration< |
| 25 | + UnitAnalysisMetadata, |
| 26 | + UnitAnalysisMetadata |
| 27 | +> { |
| 28 | + override async analyze(info: ProgramInfo): Promise<Serializable<UnitAnalysisMetadata>> { |
| 29 | + const replacements: Replacement[] = []; |
| 30 | + const {sourceFiles, program} = info; |
| 31 | + const typeChecker = program.getTypeChecker(); |
| 32 | + |
| 33 | + for (const sourceFile of sourceFiles) { |
| 34 | + const walk = (node: ts.Node): void => { |
| 35 | + ts.forEachChild(node, walk); |
| 36 | + |
| 37 | + if (ts.isCallExpression(node)) { |
| 38 | + let shouldMigrate = false; |
| 39 | + |
| 40 | + // 1. Method calls objective: obj.canMatch(a, b) |
| 41 | + if ( |
| 42 | + ts.isPropertyAccessExpression(node.expression) && |
| 43 | + node.expression.name.text === 'canMatch' |
| 44 | + ) { |
| 45 | + const type = typeChecker.getTypeAtLocation(node.expression.expression); |
| 46 | + const classSymbol = type.getSymbol(); |
| 47 | + if (classSymbol && classSymbol.declarations) { |
| 48 | + const decl = classSymbol.declarations[0]; |
| 49 | + if (ts.isClassDeclaration(decl)) { |
| 50 | + if (implementsInterface(decl, 'CanMatch')) { |
| 51 | + shouldMigrate = true; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // 2. Function calls objective: canMatch(a, b) |
| 58 | + if (ts.isIdentifier(node.expression) && node.expression.text === 'canMatch') { |
| 59 | + const type = typeChecker.getTypeAtLocation(node.expression); |
| 60 | + const typeStr = typeChecker.typeToString(type); |
| 61 | + if (typeStr.includes('CanMatchFn')) { |
| 62 | + shouldMigrate = true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (shouldMigrate && node.arguments.length === 2) { |
| 67 | + const lastArg = node.arguments[1]; |
| 68 | + replacements.push( |
| 69 | + new Replacement( |
| 70 | + projectFile(sourceFile, info), |
| 71 | + new TextUpdate({ |
| 72 | + position: lastArg.getEnd(), |
| 73 | + end: lastArg.getEnd(), |
| 74 | + toInsert: ', {} as any /* added by migration */', |
| 75 | + }), |
| 76 | + ), |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + ts.forEachChild(sourceFile, walk); |
| 83 | + } |
| 84 | + |
| 85 | + return confirmAsSerializable({replacements}); |
| 86 | + } |
| 87 | + |
| 88 | + override async combine( |
| 89 | + unitA: UnitAnalysisMetadata, |
| 90 | + unitB: UnitAnalysisMetadata, |
| 91 | + ): Promise<Serializable<UnitAnalysisMetadata>> { |
| 92 | + return confirmAsSerializable({ |
| 93 | + replacements: [...unitA.replacements, ...unitB.replacements], |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + override async globalMeta( |
| 98 | + combinedData: UnitAnalysisMetadata, |
| 99 | + ): Promise<Serializable<UnitAnalysisMetadata>> { |
| 100 | + return confirmAsSerializable(combinedData); |
| 101 | + } |
| 102 | + |
| 103 | + override async stats(globalMetadata: UnitAnalysisMetadata): Promise<Serializable<unknown>> { |
| 104 | + return confirmAsSerializable({}); |
| 105 | + } |
| 106 | + |
| 107 | + override async migrate(globalData: UnitAnalysisMetadata): Promise<{replacements: Replacement[]}> { |
| 108 | + return {replacements: globalData.replacements}; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function implementsInterface(decl: ts.ClassDeclaration, interfaceName: string): boolean { |
| 113 | + if (!decl.heritageClauses) return false; |
| 114 | + |
| 115 | + for (const clause of decl.heritageClauses) { |
| 116 | + if (clause.token === ts.SyntaxKind.ImplementsKeyword) { |
| 117 | + for (const expr of clause.types) { |
| 118 | + if (ts.isIdentifier(expr.expression) && expr.expression.text === interfaceName) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return false; |
| 126 | +} |
0 commit comments