|
| 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 { |
| 10 | + confirmAsSerializable, |
| 11 | + ProgramInfo, |
| 12 | + projectFile, |
| 13 | + Replacement, |
| 14 | + Serializable, |
| 15 | + TsurgeFunnelMigration, |
| 16 | +} from '../../utils/tsurge'; |
| 17 | +import {ImportManager} from '@angular/compiler-cli/private/migrations'; |
| 18 | +import {applyImportManagerChanges} from '../../utils/tsurge/helpers/apply_import_manager'; |
| 19 | + |
| 20 | +import {MigrationConfig} from './types'; |
| 21 | +import { |
| 22 | + findRouterTestingModuleUsages, |
| 23 | + processRouterTestingModuleUsage, |
| 24 | + RouterTestingModuleUsage, |
| 25 | +} from './utils'; |
| 26 | + |
| 27 | +export interface CompilationUnitData { |
| 28 | + replacements: Replacement[]; |
| 29 | + migratedUsages: RouterTestingModuleUsage[]; |
| 30 | + filesWithLocationMocks: Map<string, boolean>; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Migration that converts RouterTestingModule usages to the recommended API: |
| 35 | + * - Replace RouterTestingModule with RouterModule for all tests (respecting existing imports) |
| 36 | + * - Adds provideLocationMocks only when needed and not conflicting |
| 37 | + */ |
| 38 | +export class RouterTestingModuleMigration extends TsurgeFunnelMigration< |
| 39 | + CompilationUnitData, |
| 40 | + CompilationUnitData |
| 41 | +> { |
| 42 | + constructor(private readonly config: MigrationConfig = {}) { |
| 43 | + super(); |
| 44 | + } |
| 45 | + |
| 46 | + override async analyze(info: ProgramInfo): Promise<Serializable<CompilationUnitData>> { |
| 47 | + const replacements: Replacement[] = []; |
| 48 | + const migratedUsages: RouterTestingModuleUsage[] = []; |
| 49 | + const filesWithLocationMocks = new Map<string, boolean>(); |
| 50 | + const importManager = new ImportManager({ |
| 51 | + shouldUseSingleQuotes: () => true, |
| 52 | + }); |
| 53 | + |
| 54 | + for (const sourceFile of info.sourceFiles) { |
| 55 | + const file = projectFile(sourceFile, info); |
| 56 | + |
| 57 | + if (this.config.shouldMigrate && !this.config.shouldMigrate(file)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + const usages = findRouterTestingModuleUsages(sourceFile); |
| 62 | + |
| 63 | + for (const usage of usages) { |
| 64 | + processRouterTestingModuleUsage(usage, sourceFile, info, importManager, replacements); |
| 65 | + migratedUsages.push(usage); |
| 66 | + |
| 67 | + if (usage.hasLocationOrLocationStrategyImport && !usage.hasExistingLocationProviders) { |
| 68 | + filesWithLocationMocks.set(sourceFile.fileName, true); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + applyImportManagerChanges(importManager, replacements, info.sourceFiles, info); |
| 74 | + |
| 75 | + return confirmAsSerializable({ |
| 76 | + replacements, |
| 77 | + migratedUsages, |
| 78 | + filesWithLocationMocks, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + override async migrate(globalData: CompilationUnitData) { |
| 83 | + return { |
| 84 | + replacements: globalData.replacements, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + override async combine( |
| 89 | + unitA: CompilationUnitData, |
| 90 | + unitB: CompilationUnitData, |
| 91 | + ): Promise<Serializable<CompilationUnitData>> { |
| 92 | + const combinedFilesWithLocationMocks = new Map(unitA.filesWithLocationMocks); |
| 93 | + |
| 94 | + for (const [fileName, hasLocationMocks] of unitB.filesWithLocationMocks) { |
| 95 | + combinedFilesWithLocationMocks.set( |
| 96 | + fileName, |
| 97 | + hasLocationMocks || combinedFilesWithLocationMocks.get(fileName) || false, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return confirmAsSerializable({ |
| 102 | + replacements: [...unitA.replacements, ...unitB.replacements], |
| 103 | + migratedUsages: [...unitA.migratedUsages, ...unitB.migratedUsages], |
| 104 | + filesWithLocationMocks: combinedFilesWithLocationMocks, |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + override async globalMeta( |
| 109 | + combinedData: CompilationUnitData, |
| 110 | + ): Promise<Serializable<CompilationUnitData>> { |
| 111 | + return confirmAsSerializable(combinedData); |
| 112 | + } |
| 113 | + |
| 114 | + override async stats(globalMetadata: CompilationUnitData) { |
| 115 | + const stats = { |
| 116 | + counters: { |
| 117 | + replacements: globalMetadata.replacements.length, |
| 118 | + migratedUsages: globalMetadata.migratedUsages.length, |
| 119 | + filesWithLocationMocks: globalMetadata.filesWithLocationMocks.size, |
| 120 | + totalFiles: new Set(globalMetadata.migratedUsages.map((usage) => usage.sourceFile.fileName)) |
| 121 | + .size, |
| 122 | + }, |
| 123 | + }; |
| 124 | + return stats as Serializable<typeof stats>; |
| 125 | + } |
| 126 | +} |
0 commit comments