Skip to content

Commit b238c58

Browse files
committed
Fix constructorless classes
1 parent caa792d commit b238c58

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

snapshots/input/syntax/src/constructor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ export function useConstructor2(): SuperConstructor2 {
2525
export function useConstructor3(): Yay.Woo.MyClass {
2626
return new Yay.Woo.MyClass()
2727
}
28+
29+
export class NoConstructor {
30+
property: number
31+
}
32+
33+
export function useNoConstructor() {
34+
return new NoConstructor()
35+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { a } from '@example/a'
22
// definition @example/b 1.0.0 src/`b.ts`/
33
//documentation ```ts\nmodule "b.ts"\n```
4-
// ^ reference local 0
4+
// ^ reference @example/a 1.0.0 src/`a.ts`/a().
5+
// ^^^^^^^^^^^^ reference @example/a 1.0.0 src/`a.ts`/
56

67
export function b() {
78
// ^ definition @example/b 1.0.0 src/`b.ts`/b().
8-
// documentation ```ts\nfunction b(): any\n```
9+
// documentation ```ts\nfunction b(): string\n```
910
return a()
10-
// ^ reference local 0
11+
// ^ reference @example/a 1.0.0 src/`a.ts`/a().
1112
}
1213

snapshots/output/syntax/src/accessors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
const c = new C()
9898
// ^ definition local 2
9999
// documentation ```ts\nvar c: C\n```
100-
// ^ reference syntax 1.0.0 src/`accessors.ts`/C#`<constructor>`().
100+
// ^ reference syntax 1.0.0 src/`accessors.ts`/C#
101101
c.length = 10
102102
// ^ reference local 2
103103
// ^^^^^^ reference syntax 1.0.0 src/`accessors.ts`/C#`<get>length`().
@@ -120,7 +120,7 @@
120120
const d = new D()
121121
// ^ definition local 5
122122
// documentation ```ts\nvar d: D\n```
123-
// ^ reference syntax 1.0.0 src/`accessors.ts`/D#`<constructor>`().
123+
// ^ reference syntax 1.0.0 src/`accessors.ts`/D#
124124
d.length = 0
125125
// ^ reference local 5
126126
// ^^^^^^ reference syntax 1.0.0 src/`accessors.ts`/D#`<get>length`().

snapshots/output/syntax/src/constructor.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,18 @@
6666
// ^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/Yay/Woo/MyClass#`<constructor>`().
6767
}
6868

69+
export class NoConstructor {
70+
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/NoConstructor#
71+
// documentation ```ts\nclass NoConstructor\n```
72+
property: number
73+
// ^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/NoConstructor#property.
74+
// documentation ```ts\n(property) property: number\n```
75+
}
76+
77+
export function useNoConstructor() {
78+
// ^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`constructor.ts`/useNoConstructor().
79+
// documentation ```ts\nfunction useNoConstructor(): NoConstructor\n```
80+
return new NoConstructor()
81+
// ^^^^^^^^^^^^^ reference syntax 1.0.0 src/`constructor.ts`/NoConstructor#
82+
}
83+

snapshots/output/syntax/src/type-alias.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// ^^ definition syntax 1.0.0 src/`type-alias.ts`/cs.
2929
// documentation ```ts\nvar cs: Cstring\n```
3030
// ^^^^^^^ reference syntax 1.0.0 src/`type-alias.ts`/Cstring#
31-
// ^ reference syntax 1.0.0 src/`type-alias.ts`/C#`<constructor>`().
31+
// ^ reference syntax 1.0.0 src/`type-alias.ts`/C#
3232

3333
class D<T, U> {
3434
// ^ definition syntax 1.0.0 src/`type-alias.ts`/D#
@@ -66,10 +66,10 @@
6666
// ^^ definition syntax 1.0.0 src/`type-alias.ts`/dt.
6767
// documentation ```ts\nvar dt: DT<string>\n```
6868
// ^^ reference syntax 1.0.0 src/`type-alias.ts`/DT#
69-
// ^ reference syntax 1.0.0 src/`type-alias.ts`/D#`<constructor>`().
69+
// ^ reference syntax 1.0.0 src/`type-alias.ts`/D#
7070
const du: DU<string> = new D()
7171
// ^^ definition syntax 1.0.0 src/`type-alias.ts`/du.
7272
// documentation ```ts\nvar du: DU<string>\n```
7373
// ^^ reference syntax 1.0.0 src/`type-alias.ts`/DU#
74-
// ^ reference syntax 1.0.0 src/`type-alias.ts`/D#`<constructor>`().
74+
// ^ reference syntax 1.0.0 src/`type-alias.ts`/D#
7575

src/FileIndexer.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class FileIndexer {
3131
public readonly input: Input,
3232
public readonly document: scip.scip.Document,
3333
public readonly globalSymbolTable: Map<ts.Node, ScipSymbol>,
34+
public readonly globalConstructorTable: Map<ts.ClassDeclaration, boolean>,
3435
public readonly packages: Packages,
3536
public readonly sourceFile: ts.SourceFile
3637
) {
@@ -110,6 +111,21 @@ export class FileIndexer {
110111
return symbol
111112
}
112113

114+
private hasConstructor(classDeclaration: ts.ClassDeclaration): boolean {
115+
const cached = this.globalConstructorTable.get(classDeclaration)
116+
if (cached !== undefined) return cached
117+
118+
for (const member of classDeclaration.members) {
119+
if (ts.isConstructorDeclaration(member)) {
120+
this.globalConstructorTable.set(classDeclaration, true)
121+
return true
122+
}
123+
}
124+
125+
this.globalConstructorTable.set(classDeclaration, false)
126+
return false
127+
}
128+
113129
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
114130
const range = Range.fromNode(node).toLsif()
115131
let role = 0
@@ -136,7 +152,8 @@ export class FileIndexer {
136152
((ts.isIdentifier(node) && ts.isNewExpression(node.parent)) ||
137153
(ts.isPropertyAccessExpression(node.parent) &&
138154
ts.isNewExpression(node.parent.parent))) &&
139-
ts.isClassDeclaration(declaration)
155+
ts.isClassDeclaration(declaration) &&
156+
this.hasConstructor(declaration)
140157
) {
141158
scipSymbol = ScipSymbol.global(
142159
scipSymbol,

src/ProjectIndexer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class ProjectIndexer {
7272
private program: ts.Program
7373
private checker: ts.TypeChecker
7474
private symbolCache: Map<ts.Node, ScipSymbol> = new Map()
75+
private hasConstructor: Map<ts.ClassDeclaration, boolean> = new Map()
7576
private packages: Packages
7677
constructor(
7778
public readonly config: ts.ParsedCommandLine,
@@ -140,6 +141,7 @@ export class ProjectIndexer {
140141
input,
141142
document,
142143
this.symbolCache,
144+
this.hasConstructor,
143145
this.packages,
144146
sourceFile
145147
)

0 commit comments

Comments
 (0)