Skip to content
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
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "test"]
}
]
}
2 changes: 2 additions & 0 deletions snapshots/output/enclosing-ranges/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function test2() {
// < start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#
class Test {
// ^^^^ definition enclosing-ranges 0.0.1 `range.js`/Test#
// ⌄ start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().
constructor() {
//^^^^^^^^^^^ definition enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().
const a = 'a'
Expand All @@ -45,6 +46,7 @@ class Test {
// ^ reference local 14
// ^ reference local 17
}
// ^ end enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#`<constructor>`().

// ⌄ start enclosing_range enclosing-ranges 0.0.1 `range.js`/Test#test().
test() {
Expand Down
11 changes: 9 additions & 2 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ export class FileIndexer {
}

private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
const range = Range.fromNode(node).toLsif()
const isConstructor = ts.isConstructorDeclaration(node)
// For constructors, this method is passed the declaration node and not the identifier node.
// In either case, this method needs to get the range of the "name" of the declaration, for constructors we
// get the firstToken which contains the text "constructor".
const range = Range.fromNode(
isConstructor ? node.getFirstToken() ?? node : node
).toLsif()
let role = 0
let declarations: ts.Node[] =
this.getDeclarationsForPropertyAssignment(node) ?? []
Expand Down Expand Up @@ -207,7 +213,8 @@ export class FileIndexer {
ts.isTypeAliasDeclaration(declaration) ||
ts.isClassDeclaration(declaration) ||
ts.isMethodDeclaration(declaration) ||
ts.isInterfaceDeclaration(declaration)
ts.isInterfaceDeclaration(declaration) ||
ts.isConstructorDeclaration(declaration)
) {
enclosingRange = Range.fromNode(declaration).toLsif()
}
Expand Down
9 changes: 4 additions & 5 deletions src/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ export class Range {
new Position(endLine, endCharacter)
)
}

public static fromNode(node: ts.Node): Range {
const sourceFile = node.getSourceFile()
const rangeNode: ts.Node = ts.isConstructorDeclaration(node)
? node.getFirstToken() ?? node
: node
const start = sourceFile.getLineAndCharacterOfPosition(rangeNode.getStart())
const end = sourceFile.getLineAndCharacterOfPosition(rangeNode.getEnd())
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart())
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd())
return new Range(
new Position(start.line, start.character),
new Position(end.line, end.character)
)
}

public isSingleLine(): boolean {
return this.start.line === this.end.line
}
Expand Down