Fix go-to-definition for object literal shorthand properties
Go-to-definition on shorthand property names in object literals was only returning the variable/parameter declaration, missing the contextual interface property declaration. TypeScript correctly returns both.
interface Something {
foo: string; // Should be included in go-to-def results
}
function makeSomething(foo: string): Something {
return { foo }; // Go-to-def here should find both declarations
}
Changes
- Modified
getDeclarationsFromLocationininternal/ls/definition.goto concatenate shorthand symbol declarations with contextual type property declarations for shorthand property assignments - Added
getDeclarationsFromObjectLiteralElementhelper to retrieve property declarations from the object literal's contextual type - Updated test baselines for
goToDefinitionShorthandProperty04andgoToDefinitionShorthandProperty05to match TypeScript behavior - Added nil check for
GetResolvedSymbolto prevent potential nil pointer dereference
Original prompt
This section details on the original issue you should resolve
<issue_title>Go-to-def on object literal shorthand misbehaves</issue_title> <issue_description>With:
interface Something { foo: string; } function makeSomething(foo: string): Something { return { foo }; }Go-to-def on
fooin the object literal goes to the name in the object literal, as opposed to the parameter or even thefooinSomething. (I can't recall which is correct. Both?)</issue_description><agent_instructions>Write a fourslash test in ./internal/fourslash/tests based on an existing fourslash definition test. Commit that test first with its baselines showing that the current behavior only goes to the parameter. Then work on a fix to make it give both the parameter and the interface property. You may want to consult the original implementation in _submodules/TypeScript/src/services.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@jakebailey The correct behavior is to return both the parameter and the interface property; checked against tsserver.
- Fixes microsoft/typescript-go#1112
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.