|
| 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {CompilerOptions} from '@angular/compiler-cli'; |
| 10 | +import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core'; |
| 11 | +import {MetaKind} from '@angular/compiler-cli/src/ngtsc/metadata'; |
| 12 | +import { |
| 13 | + ClassIncompatibilityReason, |
| 14 | + InputIncompatibilityReason, |
| 15 | +} from '@angular/core/schematics/migrations/signal-migration/src/input_detection/incompatibility'; |
| 16 | +import {ApplyRefactoringProgressFn} from '@angular/language-service/api'; |
| 17 | +import ts from 'typescript'; |
| 18 | +import { |
| 19 | + InputNode, |
| 20 | + isInputContainerNode, |
| 21 | +} from '../../../core/schematics/migrations/signal-migration/src/input_detection/input_node'; |
| 22 | +import {KnownInputInfo} from '../../../core/schematics/migrations/signal-migration/src/input_detection/known_inputs'; |
| 23 | +import {SignalInputMigration} from '../../../core/schematics/migrations/signal-migration/src/migration'; |
| 24 | +import { |
| 25 | + getInputDescriptor, |
| 26 | + isInputDescriptor, |
| 27 | +} from '../../../core/schematics/migrations/signal-migration/src/utils/input_id'; |
| 28 | +import {groupReplacementsByFile} from '../../../core/schematics/utils/tsurge/helpers/group_replacements'; |
| 29 | +import {findTightestNode, getParentClassDeclaration} from '../utils/ts_utils'; |
| 30 | +import {isTypeScriptFile} from '../utils'; |
| 31 | +import type {Refactoring} from './refactoring'; |
| 32 | + |
| 33 | +/** |
| 34 | + * Language service refactoring action that can convert `@Input()` |
| 35 | + * declarations to signal inputs. |
| 36 | + * |
| 37 | + * The user can click on an `@Input` property declaration in e.g. the VSCode |
| 38 | + * extension and ask for the input to be migrated. All references, imports and |
| 39 | + * the declaration are updated automatically. |
| 40 | + */ |
| 41 | +export class ConvertToSignalInputRefactoring implements Refactoring { |
| 42 | + id = 'convert-to-signal-input'; |
| 43 | + description = '(experimental fixer): Convert @Input() to a signal input'; |
| 44 | + |
| 45 | + migration: SignalInputMigration | null = null; |
| 46 | + |
| 47 | + isApplicable( |
| 48 | + compiler: NgCompiler, |
| 49 | + fileName: string, |
| 50 | + positionOrRange: number | ts.TextRange, |
| 51 | + ): boolean { |
| 52 | + if (!isTypeScriptFile(fileName)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + const sf = compiler.getCurrentProgram().getSourceFile(fileName); |
| 57 | + if (sf === undefined) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + const start = typeof positionOrRange === 'number' ? positionOrRange : positionOrRange.pos; |
| 62 | + const node = findTightestNode(sf, start); |
| 63 | + if (node === undefined) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + const classDecl = getParentClassDeclaration(node); |
| 68 | + if (classDecl === undefined) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + const meta = compiler.getMeta(classDecl); |
| 73 | + if (meta === undefined || meta?.kind !== MetaKind.Directive) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + const containingProp = findParentPropertyDeclaration(node); |
| 78 | + if (containingProp === null) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (!ts.isIdentifier(containingProp.name) && !ts.isStringLiteralLike(containingProp.name)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + const inputMeta = meta.inputs.getByClassPropertyName(containingProp.name.text); |
| 86 | + if (inputMeta === null || inputMeta.isSignal) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + async computeEditsForFix( |
| 93 | + compiler: NgCompiler, |
| 94 | + compilerOptions: CompilerOptions, |
| 95 | + fileName: string, |
| 96 | + positionOrRange: number | ts.TextRange, |
| 97 | + reportProgress: ApplyRefactoringProgressFn, |
| 98 | + ): Promise<ts.RefactorEditInfo> { |
| 99 | + const sf = compiler.getCurrentProgram().getSourceFile(fileName); |
| 100 | + if (sf === undefined) { |
| 101 | + return {edits: []}; |
| 102 | + } |
| 103 | + |
| 104 | + const start = typeof positionOrRange === 'number' ? positionOrRange : positionOrRange.pos; |
| 105 | + const node = findTightestNode(sf, start); |
| 106 | + if (node === undefined) { |
| 107 | + return {edits: []}; |
| 108 | + } |
| 109 | + |
| 110 | + const containingProp = findParentPropertyDeclaration(node); |
| 111 | + if (containingProp === null || !isInputContainerNode(containingProp)) { |
| 112 | + return {edits: [], notApplicableReason: 'Not an input property.'}; |
| 113 | + } |
| 114 | + reportProgress(0, 'Starting input migration. Analyzing..'); |
| 115 | + |
| 116 | + // TS incorrectly narrows to `null` if we don't explicitly widen the type. |
| 117 | + // See: https://github.com/microsoft/TypeScript/issues/11498. |
| 118 | + let targetInput: KnownInputInfo | null = null as KnownInputInfo | null; |
| 119 | + |
| 120 | + this.migration ??= new SignalInputMigration(); |
| 121 | + this.migration.upgradeAnalysisPhaseToAvoidBatch = true; |
| 122 | + this.migration.reportProgressFn = reportProgress; |
| 123 | + this.migration.beforeMigrateHook = getBeforeMigrateHookToFilterAllUnrelatedInputs( |
| 124 | + containingProp, |
| 125 | + (i) => (targetInput = i), |
| 126 | + ); |
| 127 | + |
| 128 | + await this.migration.analyze( |
| 129 | + this.migration.prepareProgram({ |
| 130 | + ngCompiler: compiler, |
| 131 | + program: compiler.getCurrentProgram(), |
| 132 | + userOptions: compilerOptions, |
| 133 | + programAbsoluteRootPaths: [], |
| 134 | + tsconfigAbsolutePath: '', |
| 135 | + }), |
| 136 | + ); |
| 137 | + |
| 138 | + if (this.migration.upgradedAnalysisPhaseResults === null || targetInput === null) { |
| 139 | + return { |
| 140 | + edits: [], |
| 141 | + notApplicableReason: 'Unexpected error. No edits could be computed.', |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + // Check for incompatibility, and report if it prevented migration. |
| 146 | + if (targetInput.isIncompatible()) { |
| 147 | + const {container, descriptor} = targetInput; |
| 148 | + const memberIncompatibility = container.memberIncompatibility.get(descriptor.key); |
| 149 | + const classIncompatibility = container.incompatible; |
| 150 | + |
| 151 | + return { |
| 152 | + edits: [], |
| 153 | + // TODO: Output a better human-readable message here. For now this is better than a noop. |
| 154 | + notApplicableReason: `Input cannot be migrated: ${ |
| 155 | + memberIncompatibility !== undefined |
| 156 | + ? InputIncompatibilityReason[memberIncompatibility.reason] |
| 157 | + : classIncompatibility !== null |
| 158 | + ? ClassIncompatibilityReason[classIncompatibility] |
| 159 | + : 'unknown' |
| 160 | + }`, |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + const edits: ts.FileTextChanges[] = Array.from( |
| 165 | + groupReplacementsByFile(this.migration.upgradedAnalysisPhaseResults).entries(), |
| 166 | + ).map(([fileName, changes]) => { |
| 167 | + return { |
| 168 | + fileName, |
| 169 | + textChanges: changes.map((c) => ({ |
| 170 | + newText: c.data.toInsert, |
| 171 | + span: { |
| 172 | + start: c.data.position, |
| 173 | + length: c.data.end - c.data.position, |
| 174 | + }, |
| 175 | + })), |
| 176 | + }; |
| 177 | + }); |
| 178 | + |
| 179 | + if (edits.length === 0) { |
| 180 | + return { |
| 181 | + edits: [], |
| 182 | + notApplicableReason: 'No edits were generated. Consider reporting this as a bug.', |
| 183 | + }; |
| 184 | + } |
| 185 | + |
| 186 | + return {edits}; |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +function findParentPropertyDeclaration(node: ts.Node): ts.PropertyDeclaration | null { |
| 191 | + while (!ts.isPropertyDeclaration(node) && !ts.isSourceFile(node)) { |
| 192 | + node = node.parent; |
| 193 | + } |
| 194 | + if (ts.isSourceFile(node)) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + return node; |
| 198 | +} |
| 199 | + |
| 200 | +function getBeforeMigrateHookToFilterAllUnrelatedInputs( |
| 201 | + containingProp: InputNode, |
| 202 | + setTargetInput: (i: KnownInputInfo) => void, |
| 203 | +): SignalInputMigration['beforeMigrateHook'] { |
| 204 | + return (host, knownInputs, result) => { |
| 205 | + const {key} = getInputDescriptor(host, containingProp); |
| 206 | + const targetInput = knownInputs.get({key}); |
| 207 | + |
| 208 | + if (targetInput === undefined) { |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + setTargetInput(targetInput); |
| 213 | + |
| 214 | + // Mark all other inputs as incompatible. |
| 215 | + // Note that we still analyzed the whole application for potential references. |
| 216 | + // Only migrate references to the target input. |
| 217 | + for (const input of result.sourceInputs.keys()) { |
| 218 | + if (input.key !== key) { |
| 219 | + knownInputs.markInputAsIncompatible(input, { |
| 220 | + context: null, |
| 221 | + reason: InputIncompatibilityReason.IgnoredBecauseOfLanguageServiceRefactoringRange, |
| 222 | + }); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + result.references = result.references.filter( |
| 227 | + // Note: References to the whole class are not migrated as we are not migrating all inputs. |
| 228 | + // We can revisit this at a later time. |
| 229 | + (r) => isInputDescriptor(r.target) && r.target.key === key, |
| 230 | + ); |
| 231 | + }; |
| 232 | +} |
0 commit comments