|
| 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 {ImportManager} from '@angular/compiler-cli/private/migrations'; |
| 10 | +import ts from 'typescript'; |
| 11 | +import { |
| 12 | + confirmAsSerializable, |
| 13 | + ProgramInfo, |
| 14 | + projectFile, |
| 15 | + Replacement, |
| 16 | + Serializable, |
| 17 | + TextUpdate, |
| 18 | + TsurgeFunnelMigration, |
| 19 | +} from '../../utils/tsurge'; |
| 20 | +import {applyImportManagerChanges} from '../../utils/tsurge/helpers/apply_import_manager'; |
| 21 | + |
| 22 | +export interface IncrementalHydrationMigrationData { |
| 23 | + replacements: Replacement[]; |
| 24 | +} |
| 25 | + |
| 26 | +export class IncrementalHydrationMigration extends TsurgeFunnelMigration< |
| 27 | + IncrementalHydrationMigrationData, |
| 28 | + IncrementalHydrationMigrationData |
| 29 | +> { |
| 30 | + override async analyze( |
| 31 | + info: ProgramInfo, |
| 32 | + ): Promise<Serializable<IncrementalHydrationMigrationData>> { |
| 33 | + const {sourceFiles, program} = info; |
| 34 | + const typeChecker = program.getTypeChecker(); |
| 35 | + const replacements: Replacement[] = []; |
| 36 | + const importManager = new ImportManager(); |
| 37 | + const printer = ts.createPrinter(); |
| 38 | + |
| 39 | + for (const sf of sourceFiles) { |
| 40 | + ts.forEachChild(sf, function visit(node: ts.Node) { |
| 41 | + if ( |
| 42 | + ts.isCallExpression(node) && |
| 43 | + ts.isIdentifier(node.expression) && |
| 44 | + node.expression.text === 'provideClientHydration' |
| 45 | + ) { |
| 46 | + let hasIncremental = false; |
| 47 | + let incrementalArgNode: ts.CallExpression | null = null; |
| 48 | + |
| 49 | + for (const arg of node.arguments) { |
| 50 | + if ( |
| 51 | + ts.isCallExpression(arg) && |
| 52 | + ts.isIdentifier(arg.expression) && |
| 53 | + arg.expression.text === 'withIncrementalHydration' |
| 54 | + ) { |
| 55 | + hasIncremental = true; |
| 56 | + incrementalArgNode = arg; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (!hasIncremental) { |
| 62 | + // Add withNoIncrementalHydration() |
| 63 | + const withNoIncrementalExpr = importManager.addImport({ |
| 64 | + exportModuleSpecifier: '@angular/platform-browser', |
| 65 | + exportSymbolName: 'withNoIncrementalHydration', |
| 66 | + requestedFile: sf, |
| 67 | + }); |
| 68 | + |
| 69 | + const exprText = printer.printNode(ts.EmitHint.Unspecified, withNoIncrementalExpr, sf); |
| 70 | + |
| 71 | + const insertPos = node.arguments.end; |
| 72 | + const toInsert = node.arguments.length > 0 ? `, ${exprText}()` : `${exprText}()`; |
| 73 | + |
| 74 | + replacements.push( |
| 75 | + new Replacement( |
| 76 | + projectFile(sf, info), |
| 77 | + new TextUpdate({ |
| 78 | + position: insertPos, |
| 79 | + end: insertPos, |
| 80 | + toInsert: toInsert, |
| 81 | + }), |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + ts.forEachChild(node, visit); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + applyImportManagerChanges(importManager, replacements, sourceFiles, info); |
| 91 | + |
| 92 | + return confirmAsSerializable({ |
| 93 | + replacements, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + override async combine( |
| 98 | + unitA: IncrementalHydrationMigrationData, |
| 99 | + unitB: IncrementalHydrationMigrationData, |
| 100 | + ): Promise<Serializable<IncrementalHydrationMigrationData>> { |
| 101 | + return confirmAsSerializable({ |
| 102 | + replacements: [...unitA.replacements, ...unitB.replacements], |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + override async globalMeta( |
| 107 | + combinedData: IncrementalHydrationMigrationData, |
| 108 | + ): Promise<Serializable<IncrementalHydrationMigrationData>> { |
| 109 | + return confirmAsSerializable(combinedData); |
| 110 | + } |
| 111 | + |
| 112 | + override async stats( |
| 113 | + globalMetadata: IncrementalHydrationMigrationData, |
| 114 | + ): Promise<Serializable<unknown>> { |
| 115 | + return confirmAsSerializable({}); |
| 116 | + } |
| 117 | + |
| 118 | + override async migrate( |
| 119 | + globalData: IncrementalHydrationMigrationData, |
| 120 | + ): Promise<{replacements: Replacement[]}> { |
| 121 | + return {replacements: globalData.replacements}; |
| 122 | + } |
| 123 | +} |
0 commit comments