Fix shorthand property resolution in typed contexts#441
Open
robbieh-noname wants to merge 1 commit intosourcegraph:mainfrom
Open
Fix shorthand property resolution in typed contexts#441robbieh-noname wants to merge 1 commit intosourcegraph:mainfrom
robbieh-noname wants to merge 1 commit intosourcegraph:mainfrom
Conversation
When a shorthand property (e.g., `new ApolloServer({ resolvers })`) is
passed to a typed parameter, SCIP emits the type-property reference but
misses the local variable reference. This adds the missing reference by
checking if the original node is a shorthand property assignment and
emitting an additional occurrence for the local binding.
Fixes behavior reported in sourcegraph#415 and sourcegraph#440.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #440. Possibly related: #415 (same class of bug in decorator context — I haven't tested that case, so it may need a separate fix).
When a shorthand property is passed to a typed context (constructor or function call),
visitSymbolOccurrence()emits references to the type definition's properties but misses the local variable reference. This is becausegetDeclarationsForPropertyAssignment()returns non-empty declarations, causingisDefinitionNodeto befalse, which gates outhandleShorthandPropertyDefinition()— the method specifically designed to emit that local variable reference.Example
Root Cause
In
FileIndexer.ts,visitSymbolOccurrence():getDeclarationsForPropertyAssignment(node)succeeds —declarationsis non-emptyisDefinitionNode = declarations.length === 0 && isDefinition(node)—false(short-circuits)forloop emits type-property references correctlyhandleShorthandPropertyDefinitionis gated behindif (isDefinitionNode)— never calledhandleShorthandPropertyDefinitionuseschecker.getShorthandAssignmentValueSymbol()to find the local variable — the logic is correct, it's just unreachable in typed contexts.Fix
After the
for (const declaration of declarations)loop, add a block that callschecker.getShorthandAssignmentValueSymbol()directly whendeclarations.length > 0and the node is aShorthandPropertyAssignment. This emits the missing local variable reference.pushOccurrencededuplication (isEqualOccurrence) prevents double-emission.The fix is ~15 lines of logic (21 including comments), in one location.
Test plan
snapshots/input/syntax/src/object-literals-call-signatures.tshas ahandleShorthand()function that passes shorthand properties to a typed function (consumesInterface({ interfaceMethod, property })). The snapshot update (+2 lines) shows the local variable references (reference local 26,reference local 23) are now correctly emitted alongside the existing type-property references.main(cherry-picked from v0.3.15 where 30/30 passed — the extra test is from the@deprecatedfeature in v0.4.0).makeExecutableSchema()from@graphql-tools/schemaand any typed function/constructor with interface parameters.pushOccurrenceusesisEqualOccurrenceto prevent double-emission — if the local variable reference was already emitted through another path, it won't be duplicated.Impact
This bug affects any TypeScript code using shorthand property syntax in typed constructor/function contexts — not just Apollo Server. It breaks tooling that relies on SCIP reference edges to connect local variable definitions to their usage sites (e.g., code navigation, static analysis).
Note on #415 (NestJS decorators): Issue #415 reports the same symptom in decorator context (
@Module({ imports })). I haven't tested whether this fix covers decorators — they may go through a different code path. If not, that'd need a separate look.