diff --git a/src/services/goToDefinition.ts b/src/services/goToDefinition.ts index 1cb42a59e3c..e514ca01e79 100644 --- a/src/services/goToDefinition.ts +++ b/src/services/goToDefinition.ts @@ -233,20 +233,23 @@ namespace ts.GoToDefinition { } function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] | undefined { - return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(symbol.declarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node)); + // There are cases when you extend a function by adding properties to it afterwards, + // we want to strip those extra properties + const filteredDeclarations = filter(symbol.declarations, d => !isAssignmentDeclaration(d) || d === symbol.valueDeclaration) || undefined; + return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(filteredDeclarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node)); function getConstructSignatureDefinition(): DefinitionInfo[] | undefined { // Applicable only if we are in a new expression, or we are on a constructor declaration // and in either case the symbol has a construct signature definition, i.e. class if (symbol.flags & SymbolFlags.Class && (isNewExpressionTarget(node) || node.kind === SyntaxKind.ConstructorKeyword)) { - const cls = find(symbol.declarations, isClassLike) || Debug.fail("Expected declaration to have at least one class-like declaration"); + const cls = find(filteredDeclarations, isClassLike) || Debug.fail("Expected declaration to have at least one class-like declaration"); return getSignatureDefinition(cls.members, /*selectConstructors*/ true); } } function getCallSignatureDefinition(): DefinitionInfo[] | undefined { return isCallOrNewExpressionTarget(node) || isNameOfFunctionDeclaration(node) - ? getSignatureDefinition(symbol.declarations, /*selectConstructors*/ false) + ? getSignatureDefinition(filteredDeclarations, /*selectConstructors*/ false) : undefined; } diff --git a/tests/cases/fourslash/goToDefinitionPropertyAssignment.ts b/tests/cases/fourslash/goToDefinitionPropertyAssignment.ts new file mode 100644 index 00000000000..aec6e28fdf1 --- /dev/null +++ b/tests/cases/fourslash/goToDefinitionPropertyAssignment.ts @@ -0,0 +1,11 @@ +/// + +//// export const /*FunctionResult*/Component = () => { return "OK"} +//// Component./*PropertyResult*/displayName = 'Component' +//// +//// [|/*FunctionClick*/Component|] +//// +//// Component.[|/*PropertyClick*/displayName|] + +verify.goToDefinition("FunctionClick", "FunctionResult") +verify.goToDefinition("PropertyClick", "PropertyResult")