10
10
11
11
import { assert , expect } from 'chai' ;
12
12
import * as path from 'path' ;
13
- import { SourceMapConsumer } from 'source-map' ;
13
+ import { RawSourceMap , SourceMapConsumer } from 'source-map' ;
14
14
import * as ts from 'typescript' ;
15
15
16
16
import * as cliSupport from '../src/cli_support' ;
@@ -251,14 +251,25 @@ function createTests(useTransformer: boolean) {
251
251
expect ( sourceMap . originalPositionFor ( { line, column} ) . line ) . to . equal ( 6 , 'method position' ) ;
252
252
} ) ;
253
253
254
- it ( 'handles input source maps with different file names than supplied to tsc' , ( ) = > {
254
+ function createInputWithSourceMap ( overrides : Partial < RawSourceMap > = { } ) : Map < string , string > {
255
255
const sources = new Map < string , string > ( ) ;
256
- const inputSourceMap =
257
- `{"version":3,"sources":["original.ts"],"names":[],"mappings":"AAAA,MAAM,EAAE,EAAE,CAAC","file":"foo/bar/intermediate.ts","sourceRoot":""}` ;
258
- const encodedSourceMap = Buffer . from ( inputSourceMap , 'utf8' ) . toString ( 'base64' ) ;
256
+ const inputSourceMap = {
257
+ 'version' : 3 ,
258
+ 'sources' : [ 'original.ts' ] ,
259
+ 'names' : [ ] ,
260
+ 'mappings' : 'AAAA,MAAM,EAAE,EAAE,CAAC' ,
261
+ 'file' : 'intermediate.ts' ,
262
+ 'sourceRoot' : '' ,
263
+ ...overrides
264
+ } ;
265
+ const encodedSourceMap = Buffer . from ( JSON . stringify ( inputSourceMap ) , 'utf8' ) . toString ( 'base64' ) ;
259
266
sources . set ( 'intermediate.ts' , `const x = 3;
260
267
//# sourceMappingURL=data:application/json;base64,${ encodedSourceMap } ` ) ;
268
+ return sources ;
269
+ }
261
270
271
+ it ( 'handles input source maps with different file names than supplied to tsc' , ( ) => {
272
+ const sources = createInputWithSourceMap ( { file : 'foo/bar/intermediate.ts' } ) ;
262
273
const { compiledJS, sourceMap} = compile ( sources , { useTransformer} ) ;
263
274
expect ( getInlineSourceMapCount ( compiledJS ) ) . to . equal ( 0 ) ;
264
275
@@ -268,6 +279,18 @@ function createTests(useTransformer: boolean) {
268
279
. to . equal ( 'original.ts' , 'input file name' ) ;
269
280
} ) ;
270
281
282
+ it ( 'handles input source maps with an outDir different than the rootDir' , ( ) => {
283
+ const sources = createInputWithSourceMap ( { file : 'foo/bar/intermediate.ts' } ) ;
284
+
285
+ const { compiledJS, sourceMap} =
286
+ compile ( sources , { inlineSourceMap : true , useTransformer, outFile : '/out/output.js' } ) ;
287
+
288
+ const { line, column} = getLineAndColumn ( compiledJS , 'x = 3' ) ;
289
+ expect ( sourceMap . originalPositionFor ( { line, column} ) . source )
290
+ . to . equal ( 'original.ts' , 'input file name' ) ;
291
+ } ) ;
292
+
293
+
271
294
it ( `doesn't blow up putting an inline source map in an empty file` , ( ) => {
272
295
const sources = new Map < string , string > ( ) ;
273
296
sources . set ( 'input.ts' , `` ) ;
0 commit comments