From fe7719f0a9747214046eb1dc8bbf5047b2fc90d3 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 7 Mar 2017 23:03:47 -0800 Subject: [PATCH] Disable check diagnostics per line --- src/compiler/program.ts | 29 +++++++++++- .../checkJsFiles_skipDiagnostics.symbols | 38 +++++++++++++++ .../checkJsFiles_skipDiagnostics.types | 47 +++++++++++++++++++ .../compiler/checkJsFiles_skipDiagnostics.ts | 33 +++++++++++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/checkJsFiles_skipDiagnostics.symbols create mode 100644 tests/baselines/reference/checkJsFiles_skipDiagnostics.types create mode 100644 tests/cases/compiler/checkJsFiles_skipDiagnostics.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 67e9f62fe2d..bdc8f76cf60 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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[] = []; diff --git a/tests/baselines/reference/checkJsFiles_skipDiagnostics.symbols b/tests/baselines/reference/checkJsFiles_skipDiagnostics.symbols new file mode 100644 index 00000000000..a0f2200048f --- /dev/null +++ b/tests/baselines/reference/checkJsFiles_skipDiagnostics.symbols @@ -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)) + diff --git a/tests/baselines/reference/checkJsFiles_skipDiagnostics.types b/tests/baselines/reference/checkJsFiles_skipDiagnostics.types new file mode 100644 index 00000000000..69105a3112a --- /dev/null +++ b/tests/baselines/reference/checkJsFiles_skipDiagnostics.types @@ -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 + diff --git a/tests/cases/compiler/checkJsFiles_skipDiagnostics.ts b/tests/cases/compiler/checkJsFiles_skipDiagnostics.ts new file mode 100644 index 00000000000..bcbcc815e7c --- /dev/null +++ b/tests/cases/compiler/checkJsFiles_skipDiagnostics.ts @@ -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(); \ No newline at end of file