From 004488c0c923f3d1e6519621acaa4969b3e52c57 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 4 Jun 2019 15:21:31 -0700 Subject: [PATCH] Set declaration span only if its not same as own span --- src/services/findAllReferences.ts | 33 ++++++++----------- src/services/goToDefinition.ts | 11 +++---- tests/cases/fourslash/localGetReferences.ts | 2 +- .../renameDestructuringFunctionParameter.ts | 2 +- .../renameJsSpecialAssignmentRhs1.ts | 2 +- .../renameJsSpecialAssignmentRhs2.ts | 2 +- tests/cases/fourslash/renameThis.ts | 2 +- 7 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 8ef72440bc..14e47b2c4d 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -111,11 +111,6 @@ namespace ts.FindAllReferences { case SyntaxKind.ImportClause: return node.parent; - case SyntaxKind.JsxAttribute: - return (node as JsxAttribute).initializer === undefined ? - undefined : - node; - case SyntaxKind.BinaryExpression: return isExpressionStatement(node.parent) ? node.parent : @@ -129,7 +124,6 @@ namespace ts.FindAllReferences { }; case SyntaxKind.PropertyAssignment: - // TODO(shkamat):: Should we show whole object literal instead? case SyntaxKind.ShorthandPropertyAssignment: return isArrayLiteralOrObjectLiteralDestructuringPattern(node.parent) ? getDeclarationForDeclarationSpan( @@ -137,15 +131,24 @@ namespace ts.FindAllReferences { isBinaryExpression(node) || isForInOrOfStatement(node) ) as BinaryExpression | ForInOrOfStatement ) : - node.kind === SyntaxKind.PropertyAssignment ? - node : - undefined; + node; default: return node; } } + export function setDeclarationSpan(span: DocumentSpan, sourceFile: SourceFile, declaration?: DeclarationNode) { + if (declaration) { + const declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ? + getTextSpan(declaration.start, sourceFile, declaration.end) : + getTextSpan(declaration, sourceFile); + if (declarationSpan.start !== span.textSpan.start || declarationSpan.length !== span.textSpan.length) { + span.declarationSpan = declarationSpan; + } + } + } + export interface Options { readonly findInStrings?: boolean; readonly findInComments?: boolean; @@ -287,11 +290,7 @@ namespace ts.FindAllReferences { textSpan: getTextSpan(isComputedPropertyName(node) ? node.expression : node, sourceFile), displayParts }; - if (declaration) { - result.declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ? - getTextSpan(declaration.start, sourceFile, declaration.end) : - getTextSpan(declaration, sourceFile); - } + setDeclarationSpan(result, sourceFile, declaration); return result; } @@ -330,11 +329,7 @@ namespace ts.FindAllReferences { else { const sourceFile = entry.node.getSourceFile(); const result: DocumentSpan = { textSpan: getTextSpan(entry.node, sourceFile), fileName: sourceFile.fileName }; - if (entry.declaration) { - result.declarationSpan = isDeclarationNodeWithStartAndEnd(entry.declaration) ? - getTextSpan(entry.declaration.start, sourceFile, entry.declaration.end) : - getTextSpan(entry.declaration, sourceFile); - } + setDeclarationSpan(result, sourceFile, entry.declaration); return result; } } diff --git a/src/services/goToDefinition.ts b/src/services/goToDefinition.ts index 05bd62e363..4970ae573b 100644 --- a/src/services/goToDefinition.ts +++ b/src/services/goToDefinition.ts @@ -281,12 +281,11 @@ namespace ts.GoToDefinition { containerKind: undefined!, // TODO: GH#18217 containerName, }; - const declarationNode = FindAllReferences.getDeclarationForDeclarationSpan(declaration); - if (declarationNode) { - result.declarationSpan = FindAllReferences.isDeclarationNodeWithStartAndEnd(declarationNode) ? - createTextSpanFromNode(declarationNode.start, sourceFile, declarationNode.end) : - createTextSpanFromNode(declarationNode, sourceFile); - } + FindAllReferences.setDeclarationSpan( + result, + sourceFile, + FindAllReferences.getDeclarationForDeclarationSpan(declaration) + ); return result; } diff --git a/tests/cases/fourslash/localGetReferences.ts b/tests/cases/fourslash/localGetReferences.ts index 401599368a..dd78369398 100644 --- a/tests/cases/fourslash/localGetReferences.ts +++ b/tests/cases/fourslash/localGetReferences.ts @@ -117,7 +117,7 @@ ////array.forEach( //// //// -////function([|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 43 |}str|]) { +////function([|{| "isWriteAccess": true, "isDefinition": true |}str|]) { //// //// //// diff --git a/tests/cases/fourslash/renameDestructuringFunctionParameter.ts b/tests/cases/fourslash/renameDestructuringFunctionParameter.ts index 79d2b40193..80d82512ac 100644 --- a/tests/cases/fourslash/renameDestructuringFunctionParameter.ts +++ b/tests/cases/fourslash/renameDestructuringFunctionParameter.ts @@ -1,6 +1,6 @@ /// -////function f([|{[|{| "declarationRangeIndex": 0 |}a|]}: {[|{| "declarationRangeIndex": 2 |}a|]}|]) { +////function f([|{[|{| "declarationRangeIndex": 0 |}a|]}: {[|a|]}|]) { //// f({[|a|]}); ////} diff --git a/tests/cases/fourslash/renameJsSpecialAssignmentRhs1.ts b/tests/cases/fourslash/renameJsSpecialAssignmentRhs1.ts index db3f1fc66e..013cfe34cd 100644 --- a/tests/cases/fourslash/renameJsSpecialAssignmentRhs1.ts +++ b/tests/cases/fourslash/renameJsSpecialAssignmentRhs1.ts @@ -5,7 +5,7 @@ //// set: function (x) { //// this._x = x; //// }, -//// copy: function ([|{| "declarationRangeIndex": 0 |}x|]) { +//// copy: function ([|x|]) { //// this._x = [|x|].prop; //// } ////}; diff --git a/tests/cases/fourslash/renameJsSpecialAssignmentRhs2.ts b/tests/cases/fourslash/renameJsSpecialAssignmentRhs2.ts index db3f1fc66e..013cfe34cd 100644 --- a/tests/cases/fourslash/renameJsSpecialAssignmentRhs2.ts +++ b/tests/cases/fourslash/renameJsSpecialAssignmentRhs2.ts @@ -5,7 +5,7 @@ //// set: function (x) { //// this._x = x; //// }, -//// copy: function ([|{| "declarationRangeIndex": 0 |}x|]) { +//// copy: function ([|x|]) { //// this._x = [|x|].prop; //// } ////}; diff --git a/tests/cases/fourslash/renameThis.ts b/tests/cases/fourslash/renameThis.ts index 7a209c0d6c..82bfae820d 100644 --- a/tests/cases/fourslash/renameThis.ts +++ b/tests/cases/fourslash/renameThis.ts @@ -1,6 +1,6 @@ /// -////function f([|{| "declarationRangeIndex": 0 |}this|]) { +////function f([|this|]) { //// return [|this|]; ////} ////this/**/;