Skip to content

Commit 7612fdc

Browse files
authored
(feat) show nearby locs for svelte-check output (#453)
Instead of 10 prev/next chars, show the previous/next line of the diagnostic and the diagnostic line itself, highlighting the diagnostic range #278
1 parent 690770e commit 7612fdc

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

packages/svelte-check/src/writers.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ export class HumanFriendlyWriter implements Writer {
3737
);
3838

3939
// Show some context around diagnostic range
40-
const startOffset = offsetAt(diagnostic.range.start, text);
41-
const endOffset = offsetAt(diagnostic.range.end, text);
42-
const codePrev = chalk.cyan(text.substring(Math.max(startOffset - 10, 0), startOffset));
43-
const codeHighlight = chalk.magenta(text.substring(startOffset, endOffset));
44-
const codePost = chalk.cyan(text.substring(endOffset, endOffset + 10));
45-
const code = codePrev + codeHighlight + codePost;
46-
let msg;
40+
const codePrevLine = this.getLine(diagnostic.range.start.line - 1, text);
41+
const codeLine = this.getCodeLine(diagnostic, text);
42+
const codeNextLine = this.getLine(diagnostic.range.end.line + 1, text);
43+
const code = codePrevLine + codeLine + codeNextLine;
4744

45+
let msg;
4846
if (this.isVerbose) {
4947
msg = `${diagnostic.message} ${source}\n${chalk.cyan(code)}`;
5048
} else {
@@ -63,6 +61,28 @@ export class HumanFriendlyWriter implements Writer {
6361
});
6462
}
6563

64+
private getCodeLine(diagnostic: Diagnostic, text: string) {
65+
const startOffset = offsetAt(diagnostic.range.start, text);
66+
const endOffset = offsetAt(diagnostic.range.end, text);
67+
const codePrev = text.substring(
68+
offsetAt({ line: diagnostic.range.start.line, character: 0 }, text),
69+
startOffset,
70+
);
71+
const codeHighlight = chalk.magenta(text.substring(startOffset, endOffset));
72+
const codePost = text.substring(
73+
endOffset,
74+
offsetAt({ line: diagnostic.range.end.line, character: Number.MAX_SAFE_INTEGER }, text),
75+
);
76+
return codePrev + codeHighlight + codePost;
77+
}
78+
79+
private getLine(line: number, text: string): string {
80+
return text.substring(
81+
offsetAt({ line, character: 0 }, text),
82+
offsetAt({ line, character: Number.MAX_SAFE_INTEGER }, text),
83+
);
84+
}
85+
6686
completion(_f: number, errorCount: number, warningCount: number) {
6787
this.stream.write('====================================\n');
6888

packages/svelte-check/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"strict": true,
66
"declaration": true,
77
"outDir": "dist",
8-
"composite": true
8+
"composite": true,
9+
"esModuleInterop": false
910
}
1011
}

0 commit comments

Comments
 (0)