Skip to content

Commit f1a0a7f

Browse files
author
Orta Therox
committed
Don't let the additional property setting on an object show up as a definition for the lanmguage server
1 parent 74c6bc1 commit f1a0a7f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/services/goToDefinition.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace ts.GoToDefinition {
2626
if (!symbol) {
2727
return getDefinitionInfoForIndexSignatures(node, typeChecker);
2828
}
29-
29+
3030
const calledDeclaration = tryGetSignatureDeclaration(typeChecker, node);
3131
// Don't go to the component constructor definition for a JSX element, just go to the component definition.
3232
if (calledDeclaration && !(isJsxOpeningLikeElement(node.parent) && isConstructorLike(calledDeclaration))) {
@@ -233,20 +233,24 @@ namespace ts.GoToDefinition {
233233
}
234234

235235
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] | undefined {
236-
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(symbol.declarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node));
236+
// There are cases when you extend a function by adding properties to it afterwards,
237+
// we want to strip those extra properties
238+
const filteredDeclarations = symbol.declarations.filter(d => !ts.isAssignmentDeclaration(d) || d === symbol.valueDeclaration)
239+
240+
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(filteredDeclarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node));
237241

238242
function getConstructSignatureDefinition(): DefinitionInfo[] | undefined {
239243
// Applicable only if we are in a new expression, or we are on a constructor declaration
240244
// and in either case the symbol has a construct signature definition, i.e. class
241245
if (symbol.flags & SymbolFlags.Class && (isNewExpressionTarget(node) || node.kind === SyntaxKind.ConstructorKeyword)) {
242-
const cls = find(symbol.declarations, isClassLike) || Debug.fail("Expected declaration to have at least one class-like declaration");
246+
const cls = find(filteredDeclarations, isClassLike) || Debug.fail("Expected declaration to have at least one class-like declaration");
243247
return getSignatureDefinition(cls.members, /*selectConstructors*/ true);
244248
}
245249
}
246250

247251
function getCallSignatureDefinition(): DefinitionInfo[] | undefined {
248252
return isCallOrNewExpressionTarget(node) || isNameOfFunctionDeclaration(node)
249-
? getSignatureDefinition(symbol.declarations, /*selectConstructors*/ false)
253+
? getSignatureDefinition(filteredDeclarations, /*selectConstructors*/ false)
250254
: undefined;
251255
}
252256

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// export const /*FunctionResult*/Component = () => { return "OK"}
4+
//// Component./*PropertyResult*/displayName = 'Component'
5+
////
6+
//// [|/*FunctionClick*/Component|]
7+
////
8+
//// Component.[|/*PropertyClick*/displayName|]
9+
10+
verify.goToDefinition("FunctionClick", "FunctionResult")
11+
12+
verify.goToDefinition("PropertyClick", "PropertyResult")
13+
14+
// export const Component = () => { return "OK"}
15+
// Component.displayName = 'Component'
16+

0 commit comments

Comments
 (0)