@@ -596,16 +596,37 @@ namespace ts {
596
596
}
597
597
598
598
/*@internal */
599
- export function getReferencedFileLocation ( getSourceFileByPath : ( path : Path ) => SourceFile | undefined , ref : ReferencedFile ) {
600
- const file = Debug . assertDefined ( getSourceFileByPath ( ref . file ) ) ;
599
+ export interface ReferenceFileLocation {
600
+ file : SourceFile ;
601
+ pos : number ;
602
+ end : number ;
603
+ packageId : PackageId | undefined ;
604
+ }
605
+
606
+ /*@internal */
607
+ export interface SyntheticReferenceFileLocation {
608
+ file : SourceFile ;
609
+ packageId : PackageId | undefined ;
610
+ text : string ;
611
+ }
612
+
613
+ /*@internal */
614
+ export function isReferenceFileLocation ( location : ReferenceFileLocation | SyntheticReferenceFileLocation ) : location is ReferenceFileLocation {
615
+ return ( location as ReferenceFileLocation ) . pos !== undefined ;
616
+ }
617
+
618
+ /*@internal */
619
+ export function getReferencedFileLocation ( getSourceFileByPath : ( path : Path ) => SourceFile | undefined , ref : ReferencedFile ) : ReferenceFileLocation | SyntheticReferenceFileLocation {
620
+ const file = Debug . checkDefined ( getSourceFileByPath ( ref . file ) ) ;
601
621
const { kind, index } = ref ;
602
- let pos : number , end : number , packageId : PackageId | undefined ;
622
+ let pos : number | undefined , end : number | undefined , packageId : PackageId | undefined ;
603
623
switch ( kind ) {
604
624
case FileIncludeKind . Import :
605
625
const importLiteral = getModuleNameStringLiteralAt ( file , index ) ;
626
+ packageId = file . resolvedModules ?. get ( importLiteral . text ) ?. packageId ;
627
+ if ( importLiteral . pos === - 1 ) return { file, packageId, text : importLiteral . text } ;
606
628
pos = skipTrivia ( file . text , importLiteral . pos ) ;
607
629
end = importLiteral . end ;
608
- packageId = file . resolvedModules ?. get ( importLiteral . text ) ?. packageId ;
609
630
break ;
610
631
case FileIncludeKind . ReferenceFile :
611
632
( { pos, end } = file . referencedFiles [ index ] ) ;
@@ -1077,8 +1098,8 @@ namespace ts {
1077
1098
case FilePreprocessingDiagnosticsKind . FilePreprocessingFileExplainingDiagnostic :
1078
1099
return programDiagnostics . add ( createDiagnosticExplainingFile ( diagnostic . file && getSourceFileByPath ( diagnostic . file ) , diagnostic . fileProcessingReason , diagnostic . diagnostic , diagnostic . args || emptyArray ) ) ;
1079
1100
case FilePreprocessingDiagnosticsKind . FilePreprocessingReferencedDiagnostic :
1080
- const { file, pos, end } = getReferencedFileLocation ( getSourceFileByPath , diagnostic . reason ) ;
1081
- return programDiagnostics . add ( createFileDiagnostic ( file , pos , end - pos , diagnostic . diagnostic , ...diagnostic . args || emptyArray ) ) ;
1101
+ const { file, pos, end } = getReferencedFileLocation ( getSourceFileByPath , diagnostic . reason ) as ReferenceFileLocation ;
1102
+ return programDiagnostics . add ( createFileDiagnostic ( file , Debug . checkDefined ( pos ) , Debug . checkDefined ( end ) - pos , diagnostic . diagnostic , ...diagnostic . args || emptyArray ) ) ;
1082
1103
default :
1083
1104
Debug . assertNever ( diagnostic ) ;
1084
1105
}
@@ -3321,7 +3342,7 @@ namespace ts {
3321
3342
const fileIncludeReasonDetails = fileIncludeReasons && chainDiagnosticMessages ( fileIncludeReasons , Diagnostics . The_file_is_in_the_program_because_Colon ) ;
3322
3343
const redirectInfo = file && explainIfFileIsRedirect ( file ) ;
3323
3344
const chain = chainDiagnosticMessages ( redirectInfo ? fileIncludeReasonDetails ? [ fileIncludeReasonDetails , ...redirectInfo ] : redirectInfo : fileIncludeReasonDetails , diagnostic , ...args || emptyArray ) ;
3324
- return location ?
3345
+ return location && isReferenceFileLocation ( location ) ?
3325
3346
createFileDiagnosticFromMessageChain ( location . file , location . pos , location . end - location . pos , chain , relatedInfo ) :
3326
3347
createCompilerDiagnosticFromMessageChain ( chain , relatedInfo ) ;
3327
3348
@@ -3355,7 +3376,7 @@ namespace ts {
3355
3376
3356
3377
function fileIncludeReasonToRelatedInformation ( reason : FileIncludeReason ) : DiagnosticWithLocation | undefined {
3357
3378
if ( isReferencedFile ( reason ) ) {
3358
- const { file : referencedFromFile , pos , end } = getReferencedFileLocation ( getSourceFileByPath , reason ) ;
3379
+ const referenceLocation = getReferencedFileLocation ( getSourceFileByPath , reason ) ;
3359
3380
let message : DiagnosticMessage ;
3360
3381
switch ( reason . kind ) {
3361
3382
case FileIncludeKind . Import :
@@ -3373,12 +3394,12 @@ namespace ts {
3373
3394
default :
3374
3395
Debug . assertNever ( reason ) ;
3375
3396
}
3376
- return createFileDiagnostic (
3377
- referencedFromFile ,
3378
- pos ,
3379
- end - pos ,
3397
+ return isReferenceFileLocation ( referenceLocation ) ? createFileDiagnostic (
3398
+ referenceLocation . file ,
3399
+ referenceLocation . pos ,
3400
+ referenceLocation . end - referenceLocation . pos ,
3380
3401
message ,
3381
- ) ;
3402
+ ) : undefined ;
3382
3403
}
3383
3404
3384
3405
if ( ! options . configFile ) return undefined ;
0 commit comments