Skip to content

Commit

Permalink
(fix) deal with ranges with swapped start/end
Browse files Browse the repository at this point in the history
Due to source mapping, some ranges may be swapped: Start is end. Swap back in this case.
  • Loading branch information
dummdidumm authored Oct 28, 2020
1 parent d66d9ba commit 829cf87
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class DiagnosticsProviderImpl implements DiagnosticsProvider {
.map((diagnostic) => mapObjWithRangeToOriginal(fragment, diagnostic))
.filter(hasNoNegativeLines)
.filter(isNoFalsePositive(document.getText(), tsDoc))
.map(enhanceIfNecessary);
.map(enhanceIfNecessary)
.map(swapRangeStartEndIfNecessary);
}

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

return diagnostic;
}

/**
* Due to source mapping, some ranges may be swapped: Start is end. Swap back in this case.
*/
function swapRangeStartEndIfNecessary(diag: Diagnostic): Diagnostic {
if (
diag.range.end.line < diag.range.start.line ||
(diag.range.end.line === diag.range.start.line &&
diag.range.end.character < diag.range.start.character)
) {
const start = diag.range.start;
diag.range.start = diag.range.end;
diag.range.end = start;
}
return diag;
}

0 comments on commit 829cf87

Please sign in to comment.