1
1
//// [APISample_compile.ts]
2
2
/*
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
4
4
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
5
5
* Please log a "breaking change" issue for any API breaking change affecting this issue
6
6
*/
@@ -18,8 +18,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
18
18
var allDiagnostics = ts . getPreEmitDiagnostics ( program ) ;
19
19
20
20
allDiagnostics . forEach ( diagnostic => {
21
- var { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
22
21
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 ! ) ;
23
27
console . log ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
24
28
} ) ;
25
29
@@ -31,7 +35,8 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
31
35
compile ( process . argv . slice ( 2 ) , {
32
36
noEmitOnError : true , noImplicitAny : true ,
33
37
target : ts . ScriptTarget . ES5 , module : ts . ModuleKind . CommonJS
34
- } ) ;
38
+ } ) ;
39
+
35
40
36
41
//// [APISample_compile.js]
37
42
"use strict" ;
@@ -47,8 +52,12 @@ function compile(fileNames, options) {
47
52
var emitResult = program . emit ( ) ;
48
53
var allDiagnostics = ts . getPreEmitDiagnostics ( program ) ;
49
54
allDiagnostics . forEach ( function ( diagnostic ) {
50
- var _a = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) , line = _a . line , character = _a . character ;
51
55
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 ;
52
61
console . log ( diagnostic . file . fileName + " (" + ( line + 1 ) + "," + ( character + 1 ) + "): " + message ) ;
53
62
} ) ;
54
63
var exitCode = emitResult . emitSkipped ? 1 : 0 ;
0 commit comments