Skip to content

Commit fe7719f

Browse files
committed
Disable check diagnostics per line
1 parent 3d03f8d commit fe7719f

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

src/compiler/program.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ts {
66
const emptyArray: any[] = [];
7+
const suppressDiagnosticCommentRegEx = /(^\s*$)|(^\s*\/\/\/?\s*(@ts-suppress)?)/;
78

89
export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName = "tsconfig.json"): string {
910
while (true) {
@@ -923,10 +924,36 @@ namespace ts {
923924
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
924925
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
925926

926-
return bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
927+
const diagnostics = bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
928+
return isSourceFileJavaScript(sourceFile)
929+
? filter(diagnostics, shouldReportDiagnostic)
930+
: diagnostics;
927931
});
928932
}
929933

934+
/**
935+
* Skip errors if previous line start with '// @ts-suppress' comment, not counting non-empty non-comment lines
936+
*/
937+
function shouldReportDiagnostic(diagnostic: Diagnostic) {
938+
const { file, start } = diagnostic;
939+
const lineStarts = getLineStarts(file);
940+
let { line } = computeLineAndCharacterOfPosition(lineStarts, start);
941+
while (line > 0) {
942+
const previousLineText = file.text.slice(lineStarts[line - 1], lineStarts[line]);
943+
const result = suppressDiagnosticCommentRegEx.exec(previousLineText);
944+
if (!result) {
945+
// non-empty line
946+
return true;
947+
}
948+
if (result[3]) {
949+
// @ts-suppress
950+
return false;
951+
}
952+
line--;
953+
}
954+
return true;
955+
}
956+
930957
function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
931958
return runWithCancellationToken(() => {
932959
const diagnostics: Diagnostic[] = [];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/a.js ===
2+
3+
var x = 0;
4+
>x : Symbol(x, Decl(a.js, 1, 3))
5+
6+
7+
/// @ts-suppress
8+
x();
9+
>x : Symbol(x, Decl(a.js, 1, 3))
10+
11+
/// @ts-suppress
12+
x();
13+
>x : Symbol(x, Decl(a.js, 1, 3))
14+
15+
/// @ts-suppress
16+
x(
17+
>x : Symbol(x, Decl(a.js, 1, 3))
18+
19+
2,
20+
3);
21+
22+
23+
24+
// @ts-suppress
25+
// come comment
26+
// some other comment
27+
28+
// @anohter
29+
30+
x();
31+
>x : Symbol(x, Decl(a.js, 1, 3))
32+
33+
34+
35+
// @ts-suppress: no call signature
36+
x();
37+
>x : Symbol(x, Decl(a.js, 1, 3))
38+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/a.js ===
2+
3+
var x = 0;
4+
>x : number
5+
>0 : 0
6+
7+
8+
/// @ts-suppress
9+
x();
10+
>x() : any
11+
>x : number
12+
13+
/// @ts-suppress
14+
x();
15+
>x() : any
16+
>x : number
17+
18+
/// @ts-suppress
19+
x(
20+
>x( 2, 3) : any
21+
>x : number
22+
23+
2,
24+
>2 : 2
25+
26+
3);
27+
>3 : 3
28+
29+
30+
31+
// @ts-suppress
32+
// come comment
33+
// some other comment
34+
35+
// @anohter
36+
37+
x();
38+
>x() : any
39+
>x : number
40+
41+
42+
43+
// @ts-suppress: no call signature
44+
x();
45+
>x() : any
46+
>x : number
47+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @fileName: a.js
6+
var x = 0;
7+
8+
9+
/// @ts-suppress
10+
x();
11+
12+
/// @ts-suppress
13+
x();
14+
15+
/// @ts-suppress
16+
x(
17+
2,
18+
3);
19+
20+
21+
22+
// @ts-suppress
23+
// come comment
24+
// some other comment
25+
26+
// @anohter
27+
28+
x();
29+
30+
31+
32+
// @ts-suppress: no call signature
33+
x();

0 commit comments

Comments
 (0)