mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-26 03:19:10 -06:00
Disable check diagnostics per line
This commit is contained in:
parent
3d03f8d8a5
commit
fe7719f0a9
@ -4,6 +4,7 @@
|
||||
|
||||
namespace ts {
|
||||
const emptyArray: any[] = [];
|
||||
const suppressDiagnosticCommentRegEx = /(^\s*$)|(^\s*\/\/\/?\s*(@ts-suppress)?)/;
|
||||
|
||||
export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName = "tsconfig.json"): string {
|
||||
while (true) {
|
||||
@ -923,10 +924,36 @@ namespace ts {
|
||||
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
|
||||
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
|
||||
|
||||
return bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
|
||||
const diagnostics = bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
|
||||
return isSourceFileJavaScript(sourceFile)
|
||||
? filter(diagnostics, shouldReportDiagnostic)
|
||||
: diagnostics;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip errors if previous line start with '// @ts-suppress' comment, not counting non-empty non-comment lines
|
||||
*/
|
||||
function shouldReportDiagnostic(diagnostic: Diagnostic) {
|
||||
const { file, start } = diagnostic;
|
||||
const lineStarts = getLineStarts(file);
|
||||
let { line } = computeLineAndCharacterOfPosition(lineStarts, start);
|
||||
while (line > 0) {
|
||||
const previousLineText = file.text.slice(lineStarts[line - 1], lineStarts[line]);
|
||||
const result = suppressDiagnosticCommentRegEx.exec(previousLineText);
|
||||
if (!result) {
|
||||
// non-empty line
|
||||
return true;
|
||||
}
|
||||
if (result[3]) {
|
||||
// @ts-suppress
|
||||
return false;
|
||||
}
|
||||
line--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
|
||||
return runWithCancellationToken(() => {
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
|
||||
var x = 0;
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
/// @ts-suppress
|
||||
x(
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
2,
|
||||
3);
|
||||
|
||||
|
||||
|
||||
// @ts-suppress
|
||||
// come comment
|
||||
// some other comment
|
||||
|
||||
// @anohter
|
||||
|
||||
x();
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
|
||||
|
||||
// @ts-suppress: no call signature
|
||||
x();
|
||||
>x : Symbol(x, Decl(a.js, 1, 3))
|
||||
|
||||
47
tests/baselines/reference/checkJsFiles_skipDiagnostics.types
Normal file
47
tests/baselines/reference/checkJsFiles_skipDiagnostics.types
Normal file
@ -0,0 +1,47 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
|
||||
var x = 0;
|
||||
>x : number
|
||||
>0 : 0
|
||||
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
>x() : any
|
||||
>x : number
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
>x() : any
|
||||
>x : number
|
||||
|
||||
/// @ts-suppress
|
||||
x(
|
||||
>x( 2, 3) : any
|
||||
>x : number
|
||||
|
||||
2,
|
||||
>2 : 2
|
||||
|
||||
3);
|
||||
>3 : 3
|
||||
|
||||
|
||||
|
||||
// @ts-suppress
|
||||
// come comment
|
||||
// some other comment
|
||||
|
||||
// @anohter
|
||||
|
||||
x();
|
||||
>x() : any
|
||||
>x : number
|
||||
|
||||
|
||||
|
||||
// @ts-suppress: no call signature
|
||||
x();
|
||||
>x() : any
|
||||
>x : number
|
||||
|
||||
33
tests/cases/compiler/checkJsFiles_skipDiagnostics.ts
Normal file
33
tests/cases/compiler/checkJsFiles_skipDiagnostics.ts
Normal file
@ -0,0 +1,33 @@
|
||||
// @allowJs: true
|
||||
// @checkJs: true
|
||||
// @noEmit: true
|
||||
|
||||
// @fileName: a.js
|
||||
var x = 0;
|
||||
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
|
||||
/// @ts-suppress
|
||||
x();
|
||||
|
||||
/// @ts-suppress
|
||||
x(
|
||||
2,
|
||||
3);
|
||||
|
||||
|
||||
|
||||
// @ts-suppress
|
||||
// come comment
|
||||
// some other comment
|
||||
|
||||
// @anohter
|
||||
|
||||
x();
|
||||
|
||||
|
||||
|
||||
// @ts-suppress: no call signature
|
||||
x();
|
||||
Loading…
x
Reference in New Issue
Block a user