|
| 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 | + ProjectFileID, |
| 16 | + Replacement, |
| 17 | + Serializable, |
| 18 | + TextUpdate, |
| 19 | + TsurgeFunnelMigration, |
| 20 | +} from '../../utils/tsurge'; |
| 21 | +import { |
| 22 | + migrateNgStyleBindings, |
| 23 | + calculateImportReplacements, |
| 24 | + createNgStyleImportsArrayRemoval, |
| 25 | +} from './util'; |
| 26 | +import {AbsoluteFsPath} from '@angular/compiler-cli'; |
| 27 | +import {NgComponentTemplateVisitor} from '../../utils/ng_component_template'; |
| 28 | +import {MigrationConfig} from './types'; |
| 29 | + |
| 30 | +export interface NgStyleMigrationData { |
| 31 | + file: ProjectFile; |
| 32 | + replacementCount: number; |
| 33 | + replacements: Replacement[]; |
| 34 | +} |
| 35 | + |
| 36 | +export interface NgStyleCompilationUnitData { |
| 37 | + ngStyleReplacements: Array<NgStyleMigrationData>; |
| 38 | + importReplacements: Record<ProjectFileID, {add: Replacement[]; addAndRemove: Replacement[]}>; |
| 39 | +} |
| 40 | + |
| 41 | +export class NgStyleMigration extends TsurgeFunnelMigration< |
| 42 | + NgStyleCompilationUnitData, |
| 43 | + NgStyleCompilationUnitData |
| 44 | +> { |
| 45 | + constructor(private readonly config: MigrationConfig = {}) { |
| 46 | + super(); |
| 47 | + } |
| 48 | + |
| 49 | + private processTemplate( |
| 50 | + template: {content: string; inline: boolean; filePath: string | null; start: number}, |
| 51 | + node: ts.ClassDeclaration, |
| 52 | + file: ProjectFile, |
| 53 | + info: ProgramInfo, |
| 54 | + typeChecker: ts.TypeChecker, |
| 55 | + ): {replacements: Replacement[]; replacementCount: number} | null { |
| 56 | + const {migrated, changed, replacementCount} = migrateNgStyleBindings( |
| 57 | + template.content, |
| 58 | + this.config, |
| 59 | + node, |
| 60 | + typeChecker, |
| 61 | + ); |
| 62 | + |
| 63 | + if (!changed) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + const fileToMigrate = template.inline |
| 68 | + ? file |
| 69 | + : projectFile(template.filePath as AbsoluteFsPath, info); |
| 70 | + const end = template.start + template.content.length; |
| 71 | + |
| 72 | + return { |
| 73 | + replacements: [prepareTextReplacement(fileToMigrate, migrated, template.start, end)], |
| 74 | + replacementCount, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + override async analyze(info: ProgramInfo): Promise<Serializable<NgStyleCompilationUnitData>> { |
| 79 | + const {sourceFiles, program} = info; |
| 80 | + const typeChecker = program.getTypeChecker(); |
| 81 | + const ngStyleReplacements: Array<NgStyleMigrationData> = []; |
| 82 | + const filesWithNgStyleDeclarations = new Set<ts.SourceFile>(); |
| 83 | + |
| 84 | + for (const sf of sourceFiles) { |
| 85 | + ts.forEachChild(sf, (node: ts.Node) => { |
| 86 | + if (!ts.isClassDeclaration(node)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const file = projectFile(sf, info); |
| 91 | + |
| 92 | + if (this.config.shouldMigrate && !this.config.shouldMigrate(file)) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const templateVisitor = new NgComponentTemplateVisitor(typeChecker); |
| 97 | + templateVisitor.visitNode(node); |
| 98 | + |
| 99 | + const replacementsForStyle: Replacement[] = []; |
| 100 | + let replacementCountForStyle = 0; |
| 101 | + |
| 102 | + for (const template of templateVisitor.resolvedTemplates) { |
| 103 | + const result = this.processTemplate(template, node, file, info, typeChecker); |
| 104 | + if (result) { |
| 105 | + replacementsForStyle.push(...result.replacements); |
| 106 | + replacementCountForStyle += result.replacementCount; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (replacementCountForStyle === 0) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + filesWithNgStyleDeclarations.add(sf); |
| 115 | + |
| 116 | + const importArrayRemoval = createNgStyleImportsArrayRemoval(node, file, typeChecker); |
| 117 | + if (importArrayRemoval) { |
| 118 | + replacementsForStyle.push(importArrayRemoval); |
| 119 | + } |
| 120 | + |
| 121 | + const existing = ngStyleReplacements.find((entry) => entry.file === file); |
| 122 | + if (existing) { |
| 123 | + existing.replacements.push(...replacementsForStyle); |
| 124 | + existing.replacementCount += replacementCountForStyle; |
| 125 | + } else { |
| 126 | + ngStyleReplacements.push({ |
| 127 | + file, |
| 128 | + replacements: replacementsForStyle, |
| 129 | + replacementCount: replacementCountForStyle, |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const importReplacements = calculateImportReplacements(info, filesWithNgStyleDeclarations); |
| 136 | + return confirmAsSerializable({ngStyleReplacements, importReplacements}); |
| 137 | + } |
| 138 | + |
| 139 | + override async combine( |
| 140 | + unitA: NgStyleCompilationUnitData, |
| 141 | + unitB: NgStyleCompilationUnitData, |
| 142 | + ): Promise<Serializable<NgStyleCompilationUnitData>> { |
| 143 | + const importReplacements: Record< |
| 144 | + ProjectFileID, |
| 145 | + {add: Replacement[]; addAndRemove: Replacement[]} |
| 146 | + > = {}; |
| 147 | + |
| 148 | + for (const unit of [unitA, unitB]) { |
| 149 | + for (const fileIDStr of Object.keys(unit.importReplacements)) { |
| 150 | + const fileID = fileIDStr as ProjectFileID; |
| 151 | + importReplacements[fileID] = unit.importReplacements[fileID]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return confirmAsSerializable({ |
| 156 | + ngStyleReplacements: [...unitA.ngStyleReplacements, ...unitB.ngStyleReplacements], |
| 157 | + importReplacements, |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + override async globalMeta( |
| 162 | + combinedData: NgStyleCompilationUnitData, |
| 163 | + ): Promise<Serializable<NgStyleCompilationUnitData>> { |
| 164 | + return confirmAsSerializable({ |
| 165 | + ngStyleReplacements: combinedData.ngStyleReplacements, |
| 166 | + importReplacements: combinedData.importReplacements, |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + override async stats(globalMetadata: NgStyleCompilationUnitData) { |
| 171 | + const touchedFilesCount = globalMetadata.ngStyleReplacements.length; |
| 172 | + const replacementCount = globalMetadata.ngStyleReplacements.reduce( |
| 173 | + (acc, cur) => acc + cur.replacementCount, |
| 174 | + 0, |
| 175 | + ); |
| 176 | + |
| 177 | + return confirmAsSerializable({ |
| 178 | + touchedFilesCount, |
| 179 | + replacementCount, |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + override async migrate(globalData: NgStyleCompilationUnitData) { |
| 184 | + const replacements: Replacement[] = []; |
| 185 | + |
| 186 | + replacements.push(...globalData.ngStyleReplacements.flatMap(({replacements}) => replacements)); |
| 187 | + |
| 188 | + for (const fileIDStr of Object.keys(globalData.importReplacements)) { |
| 189 | + const fileID = fileIDStr as ProjectFileID; |
| 190 | + const importReplacements = globalData.importReplacements[fileID]; |
| 191 | + replacements.push(...importReplacements.addAndRemove); |
| 192 | + } |
| 193 | + |
| 194 | + return {replacements}; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function prepareTextReplacement( |
| 199 | + file: ProjectFile, |
| 200 | + replacement: string, |
| 201 | + start: number, |
| 202 | + end: number, |
| 203 | +): Replacement { |
| 204 | + return new Replacement( |
| 205 | + file, |
| 206 | + new TextUpdate({ |
| 207 | + position: start, |
| 208 | + end: end, |
| 209 | + toInsert: replacement, |
| 210 | + }), |
| 211 | + ); |
| 212 | +} |
0 commit comments