Skip to content

Commit 829cf87

Browse files
authored
(fix) deal with ranges with swapped start/end
Due to source mapping, some ranges may be swapped: Start is end. Swap back in this case.
1 parent d66d9ba commit 829cf87

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/language-server/src/plugins/typescript/features/DiagnosticsProvider.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export class DiagnosticsProviderImpl implements DiagnosticsProvider {
4646
.map((diagnostic) => mapObjWithRangeToOriginal(fragment, diagnostic))
4747
.filter(hasNoNegativeLines)
4848
.filter(isNoFalsePositive(document.getText(), tsDoc))
49-
.map(enhanceIfNecessary);
49+
.map(enhanceIfNecessary)
50+
.map(swapRangeStartEndIfNecessary);
5051
}
5152

5253
private getDiagnosticTag(diagnostic: ts.Diagnostic) {
@@ -138,3 +139,19 @@ function enhanceIfNecessary(diagnostic: Diagnostic): Diagnostic {
138139

139140
return diagnostic;
140141
}
142+
143+
/**
144+
* Due to source mapping, some ranges may be swapped: Start is end. Swap back in this case.
145+
*/
146+
function swapRangeStartEndIfNecessary(diag: Diagnostic): Diagnostic {
147+
if (
148+
diag.range.end.line < diag.range.start.line ||
149+
(diag.range.end.line === diag.range.start.line &&
150+
diag.range.end.character < diag.range.start.character)
151+
) {
152+
const start = diag.range.start;
153+
diag.range.start = diag.range.end;
154+
diag.range.end = start;
155+
}
156+
return diag;
157+
}

0 commit comments

Comments
 (0)