Disable check diagnostics per line

This commit is contained in:
Mohamed Hegazy 2017-03-07 23:03:47 -08:00
parent 3d03f8d8a5
commit fe7719f0a9
4 changed files with 146 additions and 1 deletions

View File

@ -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[] = [];

View File

@ -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))

View 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

View 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();