6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { isSyntaxError , Position } from '@angular/compiler' ;
9
+ import { isSyntaxError } from '@angular/compiler' ;
10
10
import ts from 'typescript' ;
11
11
12
12
import { absoluteFrom , AbsoluteFsPath , FileSystem , getFileSystem , ReadonlyFileSystem , relative , resolve } from '../src/ngtsc/file_system' ;
13
- import { NgCompilerOptions } from './ngtsc/core/api' ;
14
13
14
+ import { NgCompilerOptions } from './ngtsc/core/api' ;
15
15
import { replaceTsWithNgInErrors } from './ngtsc/diagnostics' ;
16
16
import * as api from './transformers/api' ;
17
17
import * as ng from './transformers/entry_points' ;
18
18
import { createMessageDiagnostic } from './transformers/util' ;
19
19
20
- export type Diagnostics = ReadonlyArray < ts . Diagnostic | api . Diagnostic > ;
21
-
22
20
const defaultFormatHost : ts . FormatDiagnosticsHost = {
23
21
getCurrentDirectory : ( ) => ts . sys . getCurrentDirectory ( ) ,
24
22
getCanonicalFileName : fileName => fileName ,
25
23
getNewLine : ( ) => ts . sys . newLine
26
24
} ;
27
25
28
- function displayFileName ( fileName : string , host : ts . FormatDiagnosticsHost ) : string {
29
- return relative (
30
- resolve ( host . getCurrentDirectory ( ) ) , resolve ( host . getCanonicalFileName ( fileName ) ) ) ;
31
- }
32
-
33
- export function formatDiagnosticPosition (
34
- position : Position , host : ts . FormatDiagnosticsHost = defaultFormatHost ) : string {
35
- return `${ displayFileName ( position . fileName , host ) } (${ position . line + 1 } ,${ position . column + 1 } )` ;
36
- }
37
-
38
- export function flattenDiagnosticMessageChain (
39
- chain : api . DiagnosticMessageChain , host : ts . FormatDiagnosticsHost = defaultFormatHost ,
40
- indent = 0 ) : string {
41
- const newLine = host . getNewLine ( ) ;
42
- let result = '' ;
43
- if ( indent ) {
44
- result += newLine ;
45
-
46
- for ( let i = 0 ; i < indent ; i ++ ) {
47
- result += ' ' ;
48
- }
49
- }
50
- result += chain . messageText ;
51
-
52
- const position = chain . position ;
53
- // add position if available, and we are not at the depest frame
54
- if ( position && indent !== 0 ) {
55
- result += ` at ${ formatDiagnosticPosition ( position , host ) } ` ;
56
- }
57
-
58
- indent ++ ;
59
- if ( chain . next ) {
60
- for ( const kid of chain . next ) {
61
- result += flattenDiagnosticMessageChain ( kid , host , indent ) ;
62
- }
63
- }
64
- return result ;
65
- }
66
-
67
- export function formatDiagnostic (
68
- diagnostic : api . Diagnostic , host : ts . FormatDiagnosticsHost = defaultFormatHost ) {
69
- let result = '' ;
70
- const newLine = host . getNewLine ( ) ;
71
- const span = diagnostic . span ;
72
- if ( span ) {
73
- result += `${
74
- formatDiagnosticPosition (
75
- { fileName : span . start . file . url , line : span . start . line , column : span . start . col } ,
76
- host ) } : `;
77
- } else if ( diagnostic . position ) {
78
- result += `${ formatDiagnosticPosition ( diagnostic . position , host ) } : ` ;
79
- }
80
- if ( diagnostic . span && diagnostic . span . details ) {
81
- result += `${ diagnostic . span . details } , ${ diagnostic . messageText } ${ newLine } ` ;
82
- } else if ( diagnostic . chain ) {
83
- result += `${ flattenDiagnosticMessageChain ( diagnostic . chain , host ) } .${ newLine } ` ;
84
- } else {
85
- result += `${ diagnostic . messageText } ${ newLine } ` ;
86
- }
87
- return result ;
88
- }
89
-
90
26
export function formatDiagnostics (
91
- diags : Diagnostics , host : ts . FormatDiagnosticsHost = defaultFormatHost ) : string {
27
+ diags : ReadonlyArray < ts . Diagnostic > ,
28
+ host : ts . FormatDiagnosticsHost = defaultFormatHost ) : string {
92
29
if ( diags && diags . length ) {
93
30
return diags
94
- . map ( diagnostic => {
95
- if ( api . isTsDiagnostic ( diagnostic ) ) {
96
- return replaceTsWithNgInErrors (
97
- ts . formatDiagnosticsWithColorAndContext ( [ diagnostic ] , host ) ) ;
98
- } else {
99
- return formatDiagnostic ( diagnostic , host ) ;
100
- }
101
- } )
31
+ . map (
32
+ diagnostic => replaceTsWithNgInErrors (
33
+ ts . formatDiagnosticsWithColorAndContext ( [ diagnostic ] , host ) ) )
102
34
. join ( '' ) ;
103
35
} else {
104
36
return '' ;
@@ -264,12 +196,12 @@ function getExtendedConfigPathWorker(
264
196
}
265
197
266
198
export interface PerformCompilationResult {
267
- diagnostics : Diagnostics ;
199
+ diagnostics : ReadonlyArray < ts . Diagnostic > ;
268
200
program ?: api . Program ;
269
201
emitResult ?: ts . EmitResult ;
270
202
}
271
203
272
- export function exitCodeFromResult ( diags : Diagnostics | undefined ) : number {
204
+ export function exitCodeFromResult ( diags : ReadonlyArray < ts . Diagnostic > | undefined ) : number {
273
205
if ( ! diags ) return 0 ;
274
206
if ( diags . every ( ( diag ) => diag . category !== ts . DiagnosticCategory . Error ) ) {
275
207
// If we have a result and didn't get any errors, we succeeded.
@@ -298,14 +230,14 @@ export function performCompilation({
298
230
oldProgram ?: api . Program ,
299
231
emitCallback ?: api . TsEmitCallback ,
300
232
mergeEmitResultsCallback ?: api . TsMergeEmitResultsCallback ,
301
- gatherDiagnostics ?: ( program : api . Program ) => Diagnostics ,
233
+ gatherDiagnostics ?: ( program : api . Program ) => ReadonlyArray < ts . Diagnostic > ,
302
234
customTransformers ?: api . CustomTransformers ,
303
235
emitFlags ?: api . EmitFlags ,
304
236
modifiedResourceFiles ?: Set < string > | null ,
305
237
} ) : PerformCompilationResult {
306
238
let program : api . Program | undefined ;
307
239
let emitResult : ts . EmitResult | undefined ;
308
- let allDiagnostics : Array < ts . Diagnostic | api . Diagnostic > = [ ] ;
240
+ let allDiagnostics : Array < ts . Diagnostic > = [ ] ;
309
241
try {
310
242
if ( ! host ) {
311
243
host = ng . createCompilerHost ( { options} ) ;
@@ -344,15 +276,21 @@ export function performCompilation({
344
276
program = undefined ;
345
277
code = api . UNKNOWN_ERROR_CODE ;
346
278
}
347
- allDiagnostics . push (
348
- { category : ts . DiagnosticCategory . Error , messageText : errMsg , code, source : api . SOURCE } ) ;
279
+ allDiagnostics . push ( {
280
+ category : ts . DiagnosticCategory . Error ,
281
+ messageText : errMsg ,
282
+ code,
283
+ file : undefined ,
284
+ start : undefined ,
285
+ length : undefined ,
286
+ } ) ;
349
287
return { diagnostics : allDiagnostics , program} ;
350
288
}
351
289
}
352
- export function defaultGatherDiagnostics ( program : api . Program ) : Diagnostics {
353
- const allDiagnostics : Array < ts . Diagnostic | api . Diagnostic > = [ ] ;
290
+ export function defaultGatherDiagnostics ( program : api . Program ) : ReadonlyArray < ts . Diagnostic > {
291
+ const allDiagnostics : Array < ts . Diagnostic > = [ ] ;
354
292
355
- function checkDiagnostics ( diags : Diagnostics | undefined ) {
293
+ function checkDiagnostics ( diags : ReadonlyArray < ts . Diagnostic > | undefined ) {
356
294
if ( diags ) {
357
295
allDiagnostics . push ( ...diags ) ;
358
296
return ! hasErrors ( diags ) ;
@@ -367,7 +305,7 @@ export function defaultGatherDiagnostics(program: api.Program): Diagnostics {
367
305
368
306
// Check syntactic diagnostics
369
307
checkOtherDiagnostics =
370
- checkOtherDiagnostics && checkDiagnostics ( program . getTsSyntacticDiagnostics ( ) as Diagnostics ) ;
308
+ checkOtherDiagnostics && checkDiagnostics ( program . getTsSyntacticDiagnostics ( ) ) ;
371
309
372
310
// Check TypeScript semantic and Angular structure diagnostics
373
311
checkOtherDiagnostics =
@@ -377,11 +315,11 @@ export function defaultGatherDiagnostics(program: api.Program): Diagnostics {
377
315
378
316
// Check Angular semantic diagnostics
379
317
checkOtherDiagnostics =
380
- checkOtherDiagnostics && checkDiagnostics ( program . getNgSemanticDiagnostics ( ) as Diagnostics ) ;
318
+ checkOtherDiagnostics && checkDiagnostics ( program . getNgSemanticDiagnostics ( ) ) ;
381
319
382
320
return allDiagnostics ;
383
321
}
384
322
385
- function hasErrors ( diags : Diagnostics ) {
323
+ function hasErrors ( diags : ReadonlyArray < ts . Diagnostic > ) {
386
324
return diags . some ( d => d . category === ts . DiagnosticCategory . Error ) ;
387
325
}
0 commit comments