Skip to content

fix(42923): Provide go-to-definition on unresolved shorthand properties #42924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace ts.GoToDefinition {
// assignment. This case and others are handled by the following code.
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(symbol.valueDeclaration);
return shorthandSymbol ? shorthandSymbol.declarations.map(decl => createDefinitionInfo(decl, typeChecker, shorthandSymbol, node)) : [];
const definitions = shorthandSymbol ? shorthandSymbol.declarations.map(decl => createDefinitionInfo(decl, typeChecker, shorthandSymbol, node)) : emptyArray;
return concatenate(definitions, getDefinitionFromObjectLiteralElement(typeChecker, node) || emptyArray);
}

// If the node is the name of a BindingElement within an ObjectBindingPattern instead of just returning the
Expand All @@ -75,25 +76,7 @@ namespace ts.GoToDefinition {
});
}

// If the current location we want to find its definition is in an object literal, try to get the contextual type for the
// object literal, lookup the property symbol in the contextual type, and use this for goto-definition.
// For example
// interface Props{
// /*first*/prop1: number
// prop2: boolean
// }
// function Foo(arg: Props) {}
// Foo( { pr/*1*/op1: 10, prop2: true })
const element = getContainingObjectLiteralElement(node);
if (element) {
const contextualType = element && typeChecker.getContextualType(element.parent);
if (contextualType) {
return flatMap(getPropertySymbolsFromContextualType(element, typeChecker, contextualType, /*unionSymbolOk*/ false), propertySymbol =>
getDefinitionFromSymbol(typeChecker, propertySymbol, node));
}
}

return getDefinitionFromSymbol(typeChecker, symbol, node);
return getDefinitionFromObjectLiteralElement(typeChecker, node) || getDefinitionFromSymbol(typeChecker, symbol, node);
}

/**
Expand All @@ -108,6 +91,26 @@ namespace ts.GoToDefinition {
|| (!isCallLikeExpression(calledDeclaration.parent) && s === calledDeclaration.parent.symbol);
}

// If the current location we want to find its definition is in an object literal, try to get the contextual type for the
// object literal, lookup the property symbol in the contextual type, and use this for goto-definition.
// For example
// interface Props{
// /*first*/prop1: number
// prop2: boolean
// }
// function Foo(arg: Props) {}
// Foo( { pr/*1*/op1: 10, prop2: true })
function getDefinitionFromObjectLiteralElement(typeChecker: TypeChecker, node: Node) {
const element = getContainingObjectLiteralElement(node);
if (element) {
const contextualType = element && typeChecker.getContextualType(element.parent);
if (contextualType) {
return flatMap(getPropertySymbolsFromContextualType(element, typeChecker, contextualType, /*unionSymbolOk*/ false), propertySymbol =>
getDefinitionFromSymbol(typeChecker, propertySymbol, node));
}
}
}

export function getReferenceAtPosition(sourceFile: SourceFile, position: number, program: Program): { reference: FileReference, file: SourceFile } | undefined {
const referencePath = findReferenceInPosition(sourceFile.referencedFiles, position);
if (referencePath) {
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/fourslash/goToDefinitionShorthandProperty04.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="./fourslash.ts"/>

////interface Foo {
//// /*2*/foo(): void
////}
////
////let x: Foo = {
//// [|f/*1*/oo|]
////}

verify.goToDefinition("1", "2");
11 changes: 11 additions & 0 deletions tests/cases/fourslash/goToDefinitionShorthandProperty05.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="./fourslash.ts"/>

////interface Foo {
//// /*3*/foo(): void
////}
////const /*2*/foo = 1;
////let x: Foo = {
//// [|f/*1*/oo|]
////}

verify.goToDefinition("1", ["2", "3"]);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/goToDefinitionShorthandProperty06.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="./fourslash.ts"/>

////interface Foo {
//// /*2*/foo(): void
////}
////const foo = 1;
////let x: Foo = {
//// [|f/*1*/oo|]()
////}

verify.goToDefinition("1", "2");