Skip to content
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

Add definitions and references for constructors #250

Merged
merged 6 commits into from
Apr 26, 2023
Merged
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
2 changes: 2 additions & 0 deletions Development.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Developing scip-typescript

Please note that the yarn version used by CI is `v1.22.19` - you should use this version as well to prevent lockfile conflicts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup #251


## References

- VS Code is a good reference point for the "correct" behavior
Expand Down
35 changes: 35 additions & 0 deletions snapshots/input/syntax/src/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Yay {
export class SuperConstructor {
constructor(public readonly property: number) {}
}

export namespace Woo {
export class MyClass {
constructor() {}
}
}
}

export class SuperConstructor2 {
constructor(public readonly property: number) {}
}

export function useConstructor(): Yay.SuperConstructor {
return new Yay.SuperConstructor(10)
}

export function useConstructor2(): SuperConstructor2 {
return new SuperConstructor2(10)
}

export function useConstructor3(): Yay.Woo.MyClass {
return new Yay.Woo.MyClass()
}

export class NoConstructor {
property: number
}

export function useNoConstructor() {
return new NoConstructor()
}
4 changes: 3 additions & 1 deletion snapshots/output/syntax/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`class.ts`/Class#classProperty.
// documentation ```ts\n(property) classProperty: string\n```
constructor(constructorParam: string) {
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`class.ts`/Class#`<constructor>`().
// documentation ```ts\nconstructor(constructorParam: string): Class\n```
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`class.ts`/Class#`<constructor>`().(constructorParam)
// documentation ```ts\n(parameter) constructorParam: string\n```
this.classProperty = constructorParam
Expand Down Expand Up @@ -48,7 +50,7 @@
const instance = new Class(param).classProperty
// ^^^^^^^^ definition local 2
// documentation ```ts\nvar instance: string\n```
// ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#
// ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#`<constructor>`().
// ^^^^^ reference syntax 1.0.0 src/`class.ts`/newClass().(param)
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#classProperty.
const instance2 = Class.staticMethod(param)
Expand Down
83 changes: 83 additions & 0 deletions snapshots/output/syntax/src/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace Yay {
// definition syntax 1.0.0 src/`constructor.ts`/
//documentation ```ts\nmodule "constructor.ts"\n```
// ^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/
// documentation ```ts\nYay: typeof Yay\n```
export class SuperConstructor {
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/SuperConstructor#
// documentation ```ts\nclass SuperConstructor\n```
constructor(public readonly property: number) {}
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/SuperConstructor#`<constructor>`().
// documentation ```ts\nconstructor(property: number): SuperConstructor\n```
// ^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/SuperConstructor#`<constructor>`().(property)
// documentation ```ts\n(property) property: number\n```
}

export namespace Woo {
// ^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/Woo/
// documentation ```ts\nWoo: typeof Woo\n```
export class MyClass {
// ^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/Woo/MyClass#
// documentation ```ts\nclass MyClass\n```
constructor() {}
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Yay/Woo/MyClass#`<constructor>`().
// documentation ```ts\nconstructor(): MyClass\n```
}
}
}

export class SuperConstructor2 {
// ^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/SuperConstructor2#
// documentation ```ts\nclass SuperConstructor2\n```
constructor(public readonly property: number) {}
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/SuperConstructor2#`<constructor>`().
// documentation ```ts\nconstructor(property: number): SuperConstructor2\n```
// ^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/SuperConstructor2#`<constructor>`().(property)
// documentation ```ts\n(property) property: number\n```
}

export function useConstructor(): Yay.SuperConstructor {
// ^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useConstructor().
// documentation ```ts\nfunction useConstructor(): SuperConstructor\n```
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/SuperConstructor#
return new Yay.SuperConstructor(10)
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/SuperConstructor#`<constructor>`().
}

export function useConstructor2(): SuperConstructor2 {
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useConstructor2().
// documentation ```ts\nfunction useConstructor2(): SuperConstructor2\n```
// ^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/SuperConstructor2#
return new SuperConstructor2(10)
// ^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/SuperConstructor2#`<constructor>`().
}

export function useConstructor3(): Yay.Woo.MyClass {
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useConstructor3().
// documentation ```ts\nfunction useConstructor3(): MyClass\n```
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/Woo/
// ^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/Woo/MyClass#
return new Yay.Woo.MyClass()
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/
// ^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/Woo/
// ^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/Woo/MyClass#`<constructor>`().
}

export class NoConstructor {
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/NoConstructor#
// documentation ```ts\nclass NoConstructor\n```
property: number
// ^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/NoConstructor#property.
// documentation ```ts\n(property) property: number\n```
}

export function useNoConstructor() {
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useNoConstructor().
// documentation ```ts\nfunction useNoConstructor(): NoConstructor\n```
return new NoConstructor()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🤩

// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/NoConstructor#
}

2 changes: 1 addition & 1 deletion snapshots/output/syntax/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// documentation ```ts\nfunction useEverything(): string\n```
return (
new Class('a').classProperty +
// ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#
// ^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#`<constructor>`().
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`class.ts`/Class#classProperty.
renamedInterface().methodSignature('a') +
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`interface.ts`/newInterface().
Expand Down
74 changes: 62 additions & 12 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export class FileIndexer {
public readonly input: Input,
public readonly document: scip.scip.Document,
public readonly globalSymbolTable: Map<ts.Node, ScipSymbol>,
public readonly globalConstructorTable: Map<ts.ClassDeclaration, boolean>,
public readonly packages: Packages,
public readonly sourceFile: ts.SourceFile
) {
this.workingDirectoryRegExp = new RegExp(options.cwd, 'g')
}
public index(): void {
// Uncomment below if you want to skip certain files for local development.
// if (!this.sourceFile.fileName.includes('infer-relationship')) {
// if (!this.sourceFile.fileName.includes('constructor')) {
// return
// }
this.emitSourceFileOccurrence()
Expand Down Expand Up @@ -67,6 +68,7 @@ export class FileIndexer {
}
private visit(node: ts.Node): void {
if (
ts.isConstructorDeclaration(node) ||
ts.isIdentifier(node) ||
ts.isPrivateIdentifier(node) ||
ts.isStringLiteralLike(node)
Expand All @@ -76,6 +78,7 @@ export class FileIndexer {
this.visitSymbolOccurrence(node, sym)
}
}

ts.forEachChild(node, node => this.visit(node))
}

Expand All @@ -84,7 +87,10 @@ export class FileIndexer {
//
// This code is directly based off src/services/goToDefinition.ts.
private getTSSymbolAtLocation(node: ts.Node): ts.Symbol | undefined {
const symbol = this.checker.getSymbolAtLocation(node)
const rangeNode: ts.Node = ts.isConstructorDeclaration(node)
? node.getFirstToken() ?? node
: node
const symbol = this.checker.getSymbolAtLocation(rangeNode)

// If this is an alias, and the request came at the declaration location
// get the aliased symbol instead. This allows for goto def on an import e.g.
Expand All @@ -105,14 +111,33 @@ export class FileIndexer {
return symbol
}

private hasConstructor(classDeclaration: ts.ClassDeclaration): boolean {
const cached = this.globalConstructorTable.get(classDeclaration)
if (cached !== undefined) {
return cached
}

for (const member of classDeclaration.members) {
if (ts.isConstructorDeclaration(member)) {
this.globalConstructorTable.set(classDeclaration, true)
return true
}
}

this.globalConstructorTable.set(classDeclaration, false)
return false
}

private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
const range = Range.fromNode(node).toLsif()
let role = 0
const isDefinitionNode = isDefinition(node)
if (isDefinitionNode) {
role |= scip.scip.SymbolRole.Definition
}
const declarations = isDefinitionNode
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:
Expand All @@ -123,7 +148,20 @@ export class FileIndexer {
[node.parent]
: sym?.declarations || []
for (const declaration of declarations) {
const scipSymbol = this.scipSymbol(declaration)
let scipSymbol = this.scipSymbol(declaration)

if (
((ts.isIdentifier(node) && ts.isNewExpression(node.parent)) ||
(ts.isPropertyAccessExpression(node.parent) &&
ts.isNewExpression(node.parent.parent))) &&
ts.isClassDeclaration(declaration) &&
this.hasConstructor(declaration)
) {
scipSymbol = ScipSymbol.global(
scipSymbol,
methodDescriptor('<constructor>')
)
}

if (scipSymbol.isEmpty()) {
// Skip empty symbols
Expand Down Expand Up @@ -474,17 +512,24 @@ export class FileIndexer {
const kind = scriptElementKind(node, sym)
const type = (): string =>
this.checker.typeToString(this.checker.getTypeAtLocation(node))
const signature = (): string | undefined => {
const asSignatureDeclaration = (
node: ts.Node,
sym: ts.Symbol
): ts.SignatureDeclaration | undefined => {
const declaration = sym.declarations?.[0]
if (!declaration) {
return undefined
}
const signatureDeclaration: ts.SignatureDeclaration | undefined =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't realized this was already inside a function 🤦🏻 The new code looks good though 👍🏻

ts.isFunctionDeclaration(declaration)
? declaration
: ts.isMethodDeclaration(declaration)
? declaration
: undefined
return ts.isConstructorDeclaration(node)
? node
: ts.isFunctionDeclaration(declaration)
? declaration
: ts.isMethodDeclaration(declaration)
? declaration
: undefined
}
const signature = (): string | undefined => {
const signatureDeclaration = asSignatureDeclaration(node, sym)
if (!signatureDeclaration) {
return undefined
}
Expand All @@ -508,6 +553,9 @@ export class FileIndexer {
return 'type ' + node.getText()
case ts.ScriptElementKind.classElement:
case ts.ScriptElementKind.localClassElement:
if (ts.isConstructorDeclaration(node)) {
return 'constructor' + (signature() || '')
}
return 'class ' + node.getText()
case ts.ScriptElementKind.interfaceElement:
return 'interface ' + node.getText()
Expand Down Expand Up @@ -769,5 +817,7 @@ function declarationName(node: ts.Node): ts.Node | undefined {
* ^^^^^^^^^^^^^^^^^^^^^ node.parent
*/
function isDefinition(node: ts.Node): boolean {
return declarationName(node.parent) === node
return (
declarationName(node.parent) === node || ts.isConstructorDeclaration(node)
)
}
2 changes: 2 additions & 0 deletions src/ProjectIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class ProjectIndexer {
private program: ts.Program
private checker: ts.TypeChecker
private symbolCache: Map<ts.Node, ScipSymbol> = new Map()
private hasConstructor: Map<ts.ClassDeclaration, boolean> = new Map()
private packages: Packages
constructor(
public readonly config: ts.ParsedCommandLine,
Expand Down Expand Up @@ -140,6 +141,7 @@ export class ProjectIndexer {
input,
document,
this.symbolCache,
this.hasConstructor,
this.packages,
sourceFile
)
Expand Down
7 changes: 5 additions & 2 deletions src/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class Range {
}
public static fromNode(node: ts.Node): Range {
const sourceFile = node.getSourceFile()
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart())
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd())
const rangeNode: ts.Node = ts.isConstructorDeclaration(node)
? node.getFirstToken() ?? node
: node
const start = sourceFile.getLineAndCharacterOfPosition(rangeNode.getStart())
const end = sourceFile.getLineAndCharacterOfPosition(rangeNode.getEnd())
return new Range(
new Position(start.line, start.character),
new Position(end.line, end.character)
Expand Down