Skip to content

Commit 1daf86c

Browse files
authored
fix(graph): Support enclosing range for constructors (#372)
* Add supporting range support for the constructor * revert mistakenly pushed file * Add missing return type * PR feedback and simplification * update comment
1 parent a4ab890 commit 1daf86c

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

.vscode/launch.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Debug Tests",
8+
"runtimeExecutable": "npm",
9+
"runtimeArgs": ["run", "test"]
10+
}
11+
]
12+
}

snapshots/output/enclosing-ranges/range.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function test2() {
3434
// < start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#
3535
class Test {
3636
// ^^^^ definition enclosing-ranges 0.0.1 `range.js`/Test#
37+
// ⌄ start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().
3738
constructor() {
3839
//^^^^^^^^^^^ definition enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().
3940
const a = 'a'
@@ -45,6 +46,7 @@ class Test {
4546
// ^ reference local 14
4647
// ^ reference local 17
4748
}
49+
// ^ end enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().
4850

4951
// ⌄ start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#test().
5052
test() {

src/FileIndexer.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ export class FileIndexer {
166166
}
167167

168168
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
169-
const range = Range.fromNode(node).toLsif()
169+
const isConstructor = ts.isConstructorDeclaration(node)
170+
// For constructors, this method is passed the declaration node and not the identifier node.
171+
// In either case, this method needs to get the range of the "name" of the declaration, for constructors we
172+
// get the firstToken which contains the text "constructor".
173+
const range = Range.fromNode(
174+
isConstructor ? node.getFirstToken() ?? node : node
175+
).toLsif()
170176
let role = 0
171177
let declarations: ts.Node[] =
172178
this.getDeclarationsForPropertyAssignment(node) ?? []
@@ -207,7 +213,8 @@ export class FileIndexer {
207213
ts.isTypeAliasDeclaration(declaration) ||
208214
ts.isClassDeclaration(declaration) ||
209215
ts.isMethodDeclaration(declaration) ||
210-
ts.isInterfaceDeclaration(declaration)
216+
ts.isInterfaceDeclaration(declaration) ||
217+
ts.isConstructorDeclaration(declaration)
211218
) {
212219
enclosingRange = Range.fromNode(declaration).toLsif()
213220
}

src/Range.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ export class Range {
3333
new Position(endLine, endCharacter)
3434
)
3535
}
36+
3637
public static fromNode(node: ts.Node): Range {
3738
const sourceFile = node.getSourceFile()
38-
const rangeNode: ts.Node = ts.isConstructorDeclaration(node)
39-
? node.getFirstToken() ?? node
40-
: node
41-
const start = sourceFile.getLineAndCharacterOfPosition(rangeNode.getStart())
42-
const end = sourceFile.getLineAndCharacterOfPosition(rangeNode.getEnd())
39+
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart())
40+
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd())
4341
return new Range(
4442
new Position(start.line, start.character),
4543
new Position(end.line, end.character)
4644
)
4745
}
46+
4847
public isSingleLine(): boolean {
4948
return this.start.line === this.end.line
5049
}

0 commit comments

Comments
 (0)