Skip to content

Commit 3aad06a

Browse files
authored
(fix) more robust source mapper + definition fallback (#984)
- Check for undefined in source mapper and return -1 if it can't find a match - fall back to start of file for definitions that can't be mapped
1 parent 11bf618 commit 3aad06a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/typescript-plugin/src/language-service/definition.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ export function decorateGetDefinition(
2323
return def;
2424
}
2525

26-
const textSpan = snapshotManager
26+
let textSpan = snapshotManager
2727
.get(def.fileName)
2828
?.getOriginalTextSpan(def.textSpan);
2929
if (!textSpan) {
30-
return undefined;
30+
// Unmapped positions are for example the default export.
31+
// Fall back to the start of the file to at least go to the correct file.
32+
textSpan = { start: 0, length: 1 };
3133
}
3234
return {
3335
...def,

packages/typescript-plugin/src/source-mapper.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ export class SourceMapper {
7878
}
7979

8080
const closestMatch = binarySearch(lineMap, position.character, 0);
81-
const { 2: line, 3: character } = lineMap[closestMatch];
81+
const match = lineMap[closestMatch];
82+
if (!match) {
83+
return { line: -1, character: -1 };
84+
}
85+
86+
const { 2: line, 3: character } = match;
8287
return { line, character };
8388
}
8489

@@ -90,7 +95,12 @@ export class SourceMapper {
9095
}
9196

9297
const closestMatch = binarySearch(lineMap, position.character, 0);
93-
const { 1: line, 2: character } = lineMap[closestMatch];
98+
const match = lineMap[closestMatch];
99+
if (!match) {
100+
return { line: -1, character: -1 };
101+
}
102+
103+
const { 1: line, 2: character } = match;
94104
return { line, character };
95105
}
96106

0 commit comments

Comments
 (0)