Skip to content

Commit bd2b454

Browse files
devversionthePunderWoman
authored andcommitted
refactor(migrations): gracefully proceed if reference cannot be resolved (#61426)
A runtime error can surface when TypeScript internally fails to resolve a reference that is named similar to an input, but no `.d.ts` is available for it. See example error: microsoft/TypeScript#61473 (comment). PR Close #61426
1 parent 9aedd30 commit bd2b454

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

packages/core/schematics/migrations/signal-migration/src/passes/reference_resolution/identify_ts_references.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,34 @@ export function identifyPotentialTypeScriptReference<D extends ClassFieldDescrip
4848

4949
let target: ts.Symbol | undefined = undefined;
5050

51-
// Resolve binding elements to their declaration symbol.
52-
// Commonly inputs are accessed via object expansion. e.g. `const {input} = this;`.
53-
if (ts.isBindingElement(node.parent)) {
54-
// Skip binding elements that are using spread.
55-
if (node.parent.dotDotDotToken !== undefined) {
56-
return;
57-
}
51+
try {
52+
// Resolve binding elements to their declaration symbol.
53+
// Commonly inputs are accessed via object expansion. e.g. `const {input} = this;`.
54+
if (ts.isBindingElement(node.parent)) {
55+
// Skip binding elements that are using spread.
56+
if (node.parent.dotDotDotToken !== undefined) {
57+
return;
58+
}
5859

59-
const bindingInfo = resolveBindingElement(node.parent);
60-
if (bindingInfo === null) {
61-
// The declaration could not be resolved. Skip analyzing this.
62-
return;
60+
const bindingInfo = resolveBindingElement(node.parent);
61+
if (bindingInfo === null) {
62+
// The declaration could not be resolved. Skip analyzing this.
63+
return;
64+
}
65+
66+
const bindingType = checker.getTypeAtLocation(bindingInfo.pattern);
67+
const resolved = lookupPropertyAccess(checker, bindingType, [bindingInfo.propertyName]);
68+
target = resolved?.symbol;
69+
} else {
70+
target = checker.getSymbolAtLocation(node);
6371
}
72+
} catch (e) {
73+
console.error('Unexpected error while trying to resolve identifier reference:');
74+
console.error(e);
6475

65-
const bindingType = checker.getTypeAtLocation(bindingInfo.pattern);
66-
const resolved = lookupPropertyAccess(checker, bindingType, [bindingInfo.propertyName]);
67-
target = resolved?.symbol;
68-
} else {
69-
target = checker.getSymbolAtLocation(node);
76+
// Gracefully skip analyzing. This can happen when e.g. a reference is named similar
77+
// to an input, but is dependant on `.d.ts` that is not necessarily available (clutz dts).
78+
return;
7079
}
7180

7281
noTargetSymbolCheck: if (target === undefined) {

0 commit comments

Comments
 (0)