Skip to content

Commit 004488c

Browse files
committed
Set declaration span only if its not same as own span
1 parent cc1cb54 commit 004488c

File tree

7 files changed

+24
-30
lines changed

7 files changed

+24
-30
lines changed

src/services/findAllReferences.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ namespace ts.FindAllReferences {
111111
case SyntaxKind.ImportClause:
112112
return node.parent;
113113

114-
case SyntaxKind.JsxAttribute:
115-
return (node as JsxAttribute).initializer === undefined ?
116-
undefined :
117-
node;
118-
119114
case SyntaxKind.BinaryExpression:
120115
return isExpressionStatement(node.parent) ?
121116
node.parent :
@@ -129,23 +124,31 @@ namespace ts.FindAllReferences {
129124
};
130125

131126
case SyntaxKind.PropertyAssignment:
132-
// TODO(shkamat):: Should we show whole object literal instead?
133127
case SyntaxKind.ShorthandPropertyAssignment:
134128
return isArrayLiteralOrObjectLiteralDestructuringPattern(node.parent) ?
135129
getDeclarationForDeclarationSpan(
136130
findAncestor(node.parent, node =>
137131
isBinaryExpression(node) || isForInOrOfStatement(node)
138132
) as BinaryExpression | ForInOrOfStatement
139133
) :
140-
node.kind === SyntaxKind.PropertyAssignment ?
141-
node :
142-
undefined;
134+
node;
143135

144136
default:
145137
return node;
146138
}
147139
}
148140

141+
export function setDeclarationSpan(span: DocumentSpan, sourceFile: SourceFile, declaration?: DeclarationNode) {
142+
if (declaration) {
143+
const declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ?
144+
getTextSpan(declaration.start, sourceFile, declaration.end) :
145+
getTextSpan(declaration, sourceFile);
146+
if (declarationSpan.start !== span.textSpan.start || declarationSpan.length !== span.textSpan.length) {
147+
span.declarationSpan = declarationSpan;
148+
}
149+
}
150+
}
151+
149152
export interface Options {
150153
readonly findInStrings?: boolean;
151154
readonly findInComments?: boolean;
@@ -287,11 +290,7 @@ namespace ts.FindAllReferences {
287290
textSpan: getTextSpan(isComputedPropertyName(node) ? node.expression : node, sourceFile),
288291
displayParts
289292
};
290-
if (declaration) {
291-
result.declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ?
292-
getTextSpan(declaration.start, sourceFile, declaration.end) :
293-
getTextSpan(declaration, sourceFile);
294-
}
293+
setDeclarationSpan(result, sourceFile, declaration);
295294
return result;
296295
}
297296

@@ -330,11 +329,7 @@ namespace ts.FindAllReferences {
330329
else {
331330
const sourceFile = entry.node.getSourceFile();
332331
const result: DocumentSpan = { textSpan: getTextSpan(entry.node, sourceFile), fileName: sourceFile.fileName };
333-
if (entry.declaration) {
334-
result.declarationSpan = isDeclarationNodeWithStartAndEnd(entry.declaration) ?
335-
getTextSpan(entry.declaration.start, sourceFile, entry.declaration.end) :
336-
getTextSpan(entry.declaration, sourceFile);
337-
}
332+
setDeclarationSpan(result, sourceFile, entry.declaration);
338333
return result;
339334
}
340335
}

src/services/goToDefinition.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,11 @@ namespace ts.GoToDefinition {
281281
containerKind: undefined!, // TODO: GH#18217
282282
containerName,
283283
};
284-
const declarationNode = FindAllReferences.getDeclarationForDeclarationSpan(declaration);
285-
if (declarationNode) {
286-
result.declarationSpan = FindAllReferences.isDeclarationNodeWithStartAndEnd(declarationNode) ?
287-
createTextSpanFromNode(declarationNode.start, sourceFile, declarationNode.end) :
288-
createTextSpanFromNode(declarationNode, sourceFile);
289-
}
284+
FindAllReferences.setDeclarationSpan(
285+
result,
286+
sourceFile,
287+
FindAllReferences.getDeclarationForDeclarationSpan(declaration)
288+
);
290289
return result;
291290
}
292291

tests/cases/fourslash/localGetReferences.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
////array.forEach(
118118
////
119119
////
120-
////function([|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 43 |}str|]) {
120+
////function([|{| "isWriteAccess": true, "isDefinition": true |}str|]) {
121121
////
122122
////
123123
////

tests/cases/fourslash/renameDestructuringFunctionParameter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts' />
22

3-
////function f([|{[|{| "declarationRangeIndex": 0 |}a|]}: {[|{| "declarationRangeIndex": 2 |}a|]}|]) {
3+
////function f([|{[|{| "declarationRangeIndex": 0 |}a|]}: {[|a|]}|]) {
44
//// f({[|a|]});
55
////}
66

tests/cases/fourslash/renameJsSpecialAssignmentRhs1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//// set: function (x) {
66
//// this._x = x;
77
//// },
8-
//// copy: function ([|{| "declarationRangeIndex": 0 |}x|]) {
8+
//// copy: function ([|x|]) {
99
//// this._x = [|x|].prop;
1010
//// }
1111
////};

tests/cases/fourslash/renameJsSpecialAssignmentRhs2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//// set: function (x) {
66
//// this._x = x;
77
//// },
8-
//// copy: function ([|{| "declarationRangeIndex": 0 |}x|]) {
8+
//// copy: function ([|x|]) {
99
//// this._x = [|x|].prop;
1010
//// }
1111
////};

tests/cases/fourslash/renameThis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts'/>
22

3-
////function f([|{| "declarationRangeIndex": 0 |}this|]) {
3+
////function f([|this|]) {
44
//// return [|this|];
55
////}
66
////this/**/;

0 commit comments

Comments
 (0)