Skip to content

Commit d25baf1

Browse files
committed
Handle destructuring in control flow reference matching
1 parent 3d8668c commit d25baf1

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14615,15 +14615,15 @@ namespace ts {
1461514615
getAccessedPropertyName(source as PropertyAccessExpression | ElementAccessExpression) === getAccessedPropertyName(target) &&
1461614616
isMatchingReference((source as PropertyAccessExpression | ElementAccessExpression).expression, target.expression);
1461714617
case SyntaxKind.BindingElement:
14618-
if (target.kind !== SyntaxKind.PropertyAccessExpression) return false;
14619-
const t = target as PropertyAccessExpression;
14620-
if (t.name.escapedText !== getBindingElementNameText(source as BindingElement)) return false;
14621-
if (source.parent.parent.kind === SyntaxKind.BindingElement && isMatchingReference(source.parent.parent, t.expression)) {
14622-
return true;
14623-
}
14624-
if (source.parent.parent.kind === SyntaxKind.VariableDeclaration) {
14625-
const maybeId = (source.parent.parent as VariableDeclaration).initializer;
14626-
return !!maybeId && isMatchingReference(maybeId, t.expression);
14618+
if (target.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>target).name.escapedText === getBindingElementNameText(<BindingElement>source)) {
14619+
const ancestor = source.parent.parent;
14620+
if (ancestor.kind === SyntaxKind.BindingElement) {
14621+
return isMatchingReference(ancestor, (<PropertyAccessExpression>target).expression);
14622+
}
14623+
if (ancestor.kind === SyntaxKind.VariableDeclaration) {
14624+
const initializer = (<VariableDeclaration>ancestor).initializer;
14625+
return !!initializer && isMatchingReference(initializer, (<PropertyAccessExpression>target).expression);
14626+
}
1462714627
}
1462814628
}
1462914629
return false;
@@ -14635,14 +14635,25 @@ namespace ts {
1463514635
undefined;
1463614636
}
1463714637

14638+
function getReferenceParent(source: Node) {
14639+
if (source.kind === SyntaxKind.PropertyAccessExpression) {
14640+
return (<PropertyAccessExpression>source).expression;
14641+
}
14642+
if (source.kind === SyntaxKind.BindingElement) {
14643+
const ancestor = source.parent.parent;
14644+
return ancestor.kind === SyntaxKind.VariableDeclaration ? (<VariableDeclaration>ancestor).initializer : ancestor;
14645+
}
14646+
return undefined;
14647+
}
14648+
1463814649
function containsMatchingReference(source: Node, target: Node) {
14639-
while (source.kind === SyntaxKind.PropertyAccessExpression) {
14640-
source = (<PropertyAccessExpression>source).expression;
14641-
if (isMatchingReference(source, target)) {
14650+
let parent = getReferenceParent(source);
14651+
while (parent) {
14652+
if (isMatchingReference(parent, target)) {
1464214653
return true;
1464314654
}
14655+
parent = getReferenceParent(parent);
1464414656
}
14645-
return false;
1464614657
}
1464714658

1464814659
// Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared

0 commit comments

Comments
 (0)