diff --git a/src/compiler/types.ts b/src/compiler/types.ts index bbbce158caa0d..2ca9134fb6cdc 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3355,9 +3355,9 @@ namespace ts { } export interface Diagnostic { - file: SourceFile; - start: number; - length: number; + file: SourceFile | undefined; + start: number | undefined; + length: number | undefined; messageText: string | DiagnosticMessageChain; category: DiagnosticCategory; code: number; diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index c2617b2a6c6a3..35d4935045777 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -1,6 +1,6 @@ //// [APISample_compile.ts] /* - * Note: This test is a public API sample. The sample sources can be found + * Note: This test is a public API sample. The sample sources can be found at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler * Please log a "breaking change" issue for any API breaking change affecting this issue */ @@ -18,8 +18,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { - var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + if (!diagnostic.file) { + console.log(message); + return; + } + var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); }); @@ -31,7 +35,8 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void compile(process.argv.slice(2), { noEmitOnError: true, noImplicitAny: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS -}); +}); + //// [APISample_compile.js] "use strict"; @@ -47,8 +52,12 @@ function compile(fileNames, options) { var emitResult = program.emit(); var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(function (diagnostic) { - var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + if (!diagnostic.file) { + console.log(message); + return; + } + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; console.log(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); }); var exitCode = emitResult.emitSkipped ? 1 : 0; diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index f06f9ced73fde..b813a44f53e3c 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -1,6 +1,6 @@ //// [APISample_watcher.ts] /* - * Note: This test is a public API sample. The sample sources can be found + * Note: This test is a public API sample. The sample sources can be found at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services * Please log a "breaking change" issue for any API breaking change affecting this issue */ @@ -91,7 +91,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) { allDiagnostics.forEach(diagnostic => { let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); if (diagnostic.file) { - let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); } else { @@ -106,7 +106,8 @@ const currentDirectoryFiles = fs.readdirSync(process.cwd()). filter(fileName=> fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"); // Start the watcher -watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS }); +watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS }); + //// [APISample_watcher.js] "use strict"; diff --git a/tests/cases/compiler/APISample_compile.ts b/tests/cases/compiler/APISample_compile.ts index b60c53685aae0..c49d2a7b122f7 100644 --- a/tests/cases/compiler/APISample_compile.ts +++ b/tests/cases/compiler/APISample_compile.ts @@ -4,7 +4,7 @@ // @strictNullChecks:true /* - * Note: This test is a public API sample. The sample sources can be found + * Note: This test is a public API sample. The sample sources can be found at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler * Please log a "breaking change" issue for any API breaking change affecting this issue */ @@ -22,8 +22,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { - var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + if (!diagnostic.file) { + console.log(message); + return; + } + var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); }); @@ -35,4 +39,4 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void compile(process.argv.slice(2), { noEmitOnError: true, noImplicitAny: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS -}); \ No newline at end of file +}); diff --git a/tests/cases/compiler/APISample_watcher.ts b/tests/cases/compiler/APISample_watcher.ts index 07922bd35c779..3e8ce3d8d63e2 100644 --- a/tests/cases/compiler/APISample_watcher.ts +++ b/tests/cases/compiler/APISample_watcher.ts @@ -4,7 +4,7 @@ // @strictNullChecks:true /* - * Note: This test is a public API sample. The sample sources can be found + * Note: This test is a public API sample. The sample sources can be found at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services * Please log a "breaking change" issue for any API breaking change affecting this issue */ @@ -95,7 +95,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) { allDiagnostics.forEach(diagnostic => { let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); if (diagnostic.file) { - let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); } else { @@ -110,4 +110,4 @@ const currentDirectoryFiles = fs.readdirSync(process.cwd()). filter(fileName=> fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"); // Start the watcher -watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS }); \ No newline at end of file +watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });