Skip to content

Commit dfb613c

Browse files
committed
Use for-of declaration list + expression as span for preview
1 parent b61813e commit dfb613c

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/services/findAllReferences.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ namespace ts.FindAllReferences {
1616
export const enum EntryKind { Span, Node, StringLiteral, SearchedLocalFoundProperty, SearchedPropertyFoundLocal }
1717
export type NodeEntryKind = EntryKind.Node | EntryKind.StringLiteral | EntryKind.SearchedLocalFoundProperty | EntryKind.SearchedPropertyFoundLocal;
1818
export type Entry = NodeEntry | SpanEntry;
19+
export interface DeclarationNodeWithStartAndEnd {
20+
start: Node;
21+
end: Node;
22+
}
23+
export type DeclarationNode = Node | DeclarationNodeWithStartAndEnd;
1924
export interface NodeEntry {
2025
readonly kind: NodeEntryKind;
2126
readonly node: Node;
22-
readonly declaration?: Node;
27+
readonly declaration?: DeclarationNode;
2328
}
2429
export interface SpanEntry {
2530
readonly kind: EntryKind.Span;
@@ -34,7 +39,11 @@ namespace ts.FindAllReferences {
3439
};
3540
}
3641

37-
function getDeclarationForDeclarationSpanForNode(node: Node): Node | undefined {
42+
export function isDeclarationNodeWithStartAndEnd(node: DeclarationNode): node is DeclarationNodeWithStartAndEnd {
43+
return node && (node as Node).kind === undefined;
44+
}
45+
46+
function getDeclarationForDeclarationSpanForNode(node: Node): DeclarationNode | undefined {
3847
if (isDeclaration(node)) {
3948
return getDeclarationForDeclarationSpan(node);
4049
}
@@ -72,14 +81,16 @@ namespace ts.FindAllReferences {
7281
return undefined;
7382
}
7483

75-
export function getDeclarationForDeclarationSpan(node: NamedDeclaration | BinaryExpression | undefined): Node | undefined {
84+
export function getDeclarationForDeclarationSpan(node: NamedDeclaration | BinaryExpression | undefined): DeclarationNode | undefined {
7685
if (!node) return undefined;
7786
switch (node.kind) {
7887
case SyntaxKind.VariableDeclaration:
7988
return !isVariableDeclarationList(node.parent) || node.parent.declarations.length !== 1 ?
8089
node :
8190
isVariableStatement(node.parent.parent) ?
8291
node.parent.parent :
92+
isForInOrOfStatement(node.parent.parent) ?
93+
{ start: node.parent.parent.initializer, end: node.parent.parent.expression } :
8394
node.parent;
8495

8596
case SyntaxKind.BindingElement:
@@ -257,7 +268,9 @@ namespace ts.FindAllReferences {
257268
displayParts
258269
};
259270
if (declaration) {
260-
result.declarationSpan = getTextSpan(declaration, sourceFile);
271+
result.declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ?
272+
getTextSpan(declaration.start, sourceFile, declaration.end) :
273+
getTextSpan(declaration, sourceFile);
261274
}
262275
return result;
263276
}
@@ -298,7 +311,9 @@ namespace ts.FindAllReferences {
298311
const sourceFile = entry.node.getSourceFile();
299312
const result: DocumentSpan = { textSpan: getTextSpan(entry.node, sourceFile), fileName: sourceFile.fileName };
300313
if (entry.declaration) {
301-
result.declarationSpan = getTextSpan(entry.declaration, sourceFile);
314+
result.declarationSpan = isDeclarationNodeWithStartAndEnd(entry.declaration) ?
315+
getTextSpan(entry.declaration.start, sourceFile, entry.declaration.end) :
316+
getTextSpan(entry.declaration, sourceFile);
302317
}
303318
return result;
304319
}
@@ -392,10 +407,11 @@ namespace ts.FindAllReferences {
392407
return { fileName: documentSpan.fileName, span };
393408
}
394409

395-
function getTextSpan(node: Node, sourceFile: SourceFile): TextSpan {
410+
function getTextSpan(node: Node, sourceFile: SourceFile, endNode?: Node): TextSpan {
396411
let start = node.getStart(sourceFile);
397-
let end = node.getEnd();
412+
let end = (endNode || node).getEnd();
398413
if (node.kind === SyntaxKind.StringLiteral) {
414+
Debug.assert(endNode === undefined);
399415
start += 1;
400416
end -= 1;
401417
}

src/services/goToDefinition.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,17 @@ namespace ts.GoToDefinition {
273273
function createDefinitionInfoFromName(declaration: Declaration, symbolKind: ScriptElementKind, symbolName: string, containerName: string): DefinitionInfo {
274274
const name = getNameOfDeclaration(declaration) || declaration;
275275
const sourceFile = name.getSourceFile();
276+
const declarationNode = FindAllReferences.getDeclarationForDeclarationSpan(declaration)!;
276277
return {
277278
fileName: sourceFile.fileName,
278279
textSpan: createTextSpanFromNode(name, sourceFile),
279280
kind: symbolKind,
280281
name: symbolName,
281282
containerKind: undefined!, // TODO: GH#18217
282283
containerName,
283-
declarationSpan: createTextSpanFromNode(
284-
FindAllReferences.getDeclarationForDeclarationSpan(declaration)!,
285-
sourceFile
286-
)
284+
declarationSpan: FindAllReferences.isDeclarationNodeWithStartAndEnd(declarationNode) ?
285+
createTextSpanFromNode(declarationNode.start, sourceFile, declarationNode.end) :
286+
createTextSpanFromNode(declarationNode, sourceFile),
287287
};
288288
}
289289

src/services/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ namespace ts {
11901190
return !!range && shouldBeReference === tripleSlashDirectivePrefixRegex.test(sourceFile.text.substring(range.pos, range.end));
11911191
}
11921192

1193-
export function createTextSpanFromNode(node: Node, sourceFile?: SourceFile): TextSpan {
1194-
return createTextSpanFromBounds(node.getStart(sourceFile), node.getEnd());
1193+
export function createTextSpanFromNode(node: Node, sourceFile?: SourceFile, endNode?: Node): TextSpan {
1194+
return createTextSpanFromBounds(node.getStart(sourceFile), (endNode || node).getEnd());
11951195
}
11961196

11971197
export function createTextRangeFromNode(node: Node, sourceFile: SourceFile): TextRange {

tests/cases/fourslash/renameDestructuringNestedBindingElement.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
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-
////for (let { skills: {[|primary|]: primaryA, secondary: secondaryA } } of multiRobots) {
11+
////for ([|let { skills: {[|{| "declarationRangeIndex": 2|}primary|]: primaryA, secondary: secondaryA } } of multiRobots|]) {
1212
//// console.log(primaryA);
1313
////}
14-
////for (let { skills: {[|primary|], secondary } } of multiRobots) {
14+
////for ([|let { skills: {[|{| "declarationRangeIndex": 4|}primary|], secondary } } of multiRobots|]) {
1515
//// console.log([|primary|]);
1616
////}
1717

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

0 commit comments

Comments
 (0)