Skip to content

fix #15666: mark file as optional in Diagnostic #15714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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}`);
});

Expand All @@ -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";
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/APISample_watcher.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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";
Expand Down
10 changes: 7 additions & 3 deletions tests/cases/compiler/APISample_compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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}`);
});

Expand All @@ -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
});
});
6 changes: 3 additions & 3 deletions tests/cases/compiler/APISample_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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 });
watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });