Skip to content

Commit fbc2707

Browse files
committed
Handle constructors properly
1 parent b64af4a commit fbc2707

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

snapshots/input/syntax/src/constructor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export class Constructor {
33
}
44

55
export function useConstructor(): Constructor {
6-
return new Constructor(42)
6+
return new Constructor(Constructor.name.length)
77
}

snapshots/output/syntax/src/constructor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Constructor#
55
// documentation ```ts\nclass Constructor\n```
66
constructor(public readonly property: number) {}
7-
// ^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Constructor#
7+
// ^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Constructor#`<constructor>`().
8+
// documentation ```ts\nconstructor(property: number): Constructor\n```
9+
// relationship scip-typescript npm syntax 1.0.0 src/`constructor.ts`/Constructor#`<constructor>`().
810
// ^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/Constructor#`<constructor>`().(property)
911
// documentation ```ts\n(property) property: number\n```
1012
}
@@ -13,7 +15,10 @@
1315
// ^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useConstructor().
1416
// documentation ```ts\nfunction useConstructor(): Constructor\n```
1517
// ^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Constructor#
16-
return new Constructor(42)
17-
// ^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Constructor#
18+
return new Constructor(Constructor.name.length)
19+
// ^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Constructor#`<constructor>`().
20+
// ^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Constructor#
21+
// ^^^^ reference typescript 4.8.4 lib/`lib.es2015.core.d.ts`/Function#name.
22+
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/String#length.
1823
}
1924

src/FileIndexer.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,35 @@ export class FileIndexer {
7373
ts.isStringLiteralLike(node)
7474
) {
7575
const sym = this.getTSSymbolAtLocation(node)
76-
if (ts.isConstructorDeclaration(node)) {
77-
console.log({ sym })
78-
}
7976
if (sym) {
8077
this.visitSymbolOccurrence(node, sym)
8178
}
8279
}
80+
81+
if (
82+
ts.isNewExpression(node) &&
83+
ts.isIdentifier(node.expression)
84+
) {
85+
const sym = this.getTSSymbolAtLocation(node.expression)
86+
const declaration = sym?.declarations?.at(0);
87+
if (declaration && ts.isClassDeclaration(declaration)) {
88+
for (const member of declaration.members) {
89+
if (ts.isConstructorDeclaration(member)) {
90+
const scipSymbol = this.scipSymbol(member)
91+
this.pushOccurrence(new scip.scip.Occurrence({
92+
range: Range.fromNode(node.expression).toLsif(),
93+
symbol: scipSymbol.value,
94+
}))
95+
break
96+
}
97+
}
98+
}
99+
100+
node.typeArguments ? node.typeArguments.forEach(node => this.visit(node)) : void{}
101+
node.arguments ? node.arguments.forEach(node => this.visit(node)) : void{}
102+
return
103+
}
104+
83105
ts.forEachChild(node, node => this.visit(node))
84106
}
85107

@@ -119,7 +141,7 @@ export class FileIndexer {
119141
if (isDefinitionNode) {
120142
role |= scip.scip.SymbolRole.Definition
121143
}
122-
const declarations = isDefinitionNode
144+
const declarations = ts.isConstructorDeclaration(node) ? [node] : isDefinitionNode
123145
? // Don't emit ambiguous definition at definition-site. You can reproduce
124146
// ambiguous results by triggering "Go to definition" in VS Code on `Conflict`
125147
// in the example below:
@@ -312,6 +334,13 @@ export class FileIndexer {
312334
}
313335
}
314336
})
337+
} else if (ts.isConstructorDeclaration(declaration)) {
338+
relationships.push(
339+
new scip.scip.Relationship({
340+
symbol: declarationSymbol.value,
341+
is_definition: true,
342+
})
343+
)
315344
}
316345
return relationships
317346
}
@@ -487,7 +516,9 @@ export class FileIndexer {
487516
return undefined
488517
}
489518
const signatureDeclaration: ts.SignatureDeclaration | undefined =
490-
ts.isFunctionDeclaration(declaration)
519+
ts.isConstructorDeclaration(node)
520+
? node
521+
: ts.isFunctionDeclaration(declaration)
491522
? declaration
492523
: ts.isMethodDeclaration(declaration)
493524
? declaration
@@ -515,6 +546,9 @@ export class FileIndexer {
515546
return 'type ' + node.getText()
516547
case ts.ScriptElementKind.classElement:
517548
case ts.ScriptElementKind.localClassElement:
549+
if (ts.isConstructorDeclaration(node)) {
550+
return 'constructor' + signature()
551+
}
518552
return 'class ' + node.getText()
519553
case ts.ScriptElementKind.interfaceElement:
520554
return 'interface ' + node.getText()
@@ -776,5 +810,5 @@ function declarationName(node: ts.Node): ts.Node | undefined {
776810
* ^^^^^^^^^^^^^^^^^^^^^ node.parent
777811
*/
778812
function isDefinition(node: ts.Node): boolean {
779-
return declarationName(node.parent) === node
813+
return declarationName(node.parent) === node || ts.isConstructorDeclaration(node)
780814
}

0 commit comments

Comments
 (0)