Skip to content

Commit 9571de4

Browse files
author
Christoph
committed
uses getContextualType to resolve property assignments to definition
This follows the implementation I found in the TypeScript goToDefinition service https://github.com/microsoft/TypeScript/blob/88809467e8761e71483e2f4948ef411d8e447188/src/services/goToDefinition.ts#L307-L316
1 parent 2927d3a commit 9571de4

File tree

9 files changed

+61
-44
lines changed

9 files changed

+61
-44
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nodejs 20.8.1
22
pnpm 8.9.2
3+
yarn 1.22.17

snapshots/input/syntax/src/property-assignment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export function shorthandPropertyAssignment() {
55
const a = 'a'
66
return { a }
77
}
8+
type A = { a: string }
9+
export function typedPropertyAssignment(): A {
10+
return { a: 'a' }
11+
}

snapshots/output/syntax/src/destructuring.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ interface Props {
88
const props: Props = { a: 42 }
99
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/props.
1010
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
11-
// ^ definition syntax 1.0.0 src/`destructuring.ts`/a0:
12-
// relationship implementation reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
11+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
1312

1413
export function objectDestructuring(): number[] {
1514
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/objectDestructuring().

snapshots/output/syntax/src/infer-relationship.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export function returnStatement(): Configuration {
2222
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
2323
return {
2424
property: 41,
25-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property0:
26-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
25+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
2726
}
2827
}
2928
for (let i = 0; i < 9; i++) {
@@ -35,8 +34,7 @@ export function returnStatement(): Configuration {
3534
// ^ reference local 2
3635
return {
3736
property: 41,
38-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property1:
39-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
37+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
4038
}
4139
}
4240
}
@@ -47,8 +45,7 @@ export function returnStatement(): Configuration {
4745
// ^ reference local 5
4846
return {
4947
property: 41,
50-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property2:
51-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
48+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
5249
}
5350
}
5451
}
@@ -65,35 +62,31 @@ export function returnStatement(): Configuration {
6562
// ^ reference local 8
6663
return {
6764
property: 41,
68-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property3:
69-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
65+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
7066
}
7167
}
7268
}
7369
while (random() < 0) {
7470
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
7571
return {
7672
property: 41,
77-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property4:
78-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
73+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
7974
}
8075
}
8176
do {
8277
if (random() > 0) {
8378
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
8479
return {
8580
property: 41,
86-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property5:
87-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
81+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
8882
}
8983
}
9084
} while (random() < 0)
9185
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
9286

9387
return {
9488
property: 42,
95-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property6:
96-
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
89+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
9790
}
9891
}
9992

@@ -109,7 +102,7 @@ export function returnStatementInsideArgumentExpression(): Configuration[] {
109102
// ^^^^^^ reference local 12
110103
return {
111104
property: incremented,
112-
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property7:
105+
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
113106
// ^^^^^^^^^^^ reference local 15
114107
}
115108
})

snapshots/output/syntax/src/inheritance.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ export const objectLiteralImplementation: Superinterface = {
9393
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/objectLiteralImplementation.
9494
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#
9595
property: 'property',
96-
//^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/property0:
97-
//relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
96+
//^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
9897
interfaceMethod: (): string => {
99-
//^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/interfaceMethod0:
100-
//relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
98+
//^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
10199
throw new Error('Function not implemented.')
102100
// ^^^^^ reference typescript 5.3.3 lib/`lib.es5.d.ts`/Error#
103101
// ^^^^^ reference typescript 5.3.3 lib/`lib.es5.d.ts`/Error.
@@ -112,11 +110,9 @@ export function infersInterface(): void {
112110
consumesInterface({
113111
//^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/consumesInterface().
114112
interfaceMethod: (): string => 'inferred',
115-
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/interfaceMethod1:
116-
// relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
113+
// ^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
117114
property: 'inferred',
118-
// ^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/property1:
119-
// relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
115+
// ^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
120116
})
121117
}
122118

snapshots/output/syntax/src/interface.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export function newInterface(): Interface {
1717
// ^^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#
1818
return {
1919
property: 'a',
20-
// ^^^^^^^^ definition syntax 1.0.0 src/`interface.ts`/property0:
21-
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#property.
20+
// ^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#property.
2221
methodSignature(param: string): string {
2322
// ^^^^^^^^^^^^^^^ definition local 4
2423
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature().
@@ -27,8 +26,7 @@ export function newInterface(): Interface {
2726
// ^^^^^ reference local 5
2827
},
2928
methodSignature2: (param: string): string => {
30-
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`interface.ts`/methodSignature20:
31-
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature2.
29+
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature2.
3230
// ^^^^^ definition local 7
3331
return param
3432
// ^^^^^ reference local 7

snapshots/output/syntax/src/property-assignment.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ export function shorthandPropertyAssignment() {
1313
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/a1:
1414
// ^ reference local 2
1515
}
16+
type A = { a: string }
17+
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#
18+
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#typeLiteral3:a.
19+
export function typedPropertyAssignment(): A {
20+
// ^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment.ts`/typedPropertyAssignment().
21+
// ^ reference syntax 1.0.0 src/`property-assignment.ts`/A#
22+
return { a: 'a' }
23+
// ^ reference syntax 1.0.0 src/`property-assignment.ts`/A#typeLiteral3:a.
24+
}
1625

snapshots/output/syntax/src/structural-type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function foo(): Promise<{ member: number }> {
1515
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
1616
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2018.promise.d.ts`/Promise#
1717
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
18-
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
18+
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/member0:
1919
}
2020
export function bar(): Promise<number> {
2121
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar().
@@ -61,5 +61,5 @@ export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type Fe
6161
export const fo: FeatureOptions = { darkMode: true }
6262
// ^^ definition syntax 1.0.0 src/`structural-type.ts`/fo.
6363
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
64-
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/darkMode0:
64+
// ^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#typeLiteral13:darkMode.
6565

src/FileIndexer.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,39 @@ export class FileIndexer {
150150
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
151151
const range = Range.fromNode(node).toLsif()
152152
let role = 0
153-
const isDefinitionNode = isDefinition(node)
153+
let isDefinitionNode = isDefinition(node)
154+
let declarations: ts.Node[] = [];
155+
if (ts.isPropertyAssignment(node.parent)) {
156+
const contextualType = this.checker.getContextualType(node.parent.parent);
157+
if (contextualType != null) {
158+
const property = contextualType.getProperty(node.getText());
159+
if (property != null) {
160+
const decls = property.getDeclarations()
161+
if (decls != null && decls.length > 0) {
162+
isDefinitionNode = false;
163+
declarations = decls;
164+
}
165+
}
166+
}
167+
}
168+
154169
if (isDefinitionNode) {
155170
role |= scip.scip.SymbolRole.Definition
156171
}
157-
const declarations = ts.isConstructorDeclaration(node)
158-
? [node]
159-
: isDefinitionNode
160-
? // Don't emit ambiguous definition at definition-site. You can reproduce
161-
// ambiguous results by triggering "Go to definition" in VS Code on `Conflict`
162-
// in the example below:
163-
// export const Conflict = 42
164-
// export interface Conflict {}
165-
// ^^^^^^^^ "Go to definition" shows two results: const and interface.
166-
// See https://github.com/sourcegraph/scip-typescript/pull/206 for more details.
167-
[node.parent]
168-
: sym?.declarations || []
172+
if (declarations.length === 0) {
173+
declarations = ts.isConstructorDeclaration(node)
174+
? [node]
175+
: isDefinitionNode
176+
? // Don't emit ambiguous definition at definition-site. You can reproduce
177+
// ambiguous results by triggering "Go to definition" in VS Code on `Conflict`
178+
// in the example below:
179+
// export const Conflict = 42
180+
// export interface Conflict {}
181+
// ^^^^^^^^ "Go to definition" shows two results: const and interface.
182+
// See https://github.com/sourcegraph/scip-typescript/pull/206 for more details.
183+
[node.parent]
184+
: sym?.declarations || []
185+
}
169186
for (const declaration of declarations) {
170187
let scipSymbol = this.scipSymbol(declaration)
171188

0 commit comments

Comments
 (0)