Skip to content

Commit f2758db

Browse files
committed
Fixes missing nested object destrucuturing variable references
1 parent 5c69140 commit f2758db

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/services/importTracker.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,10 @@ namespace ts.FindAllReferences {
577577
// If a reference is a class expression, the exported node would be its parent.
578578
// If a reference is a variable declaration, the exported node would be the variable statement.
579579
function getExportNode(parent: Node, node: Node): Node | undefined {
580-
if (parent.kind === SyntaxKind.VariableDeclaration) {
581-
const p = parent as VariableDeclaration;
582-
return p.name !== node ? undefined :
583-
p.parent.kind === SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
584-
}
585-
else if (parent.parent && parent.parent.parent && parent.parent.parent.kind === SyntaxKind.VariableDeclaration) {
586-
const p = parent.parent.parent as VariableDeclaration;
587-
const binded = parent as BindingElement;
588-
return binded.name !== node ? undefined :
589-
p.parent.kind === SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
580+
const declaration = isVariableDeclaration(parent) ? parent : isBindingElement(parent) ? walkUpBindingElementsAndPatterns(parent) : undefined;
581+
if (declaration) {
582+
return (parent as VariableDeclaration | BindingElement).name !== node ? undefined :
583+
isCatchClause(declaration.parent) ? undefined : isVariableStatement(declaration.parent.parent) ? declaration.parent.parent : undefined;
590584
}
591585
else {
592586
return parent;

src/testRunner/unittests/tsserver/getExportReferences.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ namespace ts.projectSystem {
33
const exportVariable = "export const value = 0;";
44
const exportArrayDestructured = "export const [valueA, valueB] = [0, 1];";
55
const exportObjectDestructured = "export const { valueC, valueD: renamedD } = { valueC: 0, valueD: 1 };";
6+
const exportNestedObject = "export const { nest: [valueE, { valueF }] } = { nest: [0, { valueF: 1 }] };";
67

78
const mainTs: File = {
89
path: "/main.ts",
9-
content: 'import { value, valueA, valueB, valueC, renamedD } from "./mod";',
10+
content: 'import { value, valueA, valueB, valueC, renamedD, valueE, valueF } from "./mod";',
1011
};
1112
const modTs: File = {
1213
path: "/mod.ts",
1314
content: `${exportVariable}
1415
${exportArrayDestructured}
1516
${exportObjectDestructured}
17+
${exportNestedObject}
1618
`,
1719
};
1820
const tsconfig: File = {
@@ -146,5 +148,38 @@ ${exportObjectDestructured}
146148

147149
assert.deepEqual(response, expectResponse);
148150
});
151+
152+
it("should get nested object decralation references", () => {
153+
const session = makeSampleSession();
154+
const response = executeSessionRequest<protocol.ReferencesRequest, protocol.ReferencesResponse>(
155+
session,
156+
protocol.CommandTypes.References,
157+
protocolFileLocationFromSubstring(modTs, "valueF"),
158+
);
159+
160+
const expectResponse = {
161+
refs: [
162+
referenceModTs({
163+
text: "valueF",
164+
lineText: exportNestedObject,
165+
contextText: exportNestedObject,
166+
}),
167+
referenceMainTs(mainTs, "valueF"),
168+
referenceModTs(
169+
{
170+
text: "valueF",
171+
lineText: exportNestedObject,
172+
contextText: "valueF: 1",
173+
},
174+
{ options: { index: 1 } },
175+
),
176+
],
177+
symbolDisplayString: "const valueF: number",
178+
symbolName: "valueF",
179+
symbolStartOffset: protocolLocationFromSubstring(modTs.content, "valueF").offset,
180+
};
181+
182+
assert.deepEqual(response, expectResponse);
183+
});
149184
});
150185
}

0 commit comments

Comments
 (0)