Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions snapshots/input/syntax/src/property-assignment-reference.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {
importedShorthand,
propertyAssignment,
shorthandPropertyAssignment,
} from './property-assignment'

interface ContextualProperties {
importedShorthand: string
}

function acceptContext(_value: ContextualProperties): void {}

export function run(): string {
acceptContext({ importedShorthand })
return propertyAssignment().a + shorthandPropertyAssignment().a
}
1 change: 1 addition & 0 deletions snapshots/input/syntax/src/property-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function shorthandPropertyAssignment() {
const a = 'a'
return { a }
}
export const importedShorthand = 'value'
type A = { a: string; b: number }
export function typedPropertyAssignment(): A {
// prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ export function handleShorthand() {
//^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-call-signatures.ts`/consumesInterface().
interfaceMethod,
// ^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`reusable-types.ts`/Superinterface#interfaceMethod().
// ^^^^^^^^^^^^^^^ reference local 26
property,
// ^^^^^^^^ reference syntax 1.0.0 src/`reusable-types.ts`/Superinterface#property.
// ^^^^^^^^ reference local 23
})
}

17 changes: 17 additions & 0 deletions snapshots/output/syntax/src/property-assignment-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@
// < definition syntax 1.0.0 src/`property-assignment-reference.ts`/

import {
importedShorthand,
//^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/importedShorthand.
propertyAssignment,
//^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/propertyAssignment().
shorthandPropertyAssignment,
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/shorthandPropertyAssignment().
} from './property-assignment'
// ^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/

interface ContextualProperties {
// ^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment-reference.ts`/ContextualProperties#
importedShorthand: string
//^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment-reference.ts`/ContextualProperties#importedShorthand.
}

function acceptContext(_value: ContextualProperties): void {}
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment-reference.ts`/acceptContext().
// ^^^^^^ definition syntax 1.0.0 src/`property-assignment-reference.ts`/acceptContext().(_value)
// ^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment-reference.ts`/ContextualProperties#

export function run(): string {
// ^^^ definition syntax 1.0.0 src/`property-assignment-reference.ts`/run().
acceptContext({ importedShorthand })
//^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment-reference.ts`/acceptContext().
// ^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment-reference.ts`/ContextualProperties#importedShorthand.
// ^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/importedShorthand.
return propertyAssignment().a + shorthandPropertyAssignment().a
// ^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`property-assignment.ts`/propertyAssignment().
// ^ reference syntax 1.0.0 src/`property-assignment.ts`/a0:
Expand Down
2 changes: 2 additions & 0 deletions snapshots/output/syntax/src/property-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export function shorthandPropertyAssignment() {
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/a1:
// ^ reference local 2
}
export const importedShorthand = 'value'
// ^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`property-assignment.ts`/importedShorthand.
type A = { a: string; b: number }
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#
// ^ definition syntax 1.0.0 src/`property-assignment.ts`/A#typeLiteral3:a.
Expand Down
17 changes: 11 additions & 6 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ export class FileIndexer {
)
if (isDefinitionNode) {
this.addSymbolInformation(node, sym, declaration, scipSymbol)
this.handleShorthandPropertyDefinition(declaration, range)
this.handleObjectBindingPattern(node, range)
// Only emit one symbol for definitions sites, see https://github.com/sourcegraph/lsif-typescript/issues/45
break
}
}
this.handleShorthandPropertyValue(node.parent, range)
}

/**
Expand Down Expand Up @@ -293,28 +293,33 @@ export class FileIndexer {

/**
* Handles the special-case around shorthand property syntax so that we emit two occurrences instead of only one.
* Shorthand properties need two symbols because they both define a symbol and reference a symbol. For example:
* Shorthand properties need two symbols because the token is both a property and a reference to its value. For example:
* ```
* const a = 42
* const b = {a}
* // ^ both references the local const `a` and defines a new property
* const c = b.a
* // ^ reference to the property `a`, not the local const
* ```
* Contextually typed properties similarly reference both the contextual property
* and the local or imported value at the same range.
*/
private handleShorthandPropertyDefinition(
private handleShorthandPropertyValue(
declaration: ts.Node,
range: number[]
): void {
if (declaration.kind !== ts.SyntaxKind.ShorthandPropertyAssignment) {
if (!ts.isShorthandPropertyAssignment(declaration)) {
return
}
const valueSymbol =
let valueSymbol =
this.checker.getShorthandAssignmentValueSymbol(declaration)
if (!valueSymbol) {
return
}
for (const symbol of valueSymbol?.declarations || []) {
if (valueSymbol.flags & ts.SymbolFlags.Alias) {
valueSymbol = this.checker.getAliasedSymbol(valueSymbol)
}
for (const symbol of valueSymbol.declarations || []) {
const scipSymbol = this.scipSymbol(symbol)
if (scipSymbol.isEmpty()) {
continue
Expand Down
Loading