Skip to content

Commit 0fee3b0

Browse files
committed
Handle destructuring assignments
1 parent c0537d9 commit 0fee3b0

9 files changed

+59
-44
lines changed

src/services/findAllReferences.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace ts.FindAllReferences {
8181
return undefined;
8282
}
8383

84-
export function getDeclarationForDeclarationSpan(node: NamedDeclaration | BinaryExpression | undefined): DeclarationNode | undefined {
84+
export function getDeclarationForDeclarationSpan(node: NamedDeclaration | BinaryExpression | ForInOrOfStatement | undefined): DeclarationNode | undefined {
8585
if (!node) return undefined;
8686
switch (node.kind) {
8787
case SyntaxKind.VariableDeclaration:
@@ -90,8 +90,8 @@ namespace ts.FindAllReferences {
9090
isVariableStatement(node.parent.parent) ?
9191
node.parent.parent :
9292
isForInOrOfStatement(node.parent.parent) ?
93-
{ start: node.parent.parent.initializer, end: node.parent.parent.expression } :
94-
node.parent;
93+
getDeclarationForDeclarationSpan(node.parent.parent) :
94+
node.parent;
9595

9696
case SyntaxKind.BindingElement:
9797
return getDeclarationForDeclarationSpan(node.parent.parent as NamedDeclaration);
@@ -114,12 +114,27 @@ namespace ts.FindAllReferences {
114114
case SyntaxKind.BinaryExpression:
115115
return isExpressionStatement(node.parent) ?
116116
node.parent :
117-
undefined;
117+
node;
118+
119+
case SyntaxKind.ForOfStatement:
120+
case SyntaxKind.ForInStatement:
121+
return {
122+
start: (node as ForInOrOfStatement).initializer,
123+
end: (node as ForInOrOfStatement).expression
124+
};
118125

119-
// Not really interesting definition
126+
case SyntaxKind.PropertyAssignment:
120127
// TODO(shkamat):: Should we show whole object literal instead?
121128
case SyntaxKind.ShorthandPropertyAssignment:
122-
return undefined;
129+
return isArrayLiteralOrObjectLiteralDestructuringPattern(node.parent) ?
130+
getDeclarationForDeclarationSpan(
131+
findAncestor(node.parent, node =>
132+
isBinaryExpression(node) || isForInOrOfStatement(node)
133+
) as BinaryExpression | ForInOrOfStatement
134+
) :
135+
node.kind === SyntaxKind.PropertyAssignment ?
136+
node :
137+
undefined;
123138

124139
default:
125140
return node;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// <reference path='fourslash.ts' />
22

33
////interface I {
4-
//// [|x|]: number;
4+
//// [|[|{| "declarationRangeIndex": 0 |}x|]: number;|]
55
////}
66
////var a: I;
77
////var x;
8-
////({ [|x|]: x } = a);
8+
////([|{ [|{| "declarationRangeIndex": 2 |}x|]: x } = a|]);
99

10-
verify.rangesAreRenameLocations();
10+
verify.rangesAreRenameLocations(test.rangesByText().get("x"));
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/// <reference path='fourslash.ts' />
22

33
////interface I {
4-
//// [|property1|]: number;
4+
//// [|[|{| "declarationRangeIndex": 0 |}property1|]: number;|]
55
//// property2: string;
66
////}
77
////var elems: I[];
88
////
9-
////var p2: number, [|property1|]: number;
10-
////for ({ [|property1|] } = elems[0]; p2 < 100; p2++) {
9+
////var p2: number, [|[|{| "declarationRangeIndex": 2 |}property1|]: number|];
10+
////for ([|{ [|{| "declarationRangeIndex": 4 |}property1|] } = elems[0]|]; p2 < 100; p2++) {
1111
//// p2 = [|property1|]++;
1212
////}
13-
////for ({ [|property1|]: p2 } = elems[0]; p2 < 100; p2++) {
13+
////for ([|{ [|{| "declarationRangeIndex": 7 |}property1|]: p2 } = elems[0]|]; p2 < 100; p2++) {
1414
////}
1515

1616
verify.noErrors();
1717
const ranges = test.ranges();
18-
const [r0, r1, r2, r3, r4] = ranges;
18+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3, r4Def, r4] = ranges;
1919
verify.renameLocations([r0, r4], [r0, { range: r2, suffixText: ": property1" }, r4]);
2020
verify.renameLocations([r1, r2, r3], [r1, { range: r2, prefixText: "property1: " }, r3]);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/// <reference path='fourslash.ts' />
22

33
////interface I {
4-
//// [|property1|]: number;
4+
//// [|[|{| "declarationRangeIndex": 0 |}property1|]: number;|]
55
//// property2: string;
66
////}
77
////var elems: I[];
88
////
9-
////var [|property1|]: number, p2: number;
10-
////for ({ [|property1|] } of elems) {
9+
////var [|[|{| "declarationRangeIndex": 2 |}property1|]: number|], p2: number;
10+
////for ([|{ [|{| "declarationRangeIndex": 4 |}property1|] } of elems|]) {
1111
//// [|property1|]++;
1212
////}
13-
////for ({ [|property1|]: p2 } of elems) {
13+
////for ([|{ [|{| "declarationRangeIndex": 7 |}property1|]: p2 } of elems|]) {
1414
////}
1515

1616
verify.noErrors();
1717
const ranges = test.ranges();
18-
const [r0, r1, r2, r3, r4] = ranges;
18+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3, r4Def, r4] = ranges;
1919
verify.renameLocations([r0, r4], [r0, { range: r2, suffixText: ": property1" }, r4]);
2020
verify.renameLocations([r1, r2, r3], [r1, { range: r2, prefixText: "property1: " }, r3]);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/// <reference path='fourslash.ts' />
22

33
////interface I {
4-
//// [|property1|]: number;
4+
//// [|[|{| "declarationRangeIndex": 0 |}property1|]: number;|]
55
//// property2: string;
66
////}
7-
////var elems: I[], p1: number, [|property1|]: number;
8-
////[{ [|property1|]: p1 }] = elems;
9-
////[{ [|property1|] }] = elems;
7+
////var elems: I[], p1: number, [|[|{| "declarationRangeIndex": 2 |}property1|]: number|];
8+
////[|[{ [|{| "declarationRangeIndex": 4 |}property1|]: p1 }] = elems;|]
9+
////[|[{ [|{| "declarationRangeIndex": 6 |}property1|] }] = elems;|]
1010

1111
const ranges = test.ranges();
12-
const [r0, r1, r2, r3] = ranges;
12+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3Def, r3] = ranges;
1313
verify.renameLocations([r0, r2], [r0, r2, { range: r3, suffixText: ": property1" }]);
1414
verify.renameLocations([r1, r3], [r1, { range: r3, prefixText: "property1: " }]);
1515

tests/cases/fourslash/renameDestructuringAssignmentNestedInFor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
////interface MultiRobot {
44
//// name: string;
55
//// skills: {
6-
//// [|primary|]: string;
6+
//// [|[|{| "declarationRangeIndex": 0 |}primary|]: string;|]
77
//// secondary: string;
88
//// };
99
////}
10-
////let multiRobot: MultiRobot, [|primary|]: string, secondary: string, primaryA: string, secondaryA: string, i: number;
11-
////for ({ skills: { [|primary|]: primaryA, secondary: secondaryA } } = multiRobot, i = 0; i < 1; i++) {
10+
////let multiRobot: MultiRobot, [|[|{| "declarationRangeIndex": 2 |}primary|]: string|], secondary: string, primaryA: string, secondaryA: string, i: number;
11+
////for ([|{ skills: { [|{| "declarationRangeIndex": 4 |}primary|]: primaryA, secondary: secondaryA } } = multiRobot|], i = 0; i < 1; i++) {
1212
//// primaryA;
1313
////}
14-
////for ({ skills: { [|primary|], secondary } } = multiRobot, i = 0; i < 1; i++) {
14+
////for ([|{ skills: { [|{| "declarationRangeIndex": 6 |}primary|], secondary } } = multiRobot|], i = 0; i < 1; i++) {
1515
//// [|primary|];
1616
////}
1717

1818
verify.noErrors();
1919
const ranges = test.ranges();
20-
const [r0, r1, r2, r3, r4] = ranges;
20+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3Def, r3, r4] = ranges;
2121
verify.renameLocations([r0, r2], [r0, r2, { range: r3, suffixText: ": primary" }]);
2222
verify.renameLocations([r1, r3, r4], [r1, { range: r3, prefixText: "primary: " }, r4]);

tests/cases/fourslash/renameDestructuringAssignmentNestedInFor2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
////interface MultiRobot {
44
//// name: string;
55
//// skills: {
6-
//// [|primary|]: string;
6+
//// [|[|{| "declarationRangeIndex": 0 |}primary|]: string;|]
77
//// secondary: string;
88
//// };
99
////}
10-
////let multiRobot: MultiRobot, [|primary|]: string, secondary: string, primaryA: string, secondaryA: string, i: number;
11-
////for ({ skills: { [|primary|]: primaryA, secondary: secondaryA } } = multiRobot, i = 0; i < 1; i++) {
10+
////let multiRobot: MultiRobot, [|[|{| "declarationRangeIndex": 2 |}primary|]: string|], secondary: string, primaryA: string, secondaryA: string, i: number;
11+
////for ([|{ skills: { [|{| "declarationRangeIndex": 4 |}primary|]: primaryA, secondary: secondaryA } } = multiRobot|], i = 0; i < 1; i++) {
1212
//// primaryA;
1313
////}
14-
////for ({ skills: { [|primary|], secondary } } = multiRobot, i = 0; i < 1; i++) {
14+
////for ([|{ skills: { [|{| "declarationRangeIndex": 6 |}primary|], secondary } } = multiRobot|], i = 0; i < 1; i++) {
1515
//// [|primary|];
1616
////}
1717

1818
verify.noErrors();
1919
const ranges = test.ranges();
20-
const [r0, r1, r2, r3, r4] = ranges;
20+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3Def, r3, r4] = ranges;
2121
verify.renameLocations([r0, r2], [r0, r2, { range: r3, suffixText: ": primary" }]);
2222
verify.renameLocations([r1, r3, r4], [r1, { range: r3, prefixText: "primary: " }, r4]);

tests/cases/fourslash/renameDestructuringAssignmentNestedInForOf.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
////interface MultiRobot {
44
//// name: string;
55
//// skills: {
6-
//// [|primary|]: string;
6+
//// [|[|{| "declarationRangeIndex": 0 |}primary|]: string;|]
77
//// secondary: string;
88
//// };
99
////}
1010
////let multiRobots: MultiRobot[];
11-
////let [|primary|]: string, secondary: string, primaryA: string, secondaryA: string;
12-
////for ({ skills: { [|primary|]: primaryA, secondary: secondaryA } } of multiRobots) {
11+
////let [|[|{| "declarationRangeIndex": 2 |}primary|]: string|], secondary: string, primaryA: string, secondaryA: string;
12+
////for ([|{ skills: { [|{| "declarationRangeIndex": 4 |}primary|]: primaryA, secondary: secondaryA } } of multiRobots|]) {
1313
//// primaryA;
1414
////}
15-
////for ({ skills: { [|primary|], secondary } } of multiRobots) {
15+
////for ([|{ skills: { [|{| "declarationRangeIndex": 6 |}primary|], secondary } } of multiRobots|]) {
1616
//// [|primary|];
1717
////}
1818

1919
verify.noErrors();
20-
const [r0, r1, r2, r3, r4] = test.ranges();
20+
const [r0Def, r0, r1Def, r1, r2Def, r2, r3Def, r3, r4] = test.ranges();
2121
verify.renameLocations([r0, r2], [r0, r2, { range: r3, suffixText: ": primary" }]);
2222
verify.renameLocations([r1, r3, r4], [r1, { range: r3, prefixText: "primary: " }, r4])

tests/cases/fourslash/renameDestructuringAssignmentNestedInForOf2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
////interface MultiRobot {
44
//// name: string;
55
//// skills: {
6-
//// [|primary|]: string;
6+
//// [|[|{| "declarationRangeIndex": 0 |}primary|]: string;|]
77
//// secondary: string;
88
//// };
99
////}
10-
////let multiRobots: MultiRobot[], [|primary|]: string;
11-
////for ({ skills: { [|primary|]: primaryA, secondary: secondaryA } } of multiRobots) {
10+
////let multiRobots: MultiRobot[], [|[|{| "declarationRangeIndex": 2 |}primary|]: string|];
11+
////for ([|{ skills: { [|{| "declarationRangeIndex": 4 |}primary|]: primaryA, secondary: secondaryA } } of multiRobots|]) {
1212
//// console.log(primaryA);
1313
////}
14-
////for ({ skills: { [|primary|], secondary } } of multiRobots) {
14+
////for ([|{ skills: { [|{| "declarationRangeIndex": 6 |}primary|], secondary } } of multiRobots|]) {
1515
//// console.log([|primary|]);
1616
////}
1717

1818
const ranges = test.ranges();
19-
const [r0, r1, r2, r3, r4] = ranges;
19+
const [r0Def, r0, r1Def, r1,r2Def, r2, r3Def, r3, r4] = ranges;
2020
verify.renameLocations([r0, r2], [r0, r2, { range: r3, suffixText: ": primary" }]);
2121
verify.renameLocations([r1, r3, r4], [r1, { range: r3, prefixText: "primary: " }, r4]);

0 commit comments

Comments
 (0)