@@ -394,8 +394,8 @@ namespace ts {
394
394
return resolutions ;
395
395
}
396
396
397
- interface DiagnosticCache {
398
- perFile ?: Map < Diagnostic [ ] > ;
397
+ interface DiagnosticCache < T extends Diagnostic > {
398
+ perFile ?: Map < T [ ] > ;
399
399
allDiagnostics ?: Diagnostic [ ] ;
400
400
}
401
401
@@ -454,7 +454,7 @@ namespace ts {
454
454
455
455
export function getConfigFileParsingDiagnostics ( configFileParseResult : ParsedCommandLine ) : ReadonlyArray < Diagnostic > {
456
456
return configFileParseResult . options . configFile ?
457
- configFileParseResult . options . configFile . parseDiagnostics . concat ( configFileParseResult . errors ) :
457
+ [ ... configFileParseResult . options . configFile . parseDiagnostics , ... configFileParseResult . errors ] :
458
458
configFileParseResult . errors ;
459
459
}
460
460
@@ -517,8 +517,8 @@ namespace ts {
517
517
let classifiableNames : UnderscoreEscapedMap < true > ;
518
518
let modifiedFilePaths : Path [ ] | undefined ;
519
519
520
- const cachedSemanticDiagnosticsForFile : DiagnosticCache = { } ;
521
- const cachedDeclarationDiagnosticsForFile : DiagnosticCache = { } ;
520
+ const cachedSemanticDiagnosticsForFile : DiagnosticCache < Diagnostic > = { } ;
521
+ const cachedDeclarationDiagnosticsForFile : DiagnosticCache < DiagnosticWithLocation > = { } ;
522
522
523
523
let resolvedTypeReferenceDirectives = createMap < ResolvedTypeReferenceDirective > ( ) ;
524
524
let fileProcessingDiagnostics = createDiagnosticCollection ( ) ;
@@ -1313,10 +1313,10 @@ namespace ts {
1313
1313
return filesByName . get ( path ) ;
1314
1314
}
1315
1315
1316
- function getDiagnosticsHelper (
1316
+ function getDiagnosticsHelper < T extends Diagnostic > (
1317
1317
sourceFile : SourceFile ,
1318
- getDiagnostics : ( sourceFile : SourceFile , cancellationToken : CancellationToken ) => ReadonlyArray < Diagnostic > ,
1319
- cancellationToken : CancellationToken ) : ReadonlyArray < Diagnostic > {
1318
+ getDiagnostics : ( sourceFile : SourceFile , cancellationToken : CancellationToken ) => ReadonlyArray < T > ,
1319
+ cancellationToken : CancellationToken ) : ReadonlyArray < T > {
1320
1320
if ( sourceFile ) {
1321
1321
return getDiagnostics ( sourceFile , cancellationToken ) ;
1322
1322
}
@@ -1328,15 +1328,15 @@ namespace ts {
1328
1328
} ) ) ;
1329
1329
}
1330
1330
1331
- function getSyntacticDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < Diagnostic > {
1331
+ function getSyntacticDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < DiagnosticWithLocation > {
1332
1332
return getDiagnosticsHelper ( sourceFile , getSyntacticDiagnosticsForFile , cancellationToken ) ;
1333
1333
}
1334
1334
1335
1335
function getSemanticDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < Diagnostic > {
1336
1336
return getDiagnosticsHelper ( sourceFile , getSemanticDiagnosticsForFile , cancellationToken ) ;
1337
1337
}
1338
1338
1339
- function getDeclarationDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < Diagnostic > {
1339
+ function getDeclarationDiagnostics ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < DiagnosticWithLocation > {
1340
1340
const options = program . getCompilerOptions ( ) ;
1341
1341
// collect diagnostics from the program only once if either no source file was specified or out/outFile is set (bundled emit)
1342
1342
if ( ! sourceFile || options . out || options . outFile ) {
@@ -1347,7 +1347,7 @@ namespace ts {
1347
1347
}
1348
1348
}
1349
1349
1350
- function getSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : ReadonlyArray < Diagnostic > {
1350
+ function getSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : ReadonlyArray < DiagnosticWithLocation > {
1351
1351
// For JavaScript files, we report semantic errors for using TypeScript-only
1352
1352
// constructs from within a JavaScript file as syntactic errors.
1353
1353
if ( isSourceFileJavaScript ( sourceFile ) ) {
@@ -1382,7 +1382,7 @@ namespace ts {
1382
1382
}
1383
1383
}
1384
1384
1385
- function getSemanticDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
1385
+ function getSemanticDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < Diagnostic > {
1386
1386
return getAndCacheDiagnostics ( sourceFile , cancellationToken , cachedSemanticDiagnosticsForFile , getSemanticDiagnosticsForFileNoCache ) ;
1387
1387
}
1388
1388
@@ -1403,15 +1403,22 @@ namespace ts {
1403
1403
// By default, only type-check .ts, .tsx, 'Deferred' and 'External' files (external files are added by plugins)
1404
1404
const includeBindAndCheckDiagnostics = sourceFile . scriptKind === ScriptKind . TS || sourceFile . scriptKind === ScriptKind . TSX ||
1405
1405
sourceFile . scriptKind === ScriptKind . External || isCheckJs || sourceFile . scriptKind === ScriptKind . Deferred ;
1406
- const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile . bindDiagnostics : emptyArray ;
1406
+ const bindDiagnostics : ReadonlyArray < Diagnostic > = includeBindAndCheckDiagnostics ? sourceFile . bindDiagnostics : emptyArray ;
1407
1407
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker . getDiagnostics ( sourceFile , cancellationToken ) : emptyArray ;
1408
1408
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics . getDiagnostics ( sourceFile . fileName ) ;
1409
1409
const programDiagnosticsInFile = programDiagnostics . getDiagnostics ( sourceFile . fileName ) ;
1410
- let diagnostics = bindDiagnostics . concat ( checkDiagnostics , fileProcessingDiagnosticsInFile , programDiagnosticsInFile ) ;
1411
- if ( isCheckJs ) {
1412
- diagnostics = concatenate ( diagnostics , sourceFile . jsDocDiagnostics ) ;
1410
+
1411
+ let diagnostics : Diagnostic [ ] | undefined ;
1412
+ for ( const diags of [ bindDiagnostics , checkDiagnostics , fileProcessingDiagnosticsInFile , programDiagnosticsInFile , isCheckJs ? sourceFile . jsDocDiagnostics : undefined ] ) {
1413
+ if ( diags ) {
1414
+ for ( const diag of diags ) {
1415
+ if ( shouldReportDiagnostic ( diag ) ) {
1416
+ diagnostics = append ( diagnostics , diag ) ;
1417
+ }
1418
+ }
1419
+ }
1413
1420
}
1414
- return filter ( diagnostics , shouldReportDiagnostic ) ;
1421
+ return diagnostics ;
1415
1422
} ) ;
1416
1423
}
1417
1424
@@ -1440,9 +1447,9 @@ namespace ts {
1440
1447
return true ;
1441
1448
}
1442
1449
1443
- function getJavaScriptSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : Diagnostic [ ] {
1450
+ function getJavaScriptSyntacticDiagnosticsForFile ( sourceFile : SourceFile ) : DiagnosticWithLocation [ ] {
1444
1451
return runWithCancellationToken ( ( ) => {
1445
- const diagnostics : Diagnostic [ ] = [ ] ;
1452
+ const diagnostics : DiagnosticWithLocation [ ] = [ ] ;
1446
1453
let parent : Node = sourceFile ;
1447
1454
walk ( sourceFile ) ;
1448
1455
@@ -1610,20 +1617,20 @@ namespace ts {
1610
1617
}
1611
1618
}
1612
1619
1613
- function createDiagnosticForNodeArray ( nodes : NodeArray < Node > , message : DiagnosticMessage , arg0 ?: string | number , arg1 ?: string | number , arg2 ?: string | number ) : Diagnostic {
1620
+ function createDiagnosticForNodeArray ( nodes : NodeArray < Node > , message : DiagnosticMessage , arg0 ?: string | number , arg1 ?: string | number , arg2 ?: string | number ) : DiagnosticWithLocation {
1614
1621
const start = nodes . pos ;
1615
1622
return createFileDiagnostic ( sourceFile , start , nodes . end - start , message , arg0 , arg1 , arg2 ) ;
1616
1623
}
1617
1624
1618
1625
// Since these are syntactic diagnostics, parent might not have been set
1619
1626
// this means the sourceFile cannot be infered from the node
1620
- function createDiagnosticForNode ( node : Node , message : DiagnosticMessage , arg0 ?: string | number , arg1 ?: string | number , arg2 ?: string | number ) : Diagnostic {
1627
+ function createDiagnosticForNode ( node : Node , message : DiagnosticMessage , arg0 ?: string | number , arg1 ?: string | number , arg2 ?: string | number ) : DiagnosticWithLocation {
1621
1628
return createDiagnosticForNodeInSourceFile ( sourceFile , node , message , arg0 , arg1 , arg2 ) ;
1622
1629
}
1623
1630
} ) ;
1624
1631
}
1625
1632
1626
- function getDeclarationDiagnosticsWorker ( sourceFile : SourceFile | undefined , cancellationToken : CancellationToken ) : Diagnostic [ ] {
1633
+ function getDeclarationDiagnosticsWorker ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < DiagnosticWithLocation > {
1627
1634
return getAndCacheDiagnostics ( sourceFile , cancellationToken , cachedDeclarationDiagnosticsForFile , getDeclarationDiagnosticsForFileNoCache ) ;
1628
1635
}
1629
1636
@@ -1635,23 +1642,24 @@ namespace ts {
1635
1642
} ) ;
1636
1643
}
1637
1644
1638
- function getAndCacheDiagnostics (
1645
+ function getAndCacheDiagnostics < T extends Diagnostic > (
1639
1646
sourceFile : SourceFile | undefined ,
1640
1647
cancellationToken : CancellationToken ,
1641
- cache : DiagnosticCache ,
1642
- getDiagnostics : ( sourceFile : SourceFile , cancellationToken : CancellationToken ) => Diagnostic [ ] ) {
1648
+ cache : DiagnosticCache < T > ,
1649
+ getDiagnostics : ( sourceFile : SourceFile , cancellationToken : CancellationToken ) => T [ ] ,
1650
+ ) : ReadonlyArray < T > {
1643
1651
1644
1652
const cachedResult = sourceFile
1645
1653
? cache . perFile && cache . perFile . get ( sourceFile . path )
1646
- : cache . allDiagnostics ;
1654
+ : cache . allDiagnostics as T [ ] ;
1647
1655
1648
1656
if ( cachedResult ) {
1649
1657
return cachedResult ;
1650
1658
}
1651
1659
const result = getDiagnostics ( sourceFile , cancellationToken ) || emptyArray ;
1652
1660
if ( sourceFile ) {
1653
1661
if ( ! cache . perFile ) {
1654
- cache . perFile = createMap < Diagnostic [ ] > ( ) ;
1662
+ cache . perFile = createMap < T [ ] > ( ) ;
1655
1663
}
1656
1664
cache . perFile . set ( sourceFile . path , result ) ;
1657
1665
}
@@ -1661,7 +1669,7 @@ namespace ts {
1661
1669
return result ;
1662
1670
}
1663
1671
1664
- function getDeclarationDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : Diagnostic [ ] {
1672
+ function getDeclarationDiagnosticsForFile ( sourceFile : SourceFile , cancellationToken : CancellationToken ) : ReadonlyArray < DiagnosticWithLocation > {
1665
1673
return sourceFile . isDeclarationFile ? [ ] : getDeclarationDiagnosticsWorker ( sourceFile , cancellationToken ) ;
1666
1674
}
1667
1675
0 commit comments