@@ -22,7 +22,7 @@ namespace ts {
2222 *
2323 * @param sourceFile The source file.
2424 */
25- setSourceFile ( sourceFile : SourceFile ) : void ;
25+ setSourceFile ( sourceFile : SourceMapSource ) : void ;
2626
2727 /**
2828 * Emits a mapping.
@@ -81,7 +81,7 @@ namespace ts {
8181 export function createSourceMapWriter ( host : EmitHost , writer : EmitTextWriter ) : SourceMapWriter {
8282 const compilerOptions = host . getCompilerOptions ( ) ;
8383 const extendedDiagnostics = compilerOptions . extendedDiagnostics ;
84- let currentSourceFile : SourceFile ;
84+ let currentSource : SourceMapSource ;
8585 let currentSourceText : string ;
8686 let sourceMapDir : string ; // The directory in which sourcemap will be
8787
@@ -109,6 +109,13 @@ namespace ts {
109109 getSourceMappingURL,
110110 } ;
111111
112+ /**
113+ * Skips trivia such as comments and white-space that can optionally overriden by the source map source
114+ */
115+ function skipSourceTrivia ( pos : number ) : number {
116+ return currentSource . skipTrivia ? currentSource . skipTrivia ( pos ) : skipTrivia ( currentSourceText , pos ) ;
117+ }
118+
112119 /**
113120 * Initialize the SourceMapWriter for a new output file.
114121 *
@@ -125,7 +132,7 @@ namespace ts {
125132 reset ( ) ;
126133 }
127134
128- currentSourceFile = undefined ;
135+ currentSource = undefined ;
129136 currentSourceText = undefined ;
130137
131138 // Current source map file and its index in the sources list
@@ -192,7 +199,7 @@ namespace ts {
192199 return ;
193200 }
194201
195- currentSourceFile = undefined ;
202+ currentSource = undefined ;
196203 sourceMapDir = undefined ;
197204 sourceMapSourceIndex = undefined ;
198205 lastRecordedSourceMapSpan = undefined ;
@@ -263,7 +270,7 @@ namespace ts {
263270 performance . mark ( "beforeSourcemap" ) ;
264271 }
265272
266- const sourceLinePos = getLineAndCharacterOfPosition ( currentSourceFile , pos ) ;
273+ const sourceLinePos = getLineAndCharacterOfPosition ( currentSource , pos ) ;
267274
268275 // Convert the location to be one-based.
269276 sourceLinePos . line ++ ;
@@ -320,14 +327,22 @@ namespace ts {
320327 if ( node ) {
321328 const emitNode = node . emitNode ;
322329 const emitFlags = emitNode && emitNode . flags ;
323- const { pos, end } = emitNode && emitNode . sourceMapRange || node ;
330+ const range = emitNode && emitNode . sourceMapRange ;
331+ const { pos, end } = range || node ;
332+ let source = range && range . source ;
333+ const oldSource = currentSource ;
334+ if ( source === oldSource ) source = undefined ;
335+
336+ if ( source ) setSourceFile ( source ) ;
324337
325338 if ( node . kind !== SyntaxKind . NotEmittedStatement
326339 && ( emitFlags & EmitFlags . NoLeadingSourceMap ) === 0
327340 && pos >= 0 ) {
328- emitPos ( skipTrivia ( currentSourceText , pos ) ) ;
341+ emitPos ( skipSourceTrivia ( pos ) ) ;
329342 }
330343
344+ if ( source ) setSourceFile ( oldSource ) ;
345+
331346 if ( emitFlags & EmitFlags . NoNestedSourceMaps ) {
332347 disabled = true ;
333348 emitCallback ( hint , node ) ;
@@ -337,11 +352,15 @@ namespace ts {
337352 emitCallback ( hint , node ) ;
338353 }
339354
355+ if ( source ) setSourceFile ( source ) ;
356+
340357 if ( node . kind !== SyntaxKind . NotEmittedStatement
341358 && ( emitFlags & EmitFlags . NoTrailingSourceMap ) === 0
342359 && end >= 0 ) {
343360 emitPos ( end ) ;
344361 }
362+
363+ if ( source ) setSourceFile ( oldSource ) ;
345364 }
346365 }
347366
@@ -362,7 +381,7 @@ namespace ts {
362381 const emitFlags = emitNode && emitNode . flags ;
363382 const range = emitNode && emitNode . tokenSourceMapRanges && emitNode . tokenSourceMapRanges [ token ] ;
364383
365- tokenPos = skipTrivia ( currentSourceText , range ? range . pos : tokenPos ) ;
384+ tokenPos = skipSourceTrivia ( range ? range . pos : tokenPos ) ;
366385 if ( ( emitFlags & EmitFlags . NoTokenLeadingSourceMaps ) === 0 && tokenPos >= 0 ) {
367386 emitPos ( tokenPos ) ;
368387 }
@@ -382,21 +401,21 @@ namespace ts {
382401 *
383402 * @param sourceFile The source file.
384403 */
385- function setSourceFile ( sourceFile : SourceFile ) {
404+ function setSourceFile ( sourceFile : SourceMapSource ) {
386405 if ( disabled ) {
387406 return ;
388407 }
389408
390- currentSourceFile = sourceFile ;
391- currentSourceText = currentSourceFile . text ;
409+ currentSource = sourceFile ;
410+ currentSourceText = currentSource . text ;
392411
393412 // Add the file to tsFilePaths
394413 // If sourceroot option: Use the relative path corresponding to the common directory path
395414 // otherwise source locations relative to map file location
396415 const sourcesDirectoryPath = compilerOptions . sourceRoot ? host . getCommonSourceDirectory ( ) : sourceMapDir ;
397416
398417 const source = getRelativePathToDirectoryOrUrl ( sourcesDirectoryPath ,
399- currentSourceFile . fileName ,
418+ currentSource . fileName ,
400419 host . getCurrentDirectory ( ) ,
401420 host . getCanonicalFileName ,
402421 /*isAbsolutePathAnUrl*/ true ) ;
@@ -407,10 +426,10 @@ namespace ts {
407426 sourceMapData . sourceMapSources . push ( source ) ;
408427
409428 // The one that can be used from program to get the actual source file
410- sourceMapData . inputSourceFileNames . push ( currentSourceFile . fileName ) ;
429+ sourceMapData . inputSourceFileNames . push ( currentSource . fileName ) ;
411430
412431 if ( compilerOptions . inlineSources ) {
413- sourceMapData . sourceMapSourcesContent . push ( currentSourceFile . text ) ;
432+ sourceMapData . sourceMapSourcesContent . push ( currentSource . text ) ;
414433 }
415434 }
416435 }
0 commit comments