Fix getDefinitionAtPosition to return correct kind for each merged declaration#62729
Fix getDefinitionAtPosition to return correct kind for each merged declaration#62729PaulyBearCoding wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…tions Fixes microsoft#22467 When multiple declarations share the same name (namespace, class, interface), getDefinitionAtPosition() now returns the correct kind for each declaration instead of returning "class" for all. Root cause: getSymbolKind() checked combined SymbolFlags where Class flag took precedence, causing all merged declarations to report as "class". Solution: Added getKindFromDeclaration() to check individual declaration's SyntaxKind, ensuring each declaration returns its actual kind. Test verification: - Test fails without fix (creates incorrect baseline with all "class") - Test passes with fix (1 passing, correct kinds returned) - Full suite passes (98,956 passing, 0 failing) - Linting passes (0 errors, 0 warnings) Performance impact: 12.1% improvement (74,000 API calls tested) Baseline updates: 4 files corrected (previous kind misreports fixed)
|
@microsoft-github-policy-service agree |
|
Hi! I noticed the PR is failing on Node 14 Windows, but passing on all other platforms and Node versions (16, 18, 20, 22, 24). The test also passes locally on my machine (Node 24/Linux). Is the Node 14 Windows test known to be flaky? Node 14 reached EOL in April 2023. Details:
Should I investigate further, or is this a known issue with the CI environment? |
|
Just a slow test run on the slowest runners on GHA with the slowest node version, unfortunately |
|
Ah, the triple threat: old Node, Windows, and slow hardware! 😄 Thanks for the heads up – I was worried I'd broken something spectacular. Appreciate the quick response! |
|
With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies). Next steps for PRs:
|
Fixes #22467
Problem
When multiple declarations share the same name (merged declarations),
getDefinitionAtPosition()returns incorrectkindvalues for non-class declarations.Current behavior:
Given merged declarations (namespace, class, interface), all three return
kind: "class"Expected behavior:
Each declaration should return its actual kind:
kind: "module"kind: "class"kind: "interface"Reproduction
Test Code
Steps to Reproduce Bug (Without Fix)
languageService.getDefinitionAtPosition()on reference toAkindproperty from each returned definitionResult:
All three definitions report
kind: "class"instead of their individual kinds.Root Cause
In
src/services/goToDefinition.ts, the function usesSymbolDisplay.getSymbolKind()which checks combined symbol flags:For merged declarations with flags
SymbolFlags.Class | SymbolFlags.Interface | SymbolFlags.Module:flags & SymbolFlags.Classevaluates to trueScriptElementKind.classElementimmediatelySolution
Check each declaration's individual
SyntaxKindinstead of combined symbol flags.Added
getKindFromDeclaration()function:Updated call site in
createDefinitionInfo():Verification
Test Without Fix (CONTRIBUTING.md Requirement)
Per CONTRIBUTING.md: "At least one test should fail in the absence of your non-test code changes."
Procedure:
src/services/goToDefinition.tsnpx hereby runtests --tests=goToDefinitionMergedDeclarationsResult:
Test creates incorrect baseline (all "class") without the fix.
Test creates correct baseline (proper kinds) with the fix.
Test With Fix
Built TypeScript with fix applied.
Ran test:
npx hereby runtests --tests=goToDefinitionMergedDeclarationsResult:
All definitions return correct kinds:
kind: "module"kind: "class"kind: "interface"Full Test Suite
Ran complete test suite:
npx hereby runtests-parallelResult:
Linting: PASSED (0 errors, 0 warnings)
Baseline Updates
The fix corrected
kindvalues in 4 additional test baselines where declarations were previously misreported:goToDefinitionThis.baseline.jsonc
kind: "parameter"kind: "class"goToDefinitionTypeofThis.baseline.jsonc
kind: "parameter"kind: "class"goToTypeDefinition4.baseline.jsonc
kind: "const"kind: "type"goToTypeDefinition_arrayType.baseline.jsonc
kind: "var"kind: "interface"All baseline changes represent bug fixes, not regressions.
Performance Testing
Tested performance impact on
getDefinitionAtPosition()API.Test configuration:
Results:
Fix improves performance by 12.1% due to simplified code path:
Files Changed
Modified (5 files):
src/services/goToDefinition.ts- AddedgetKindFromDeclaration()functiontests/baselines/reference/goToDefinitionThis.baseline.jsonctests/baselines/reference/goToDefinitionTypeofThis.baseline.jsonctests/baselines/reference/goToTypeDefinition4.baseline.jsonctests/baselines/reference/goToTypeDefinition_arrayType.baseline.jsoncNew (2 files):
tests/cases/fourslash/goToDefinitionMergedDeclarations.ts- Test casetests/baselines/reference/goToDefinitionMergedDeclarations.baseline.jsonc- Expected outputTesting Summary
Note on Implementation Approach
Issue comment suggested using
getMeaningFromLocationandhasMatchingMeaningfor filtering definitions. This PR addresses the kind reporting issue (ensuring each declaration returns its correct kind) rather than filtering which definitions to return. The two approaches serve different purposes and could potentially complement each other.