Skip to content

Commit 8321b81

Browse files
authored
Merge pull request microsoft#15714 from HerringtonDarkholme/lib
fix microsoft#15666: mark file as optional in Diagnostic
2 parents 76cc39e + 1bbc94f commit 8321b81

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

src/compiler/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3357,9 +3357,9 @@ namespace ts {
33573357
}
33583358

33593359
export interface Diagnostic {
3360-
file: SourceFile;
3361-
start: number;
3362-
length: number;
3360+
file: SourceFile | undefined;
3361+
start: number | undefined;
3362+
length: number | undefined;
33633363
messageText: string | DiagnosticMessageChain;
33643364
category: DiagnosticCategory;
33653365
code: number;

tests/baselines/reference/APISample_compile.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [APISample_compile.ts]
22
/*
3-
* Note: This test is a public API sample. The sample sources can be found
3+
* Note: This test is a public API sample. The sample sources can be found
44
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
55
* Please log a "breaking change" issue for any API breaking change affecting this issue
66
*/
@@ -18,8 +18,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
1818
var allDiagnostics = ts.getPreEmitDiagnostics(program);
1919

2020
allDiagnostics.forEach(diagnostic => {
21-
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
2221
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
22+
if (!diagnostic.file) {
23+
console.log(message);
24+
return;
25+
}
26+
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
2327
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
2428
});
2529

@@ -31,7 +35,8 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
3135
compile(process.argv.slice(2), {
3236
noEmitOnError: true, noImplicitAny: true,
3337
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
34-
});
38+
});
39+
3540

3641
//// [APISample_compile.js]
3742
"use strict";
@@ -47,8 +52,12 @@ function compile(fileNames, options) {
4752
var emitResult = program.emit();
4853
var allDiagnostics = ts.getPreEmitDiagnostics(program);
4954
allDiagnostics.forEach(function (diagnostic) {
50-
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
5155
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
56+
if (!diagnostic.file) {
57+
console.log(message);
58+
return;
59+
}
60+
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
5261
console.log(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message);
5362
});
5463
var exitCode = emitResult.emitSkipped ? 1 : 0;

tests/baselines/reference/APISample_watcher.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [APISample_watcher.ts]
22
/*
3-
* Note: This test is a public API sample. The sample sources can be found
3+
* Note: This test is a public API sample. The sample sources can be found
44
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
55
* Please log a "breaking change" issue for any API breaking change affecting this issue
66
*/
@@ -91,7 +91,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
9191
allDiagnostics.forEach(diagnostic => {
9292
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
9393
if (diagnostic.file) {
94-
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
94+
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
9595
console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
9696
}
9797
else {
@@ -106,7 +106,8 @@ const currentDirectoryFiles = fs.readdirSync(process.cwd()).
106106
filter(fileName=> fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts");
107107

108108
// Start the watcher
109-
watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
109+
watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
110+
110111

111112
//// [APISample_watcher.js]
112113
"use strict";

tests/cases/compiler/APISample_compile.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @strictNullChecks:true
55

66
/*
7-
* Note: This test is a public API sample. The sample sources can be found
7+
* Note: This test is a public API sample. The sample sources can be found
88
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
99
* Please log a "breaking change" issue for any API breaking change affecting this issue
1010
*/
@@ -22,8 +22,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
2222
var allDiagnostics = ts.getPreEmitDiagnostics(program);
2323

2424
allDiagnostics.forEach(diagnostic => {
25-
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
2625
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
26+
if (!diagnostic.file) {
27+
console.log(message);
28+
return;
29+
}
30+
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
2731
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
2832
});
2933

@@ -35,4 +39,4 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
3539
compile(process.argv.slice(2), {
3640
noEmitOnError: true, noImplicitAny: true,
3741
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
38-
});
42+
});

tests/cases/compiler/APISample_watcher.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @strictNullChecks:true
55

66
/*
7-
* Note: This test is a public API sample. The sample sources can be found
7+
* Note: This test is a public API sample. The sample sources can be found
88
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
99
* Please log a "breaking change" issue for any API breaking change affecting this issue
1010
*/
@@ -95,7 +95,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
9595
allDiagnostics.forEach(diagnostic => {
9696
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
9797
if (diagnostic.file) {
98-
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
98+
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
9999
console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
100100
}
101101
else {
@@ -110,4 +110,4 @@ const currentDirectoryFiles = fs.readdirSync(process.cwd()).
110110
filter(fileName=> fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts");
111111

112112
// Start the watcher
113-
watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
113+
watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });

0 commit comments

Comments
 (0)