@@ -266,14 +266,24 @@ namespace ts {
266266 const sourceMapCommentRegExp = / ^ \/ \/ [ @ # ] s o u r c e [ M ] a p p i n g U R L = ( .+ ) \s * $ / ;
267267 const whitespaceOrMapCommentRegExp = / ^ \s * ( \/ \/ [ @ # ] .* ) ? $ / ;
268268
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+
269281 /**
270282 * 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.
273283 */
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 ) ;
277287 const comment = sourceMapCommentRegExp . exec ( line ) ;
278288 if ( comment ) {
279289 return comment [ 1 ] ;
@@ -573,7 +583,10 @@ namespace ts {
573583 }
574584
575585 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 ) ;
577590 }
578591
579592 function compareGeneratedPositions ( left : MappedPosition , right : MappedPosition ) {
@@ -592,11 +605,9 @@ namespace ts {
592605 const mapDirectory = getDirectoryPath ( mapPath ) ;
593606 const sourceRoot = map . sourceRoot ? getNormalizedAbsolutePath ( map . sourceRoot , mapDirectory ) : mapDirectory ;
594607 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 ) ;
597609 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 ] ) ) ;
600611 let decodedMappings : ReadonlyArray < MappedPosition > | undefined ;
601612 let generatedMappings : SortedReadonlyArray < MappedPosition > | undefined ;
602613 let sourceMappings : ReadonlyArray < SortedReadonlyArray < SourceMappedPosition > > | undefined ;
@@ -608,16 +619,15 @@ namespace ts {
608619
609620 function processMapping ( mapping : Mapping ) : MappedPosition {
610621 const generatedPosition = generatedFile !== undefined
611- ? getPositionOfLineAndCharacterWithEdits ( generatedFile , mapping . generatedLine , mapping . generatedCharacter )
622+ ? getPositionOfLineAndCharacter ( generatedFile , mapping . generatedLine , mapping . generatedCharacter , /*allowEdits*/ true )
612623 : - 1 ;
613624 let source : string | undefined ;
614625 let sourcePosition : number | undefined ;
615626 if ( isSourceMapping ( mapping ) ) {
616- const sourceFilePath = sourceFileCanonicalPaths [ mapping . sourceIndex ] ;
617- const sourceFile = host . getSourceFileLike ( sourceFilePath ) ;
627+ const sourceFile = host . getSourceFileLike ( sourceFileAbsolutePaths [ mapping . sourceIndex ] ) ;
618628 source = map . sources [ mapping . sourceIndex ] ;
619629 sourcePosition = sourceFile !== undefined
620- ? getPositionOfLineAndCharacterWithEdits ( sourceFile , mapping . sourceLine , mapping . sourceCharacter )
630+ ? getPositionOfLineAndCharacter ( sourceFile , mapping . sourceLine , mapping . sourceCharacter , /*allowEdits*/ true )
621631 : - 1 ;
622632 }
623633 return {
0 commit comments