From a641e6f85f6016b008b72de7573d98b79ce8283f Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 4 Jun 2018 14:13:27 -0700 Subject: [PATCH] goToDefinition: Put variable definition before signature definition (#24649) * goToDefinition: Put variable definition before signature definition * Fix lint --- src/services/goToDefinition.ts | 13 +++++++++---- .../goToDefinitionNewExpressionTargetNotClass.ts | 2 +- .../cases/fourslash/goToDefinitionSignatureAlias.ts | 4 ++-- .../fourslash/tsxGoToDefinitionUnionElementType1.ts | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/services/goToDefinition.ts b/src/services/goToDefinition.ts index 1b9ecf0b5f..e9937549e2 100644 --- a/src/services/goToDefinition.ts +++ b/src/services/goToDefinition.ts @@ -32,11 +32,16 @@ namespace ts.GoToDefinition { const sigInfo = createDefinitionFromSignatureDeclaration(typeChecker, calledDeclaration); // For a function, if this is the original function definition, return just sigInfo. // If this is the original constructor definition, parent is the class. - return typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) || + if (typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) || // TODO: GH#23742 Following check shouldn't be necessary if 'require' is an alias - symbol.declarations.some(d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false)) - ? [sigInfo] - : [sigInfo, ...getDefinitionFromSymbol(typeChecker, symbol, node)!]; + symbol.declarations.some(d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false))) { + return [sigInfo]; + } + else { + const defs = getDefinitionFromSymbol(typeChecker, symbol, node)!; + // For a 'super()' call, put the signature first, else put the variable first. + return node.kind === SyntaxKind.SuperKeyword ? [sigInfo, ...defs] : [...defs, sigInfo]; + } } // Because name in short-hand property assignment has two different meanings: property name and property value, diff --git a/tests/cases/fourslash/goToDefinitionNewExpressionTargetNotClass.ts b/tests/cases/fourslash/goToDefinitionNewExpressionTargetNotClass.ts index 00f83323a0..5be7a4307f 100644 --- a/tests/cases/fourslash/goToDefinitionNewExpressionTargetNotClass.ts +++ b/tests/cases/fourslash/goToDefinitionNewExpressionTargetNotClass.ts @@ -11,6 +11,6 @@ ////new [|/*invokeExpression2*/I2|](); verify.goToDefinition({ - invokeExpression1: ["constructSignature", "I"], + invokeExpression1: ["I", "constructSignature"], invokeExpression2: "symbolDeclaration" }); diff --git a/tests/cases/fourslash/goToDefinitionSignatureAlias.ts b/tests/cases/fourslash/goToDefinitionSignatureAlias.ts index 07a5678f5d..27ff0eb69d 100644 --- a/tests/cases/fourslash/goToDefinitionSignatureAlias.ts +++ b/tests/cases/fourslash/goToDefinitionSignatureAlias.ts @@ -10,6 +10,6 @@ verify.goToDefinition({ useF: "f", - useG: ["f", "g"], - useH: ["f", "h"], + useG: ["g", "f"], + useH: ["h", "f"], }); diff --git a/tests/cases/fourslash/tsxGoToDefinitionUnionElementType1.ts b/tests/cases/fourslash/tsxGoToDefinitionUnionElementType1.ts index 0e899c71d2..957b6b811e 100644 --- a/tests/cases/fourslash/tsxGoToDefinitionUnionElementType1.ts +++ b/tests/cases/fourslash/tsxGoToDefinitionUnionElementType1.ts @@ -22,5 +22,5 @@ //// <[|SFC/*one*/Comp|] x /> verify.goToDefinition({ - "one": ["pt1", "def"], + "one": ["def", "pt1"], });