Skip to content

fix: resolve property assignments in object literals #373

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 11 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nodejs 20.8.1
pnpm 8.9.2
yarn 1.22.17
4 changes: 4 additions & 0 deletions snapshots/input/pure-js/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ function var_function_scope() {
}
print_fib(k)
}

function array_of_objects() {
[{ element: 0 }, { element: 1}]
}
4 changes: 4 additions & 0 deletions snapshots/input/syntax/src/property-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export function shorthandPropertyAssignment() {
const a = 'a'
return { a }
}
type A = { a: string }
export function typedPropertyAssignment(): A {
return { a: 'a' }
}
7 changes: 7 additions & 0 deletions snapshots/output/pure-js/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,10 @@ function var_function_scope() {
// ^ reference local 17
}

function array_of_objects() {
// ^^^^^^^^^^^^^^^^ definition pure-js 1.0.0 src/`main.js`/array_of_objects().
[{ element: 0 }, { element: 1}]
// ^^^^^^^ definition pure-js 1.0.0 src/`main.js`/element0:
// ^^^^^^^ definition pure-js 1.0.0 src/`main.js`/element1:
}

3 changes: 1 addition & 2 deletions snapshots/output/syntax/src/destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ interface Props {
const props: Props = { a: 42 }
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/props.
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
// ^ definition syntax 1.0.0 src/`destructuring.ts`/a0:
// relationship implementation reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.

export function objectDestructuring(): number[] {
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/objectDestructuring().
Expand Down
23 changes: 8 additions & 15 deletions snapshots/output/syntax/src/infer-relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export function returnStatement(): Configuration {
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property0:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
for (let i = 0; i < 9; i++) {
Expand All @@ -35,8 +34,7 @@ export function returnStatement(): Configuration {
// ^ reference local 2
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property1:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
}
Expand All @@ -47,8 +45,7 @@ export function returnStatement(): Configuration {
// ^ reference local 5
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property2:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
}
Expand All @@ -65,35 +62,31 @@ export function returnStatement(): Configuration {
// ^ reference local 8
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property3:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
}
while (random() < 0) {
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property4:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
do {
if (random() > 0) {
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().
return {
property: 41,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property5:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}
} while (random() < 0)
// ^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/random().

return {
property: 42,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property6:
// relationship implementation reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
}
}

Expand All @@ -109,7 +102,7 @@ export function returnStatementInsideArgumentExpression(): Configuration[] {
// ^^^^^^ reference local 12
return {
property: incremented,
// ^^^^^^^^ definition syntax 1.0.0 src/`infer-relationship.ts`/property7:
// ^^^^^^^^ reference syntax 1.0.0 src/`infer-relationship.ts`/Configuration#property.
// ^^^^^^^^^^^ reference local 15
}
})
Expand Down
12 changes: 4 additions & 8 deletions snapshots/output/syntax/src/inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ export const objectLiteralImplementation: Superinterface = {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/objectLiteralImplementation.
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#
property: 'property',
//^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/property0:
//relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
//^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
interfaceMethod: (): string => {
//^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/interfaceMethod0:
//relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
//^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
throw new Error('Function not implemented.')
// ^^^^^ reference typescript 5.3.3 lib/`lib.es5.d.ts`/Error#
// ^^^^^ reference typescript 5.3.3 lib/`lib.es5.d.ts`/Error.
Expand All @@ -112,11 +110,9 @@ export function infersInterface(): void {
consumesInterface({
//^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/consumesInterface().
interfaceMethod: (): string => 'inferred',
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/interfaceMethod1:
// relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
// ^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#interfaceMethod().
property: 'inferred',
// ^^^^^^^^ definition syntax 1.0.0 src/`inheritance.ts`/property1:
// relationship implementation reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/Superinterface#property.
})
}

6 changes: 2 additions & 4 deletions snapshots/output/syntax/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export function newInterface(): Interface {
// ^^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#
return {
property: 'a',
// ^^^^^^^^ definition syntax 1.0.0 src/`interface.ts`/property0:
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#property.
// ^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#property.
methodSignature(param: string): string {
// ^^^^^^^^^^^^^^^ definition local 4
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature().
Expand All @@ -27,8 +26,7 @@ export function newInterface(): Interface {
// ^^^^^ reference local 5
},
methodSignature2: (param: string): string => {
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`interface.ts`/methodSignature20:
// relationship implementation reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature2.
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/Interface#methodSignature2.
// ^^^^^ definition local 7
return param
// ^^^^^ reference local 7
Expand Down
9 changes: 9 additions & 0 deletions snapshots/output/syntax/src/property-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ export function shorthandPropertyAssignment() {
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/a1:
// ^ reference local 2
}
type A = { a: string }
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#typeLiteral3:a.
export function typedPropertyAssignment(): A {
// ^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment.ts`/typedPropertyAssignment().
// ^ reference syntax 1.0.0 src/`property-assignment.ts`/A#
return { a: 'a' }
// ^ reference syntax 1.0.0 src/`property-assignment.ts`/A#typeLiteral3:a.
}

4 changes: 2 additions & 2 deletions snapshots/output/syntax/src/structural-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function foo(): Promise<{ member: number }> {
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2018.promise.d.ts`/Promise#
// ^^^^^^^ reference typescript 5.3.3 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/member0:
}
export function bar(): Promise<number> {
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar().
Expand Down Expand Up @@ -61,5 +61,5 @@ export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type Fe
export const fo: FeatureOptions = { darkMode: true }
// ^^ definition syntax 1.0.0 src/`structural-type.ts`/fo.
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/darkMode0:
// ^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#typeLiteral13:darkMode.

43 changes: 30 additions & 13 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,39 @@ export class FileIndexer {
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
const range = Range.fromNode(node).toLsif()
let role = 0
const isDefinitionNode = isDefinition(node)
let isDefinitionNode = isDefinition(node)
let declarations: ts.Node[] = [];
if (ts.isPropertyAssignment(node.parent)) {
const contextualType = this.checker.getContextualType(node.parent.parent);
if (contextualType != null) {
const property = contextualType.getProperty(node.getText());
if (property != null) {
const decls = property.getDeclarations()
if (decls != null && decls.length > 0) {
isDefinitionNode = false;
declarations = decls;
}
}
}
}

if (isDefinitionNode) {
role |= scip.scip.SymbolRole.Definition
}
const declarations = ts.isConstructorDeclaration(node)
? [node]
: isDefinitionNode
? // Don't emit ambiguous definition at definition-site. You can reproduce
// ambiguous results by triggering "Go to definition" in VS Code on `Conflict`
// in the example below:
// export const Conflict = 42
// export interface Conflict {}
// ^^^^^^^^ "Go to definition" shows two results: const and interface.
// See https://github.com/sourcegraph/scip-typescript/pull/206 for more details.
[node.parent]
: sym?.declarations || []
if (declarations.length === 0) {
declarations = ts.isConstructorDeclaration(node)
? [node]
: isDefinitionNode
? // Don't emit ambiguous definition at definition-site. You can reproduce
// ambiguous results by triggering "Go to definition" in VS Code on `Conflict`
// in the example below:
// export const Conflict = 42
// export interface Conflict {}
// ^^^^^^^^ "Go to definition" shows two results: const and interface.
// See https://github.com/sourcegraph/scip-typescript/pull/206 for more details.
[node.parent]
: sym?.declarations || []
}
for (const declaration of declarations) {
let scipSymbol = this.scipSymbol(declaration)

Expand Down
Loading