@@ -266,14 +266,24 @@ namespace ts {
266
266
const sourceMapCommentRegExp = / ^ \/ \/ [ @ # ] s o u r c e [ M ] a p p i n g U R L = ( .+ ) \s * $ / ;
267
267
const whitespaceOrMapCommentRegExp = / ^ \s * ( \/ \/ [ @ # ] .* ) ? $ / ;
268
268
269
+ export interface LineInfo {
270
+ getLineCount ( ) : number ;
271
+ getLineText ( line : number ) : string ;
272
+ }
273
+
274
+ export function getLineInfo ( text : string , lineStarts : ReadonlyArray < number > ) : LineInfo {
275
+ return {
276
+ getLineCount : ( ) => lineStarts . length ,
277
+ getLineText : line => text . substring ( lineStarts [ line ] , lineStarts [ line + 1 ] )
278
+ } ;
279
+ }
280
+
269
281
/**
270
282
* Tries to find the sourceMappingURL comment at the end of a file.
271
- * @param text The source text of the file.
272
- * @param lineStarts The line starts of the file.
273
283
*/
274
- export function tryGetSourceMappingURL ( text : string , lineStarts : ReadonlyArray < number > = computeLineStarts ( text ) ) {
275
- for ( let index = lineStarts . length - 1 ; index >= 0 ; index -- ) {
276
- const line = text . substring ( lineStarts [ index ] , lineStarts [ index + 1 ] ) ;
284
+ export function tryGetSourceMappingURL ( lineInfo : LineInfo ) {
285
+ for ( let index = lineInfo . getLineCount ( ) - 1 ; index >= 0 ; index -- ) {
286
+ const line = lineInfo . getLineText ( index ) ;
277
287
const comment = sourceMapCommentRegExp . exec ( line ) ;
278
288
if ( comment ) {
279
289
return comment [ 1 ] ;
@@ -573,7 +583,10 @@ namespace ts {
573
583
}
574
584
575
585
function compareSourcePositions ( left : SourceMappedPosition , right : SourceMappedPosition ) {
576
- return compareValues ( left . sourceIndex , right . sourceIndex ) ;
586
+ // Compares sourcePosition without comparing sourceIndex
587
+ // since the mappings are grouped by sourceIndex
588
+ Debug . assert ( left . sourceIndex === right . sourceIndex ) ;
589
+ return compareValues ( left . sourcePosition , right . sourcePosition ) ;
577
590
}
578
591
579
592
function compareGeneratedPositions ( left : MappedPosition , right : MappedPosition ) {
@@ -592,11 +605,9 @@ namespace ts {
592
605
const mapDirectory = getDirectoryPath ( mapPath ) ;
593
606
const sourceRoot = map . sourceRoot ? getNormalizedAbsolutePath ( map . sourceRoot , mapDirectory ) : mapDirectory ;
594
607
const generatedAbsoluteFilePath = getNormalizedAbsolutePath ( map . file , mapDirectory ) ;
595
- const generatedCanonicalFilePath = host . getCanonicalFileName ( generatedAbsoluteFilePath ) as Path ;
596
- const generatedFile = host . getSourceFileLike ( generatedCanonicalFilePath ) ;
608
+ const generatedFile = host . getSourceFileLike ( generatedAbsoluteFilePath ) ;
597
609
const sourceFileAbsolutePaths = map . sources . map ( source => getNormalizedAbsolutePath ( source , sourceRoot ) ) ;
598
- const sourceFileCanonicalPaths = sourceFileAbsolutePaths . map ( source => host . getCanonicalFileName ( source ) as Path ) ;
599
- const sourceToSourceIndexMap = createMapFromEntries ( sourceFileCanonicalPaths . map ( ( source , i ) => [ source , i ] as [ string , number ] ) ) ;
610
+ const sourceToSourceIndexMap = createMapFromEntries ( sourceFileAbsolutePaths . map ( ( source , i ) => [ host . getCanonicalFileName ( source ) , i ] as [ string , number ] ) ) ;
600
611
let decodedMappings : ReadonlyArray < MappedPosition > | undefined ;
601
612
let generatedMappings : SortedReadonlyArray < MappedPosition > | undefined ;
602
613
let sourceMappings : ReadonlyArray < SortedReadonlyArray < SourceMappedPosition > > | undefined ;
@@ -608,16 +619,15 @@ namespace ts {
608
619
609
620
function processMapping ( mapping : Mapping ) : MappedPosition {
610
621
const generatedPosition = generatedFile !== undefined
611
- ? getPositionOfLineAndCharacterWithEdits ( generatedFile , mapping . generatedLine , mapping . generatedCharacter )
622
+ ? getPositionOfLineAndCharacter ( generatedFile , mapping . generatedLine , mapping . generatedCharacter , /*allowEdits*/ true )
612
623
: - 1 ;
613
624
let source : string | undefined ;
614
625
let sourcePosition : number | undefined ;
615
626
if ( isSourceMapping ( mapping ) ) {
616
- const sourceFilePath = sourceFileCanonicalPaths [ mapping . sourceIndex ] ;
617
- const sourceFile = host . getSourceFileLike ( sourceFilePath ) ;
627
+ const sourceFile = host . getSourceFileLike ( sourceFileAbsolutePaths [ mapping . sourceIndex ] ) ;
618
628
source = map . sources [ mapping . sourceIndex ] ;
619
629
sourcePosition = sourceFile !== undefined
620
- ? getPositionOfLineAndCharacterWithEdits ( sourceFile , mapping . sourceLine , mapping . sourceCharacter )
630
+ ? getPositionOfLineAndCharacter ( sourceFile , mapping . sourceLine , mapping . sourceCharacter , /*allowEdits*/ true )
621
631
: - 1 ;
622
632
}
623
633
return {
0 commit comments