From 2dd6627022c3d54b73d0d415971a451b14e2d8a1 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 30 May 2017 13:32:46 -0700 Subject: [PATCH 1/2] Report jsdoc syntax errors when checkJs is on --- src/compiler/parser.ts | 6 ++++++ src/compiler/program.ts | 3 +++ src/compiler/types.ts | 13 ++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2d0a4b66a1a..51c9375c291 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6322,6 +6322,12 @@ namespace ts { comment.parent = parent; } + if (isInJavaScriptFile(parent)) { + if (!sourceFile.jsDocDiagnostics) { + sourceFile.jsDocDiagnostics = []; + } + sourceFile.jsDocDiagnostics.push(...parseDiagnostics); + } currentToken = saveToken; parseDiagnostics.length = saveParseDiagnosticsLength; parseErrorBeforeNextFinishedNode = saveParseErrorBeforeNextFinishedNode; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index dd293727b4a..e69a89cc618 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1021,6 +1021,9 @@ namespace ts { if (isSourceFileJavaScript(sourceFile)) { if (!sourceFile.additionalSyntacticDiagnostics) { sourceFile.additionalSyntacticDiagnostics = getJavaScriptSyntacticDiagnosticsForFile(sourceFile); + if (isCheckJsEnabledForFile(sourceFile, options)) { + sourceFile.additionalSyntacticDiagnostics = concatenate(sourceFile.additionalSyntacticDiagnostics, sourceFile.jsDocDiagnostics); + } } return concatenate(sourceFile.additionalSyntacticDiagnostics, sourceFile.parseDiagnostics); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 6f602ff23af..aaa790b6d13 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2312,16 +2312,19 @@ namespace ts { /* @internal */ identifierCount: number; /* @internal */ symbolCount: number; - // File level diagnostics reported by the parser (includes diagnostics about /// references + // File-level diagnostics reported by the parser (includes diagnostics about /// references // as well as code diagnostics). /* @internal */ parseDiagnostics: Diagnostic[]; - // Stores additional file level diagnostics reported by the program - /* @internal */ additionalSyntacticDiagnostics?: Diagnostic[]; - - // File level diagnostics reported by the binder. + // File-level diagnostics reported by the binder. /* @internal */ bindDiagnostics: Diagnostic[]; + // File-level JSDoc diagnostics reported by the JSDoc parser + /* @internal */ jsDocDiagnostics?: Diagnostic[]; + + // Stores additional file-level diagnostics reported by the program + /* @internal */ additionalSyntacticDiagnostics?: Diagnostic[]; + // Stores a line map for the file. // This field should never be used directly to obtain line map, use getLineMap function instead. /* @internal */ lineMap: number[]; From 41e134529a6797904b0f8d39685cfdd2f4aea7e0 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 30 May 2017 13:33:13 -0700 Subject: [PATCH 2/2] Test reporting jsdoc syntax errors --- .../reference/syntaxErrors.errors.txt | 31 +++++++++++++++++++ tests/cases/conformance/jsdoc/syntaxErrors.ts | 20 ++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/baselines/reference/syntaxErrors.errors.txt create mode 100644 tests/cases/conformance/jsdoc/syntaxErrors.ts diff --git a/tests/baselines/reference/syntaxErrors.errors.txt b/tests/baselines/reference/syntaxErrors.errors.txt new file mode 100644 index 00000000000..8e3005891a5 --- /dev/null +++ b/tests/baselines/reference/syntaxErrors.errors.txt @@ -0,0 +1,31 @@ +tests/cases/conformance/jsdoc/foo.js(2,15): error TS1005: '}' expected. +tests/cases/conformance/jsdoc/foo.js(3,19): error TS1005: '}' expected. +tests/cases/conformance/jsdoc/foo.js(4,18): error TS1003: Identifier expected. +tests/cases/conformance/jsdoc/foo.js(4,19): error TS1005: '}' expected. + + +==== tests/cases/conformance/jsdoc/foo.js (4 errors) ==== + /** + * @param {(x)=>void} x + ~~ +!!! error TS1005: '}' expected. + * @param {typeof String} y + ~~~~~~ +!!! error TS1005: '}' expected. + * @param {string & number} z + +!!! error TS1003: Identifier expected. + ~ +!!! error TS1005: '}' expected. + **/ + function foo(x, y, z) { } + +==== tests/cases/conformance/jsdoc/skipped.js (0 errors) ==== + // @ts-nocheck + /** + * @param {(x)=>void} x + * @param {typeof String} y + * @param {string & number} z + **/ + function bar(x, y, z) { } + \ No newline at end of file diff --git a/tests/cases/conformance/jsdoc/syntaxErrors.ts b/tests/cases/conformance/jsdoc/syntaxErrors.ts new file mode 100644 index 00000000000..4f9024810dd --- /dev/null +++ b/tests/cases/conformance/jsdoc/syntaxErrors.ts @@ -0,0 +1,20 @@ +// @checkJs: true +// @allowJs: true +// @noEmit: true + +// @Filename: foo.js +/** + * @param {(x)=>void} x + * @param {typeof String} y + * @param {string & number} z + **/ +function foo(x, y, z) { } + +// @Filename: skipped.js +// @ts-nocheck +/** + * @param {(x)=>void} x + * @param {typeof String} y + * @param {string & number} z + **/ +function bar(x, y, z) { }