From 4514f8fde52f1d58722e961339ceec43cf3e853d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 29 Aug 2016 10:14:59 -0700 Subject: [PATCH] Make goto-definition go to a signature declaration if possible --- src/harness/fourslash.ts | 6 +- src/services/services.ts | 58 +++++++++++++++---- .../goToDefinitionConstructorOverloads.ts | 4 +- .../goToDefinitionFunctionOverloads.ts | 4 +- .../goToDefinitionMethodOverloads.ts | 22 +++---- tests/cases/fourslash/goToDefinition_super.ts | 33 +++++++++++ 6 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 tests/cases/fourslash/goToDefinition_super.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 63731417f48..408c52ef5f5 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1546,7 +1546,7 @@ namespace FourSlash { public goToDefinition(definitionIndex: number) { const definitions = this.languageService.getDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition); if (!definitions || !definitions.length) { - this.raiseError("goToDefinition failed - expected to at least one definition location but got 0"); + this.raiseError("goToDefinition failed - expected to find at least one definition location but got 0"); } if (definitionIndex >= definitions.length) { @@ -1561,7 +1561,7 @@ namespace FourSlash { public goToTypeDefinition(definitionIndex: number) { const definitions = this.languageService.getTypeDefinitionAtPosition(this.activeFile.fileName, this.currentCaretPosition); if (!definitions || !definitions.length) { - this.raiseError("goToTypeDefinition failed - expected to at least one definition location but got 0"); + this.raiseError("goToTypeDefinition failed - expected to find at least one definition location but got 0"); } if (definitionIndex >= definitions.length) { @@ -1582,7 +1582,7 @@ namespace FourSlash { this.raiseError(`goToDefinition - expected to 0 definition locations but got ${definitions.length}`); } else if (!foundDefinitions && !negative) { - this.raiseError("goToDefinition - expected to at least one definition location but got 0"); + this.raiseError("goToDefinition - expected to find at least one definition location but got 0"); } } diff --git a/src/services/services.ts b/src/services/services.ts index a55f2cf61b0..29cbcfd59d5 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2788,18 +2788,37 @@ namespace ts { return node && node.parent && node.parent.kind === SyntaxKind.PropertyAccessExpression && (node.parent).name === node; } + function climbPastPropertyAccess(node: Node) { + return isRightSideOfPropertyAccess(node) ? node.parent : node; + } + function isCallExpressionTarget(node: Node): boolean { - if (isRightSideOfPropertyAccess(node)) { - node = node.parent; - } - return node && node.parent && node.parent.kind === SyntaxKind.CallExpression && (node.parent).expression === node; + return !!getCallOrNewExpressionWorker(node, SyntaxKind.CallExpression); } function isNewExpressionTarget(node: Node): boolean { - if (isRightSideOfPropertyAccess(node)) { - node = node.parent; + return !!getCallOrNewExpressionWorker(node, SyntaxKind.NewExpression); + } + + function getCallOrNewExpressionTargetingNode(node: Node): CallExpression | NewExpression | undefined { + return getCallOrNewExpressionWorker(node, SyntaxKind.CallExpression) || getCallOrNewExpressionWorker(node, SyntaxKind.NewExpression); + } + + function tryGetCalledDeclaration(typeChecker: TypeChecker, node: Node): SignatureDeclaration | undefined { + const callOrNewExpression = getCallOrNewExpressionTargetingNode(node); + if (callOrNewExpression) { + const signature = typeChecker.getResolvedSignature(callOrNewExpression); + return signature.declaration; } - return node && node.parent && node.parent.kind === SyntaxKind.NewExpression && (node.parent).expression === node; + } + + function getCallOrNewExpressionWorker(node: Node, kind: SyntaxKind): Node | undefined { + const target = climbPastPropertyAccess(node); + return target && + target.parent && + target.parent.kind === kind && + (target.parent).expression === target && + target.parent; } function isNameOfModuleDeclaration(node: Node) { @@ -5068,14 +5087,25 @@ namespace ts { }; } + function getSymbolInfo(typeChecker: TypeChecker, symbol: Symbol, node: Node) { + return { + symbolName: typeChecker.symbolToString(symbol), // Do not get scoped name, just the name of the symbol + symbolKind: getSymbolKind(symbol, node), + containerName: symbol.parent ? typeChecker.symbolToString(symbol.parent, node) : "" + }; + } + + function getDefinitionFromSignatureDeclaration(decl: SignatureDeclaration): DefinitionInfo { + const typeChecker = program.getTypeChecker(); + const { symbolName, symbolKind, containerName } = getSymbolInfo(typeChecker, decl.symbol, decl); + return createDefinitionInfo(decl, symbolKind, symbolName, containerName); + } + function getDefinitionFromSymbol(symbol: Symbol, node: Node): DefinitionInfo[] { const typeChecker = program.getTypeChecker(); const result: DefinitionInfo[] = []; const declarations = symbol.getDeclarations(); - const symbolName = typeChecker.symbolToString(symbol); // Do not get scoped name, just the name of the symbol - const symbolKind = getSymbolKind(symbol, node); - const containerSymbol = symbol.parent; - const containerName = containerSymbol ? typeChecker.symbolToString(containerSymbol, node) : ""; + const { symbolName, symbolKind, containerName } = getSymbolInfo(typeChecker, symbol, node); if (!tryAddConstructSignature(symbol, node, symbolKind, symbolName, containerName, result) && !tryAddCallSignature(symbol, node, symbolKind, symbolName, containerName, result)) { @@ -5201,6 +5231,12 @@ namespace ts { } const typeChecker = program.getTypeChecker(); + + const calledDeclaration = tryGetCalledDeclaration(typeChecker, node); + if (calledDeclaration) { + return [getDefinitionFromSignatureDeclaration(calledDeclaration)]; + } + let symbol = typeChecker.getSymbolAtLocation(node); // Could not find a symbol e.g. node is string or number keyword, diff --git a/tests/cases/fourslash/goToDefinitionConstructorOverloads.ts b/tests/cases/fourslash/goToDefinitionConstructorOverloads.ts index a1fda8886db..d52243a6465 100644 --- a/tests/cases/fourslash/goToDefinitionConstructorOverloads.ts +++ b/tests/cases/fourslash/goToDefinitionConstructorOverloads.ts @@ -11,11 +11,11 @@ goTo.marker('constructorOverloadReference1'); goTo.definition(); -verify.caretAtMarker('constructorDefinition'); +verify.caretAtMarker('constructorOverload1'); goTo.marker('constructorOverloadReference2'); goTo.definition(); -verify.caretAtMarker('constructorDefinition'); +verify.caretAtMarker('constructorOverload2'); goTo.marker('constructorOverload1'); goTo.definition(); diff --git a/tests/cases/fourslash/goToDefinitionFunctionOverloads.ts b/tests/cases/fourslash/goToDefinitionFunctionOverloads.ts index 488d788073a..fb690607f1a 100644 --- a/tests/cases/fourslash/goToDefinitionFunctionOverloads.ts +++ b/tests/cases/fourslash/goToDefinitionFunctionOverloads.ts @@ -9,11 +9,11 @@ goTo.marker('functionOverloadReference1'); goTo.definition(); -verify.caretAtMarker('functionOverloadDefinition'); +verify.caretAtMarker('functionOverload1'); goTo.marker('functionOverloadReference2'); goTo.definition(); -verify.caretAtMarker('functionOverloadDefinition'); +verify.caretAtMarker('functionOverload2'); goTo.marker('functionOverload'); goTo.definition(); diff --git a/tests/cases/fourslash/goToDefinitionMethodOverloads.ts b/tests/cases/fourslash/goToDefinitionMethodOverloads.ts index 6d74881c2af..7b4bd0630d9 100644 --- a/tests/cases/fourslash/goToDefinitionMethodOverloads.ts +++ b/tests/cases/fourslash/goToDefinitionMethodOverloads.ts @@ -1,11 +1,11 @@ /// ////class MethodOverload { -//// static me/*staticMethodOverload1*/thod(); -//// static me/*staticMethodOverload2*/thod(foo: string); -/////*staticMethodDefinition*/static method(foo?: any) { } -//// public met/*instanceMethodOverload1*/hod(): any; -//// public met/*instanceMethodOverload2*/hod(foo: string); +//// /*staticMethodOverload1*/static /*staticMethodOverload1Name*/method(); +//// /*staticMethodOverload2*/static method(foo: string); +//// /*staticMethodDefinition*/static method(foo?: any) { } +//// /*instanceMethodOverload1*/public /*instanceMethodOverload1Name*/method(): any; +//// /*instanceMethodOverload2*/public method(foo: string); /////*instanceMethodDefinition*/public method(foo?: any) { return "foo" } ////} @@ -20,25 +20,25 @@ goTo.marker('staticMethodReference1'); goTo.definition(); -verify.caretAtMarker('staticMethodDefinition'); +verify.caretAtMarker('staticMethodOverload1'); goTo.marker('staticMethodReference2'); goTo.definition(); -verify.caretAtMarker('staticMethodDefinition'); +verify.caretAtMarker('staticMethodOverload2'); goTo.marker('instanceMethodReference1'); goTo.definition(); -verify.caretAtMarker('instanceMethodDefinition'); +verify.caretAtMarker('instanceMethodOverload1'); goTo.marker('instanceMethodReference2'); goTo.definition(); -verify.caretAtMarker('instanceMethodDefinition'); +verify.caretAtMarker('instanceMethodOverload2'); -goTo.marker('staticMethodOverload1'); +goTo.marker('staticMethodOverload1Name'); goTo.definition(); verify.caretAtMarker('staticMethodDefinition'); -goTo.marker('instanceMethodOverload1'); +goTo.marker('instanceMethodOverload1Name'); goTo.definition(); verify.caretAtMarker('instanceMethodDefinition'); diff --git a/tests/cases/fourslash/goToDefinition_super.ts b/tests/cases/fourslash/goToDefinition_super.ts new file mode 100644 index 00000000000..576d3535af0 --- /dev/null +++ b/tests/cases/fourslash/goToDefinition_super.ts @@ -0,0 +1,33 @@ +/// + +////class A { +//// /*ctr*/constructor() {} +//// x() {} +////} +/////*B*/class B extends A {} +////class C extends B { +//// constructor() { +//// /*super*/super(); +//// } +//// method() { +//// /*superExpression*/super.x(); +//// } +////} +////class D { +//// constructor() { +//// /*superBroken*/super(); +//// } +////} + +// Super in call position goes to constructor. +goTo.marker("super"); +goTo.definition(); +verify.caretAtMarker("ctr"); + +// Super in any other position goes to the superclass. +goTo.marker("superExpression"); +goTo.definition(); +verify.caretAtMarker("B"); + +goTo.marker("superBroken"); +verify.definitionCountIs(0);