-
Notifications
You must be signed in to change notification settings - Fork 20
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b64af4a
WIP - hacking on adding constructor symbols
olafurpg fbc2707
Handle constructors properly
SuperAuguste c60d510
Address reviews, consolidate code
SuperAuguste caa792d
Format code
SuperAuguste b238c58
Fix constructorless classes
SuperAuguste c597163
Fix yarn issues, mention yarn version
SuperAuguste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
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() | ||
} |
This file contains 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
This file contains 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
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 🤩 |
||
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/NoConstructor# | ||
} | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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) | ||
|
@@ -76,6 +78,7 @@ export class FileIndexer { | |
this.visitSymbolOccurrence(node, sym) | ||
} | ||
} | ||
|
||
ts.forEachChild(node, node => this.visit(node)) | ||
} | ||
|
||
|
@@ -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. | ||
|
@@ -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: | ||
|
@@ -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 | ||
|
@@ -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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -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() | ||
|
@@ -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) | ||
) | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Followup #251