@@ -12,6 +12,12 @@ common.expectWarning(
1212 'stripTypeScriptTypes is an experimental feature and might change at any time' ,
1313) ;
1414
15+ const sourceToBeTransformed = `
16+ namespace MathUtil {
17+ export const add = (a: number, b: number) => a + b;
18+ }` ;
19+ const sourceToBeTransformedMapping = 'UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' ;
20+
1521test ( 'stripTypeScriptTypes' , ( ) => {
1622 const source = 'const x: number = 1;' ;
1723 const result = stripTypeScriptTypes ( source ) ;
@@ -48,45 +54,52 @@ test('stripTypeScriptTypes sourceUrl throws when mode is strip', () => {
4854} ) ;
4955
5056test ( 'stripTypeScriptTypes source map when mode is transform' , ( ) => {
51- const source = `
52- namespace MathUtil {
53- export const add = (a: number, b: number) => a + b;
54- }` ;
55- const result = stripTypeScriptTypes ( source , { mode : 'transform' , sourceMap : true } ) ;
57+ const result = stripTypeScriptTypes ( sourceToBeTransformed , { mode : 'transform' , sourceMap : true } ) ;
5658 const script = new vm . Script ( result ) ;
5759 const sourceMap =
5860 {
5961 version : 3 ,
60- sources : [
61- '<anon>' ,
62- ] ,
63- sourcesContent : [
64- '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }' ,
65- ] ,
62+ sources : [ '' ] ,
6663 names : [ ] ,
67- mappings : ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA'
64+ mappings : sourceToBeTransformedMapping ,
6865 } ;
69- assert ( script . sourceMapURL , `sourceMappingURL=data:application/json;base64,${ JSON . stringify ( sourceMap ) } ` ) ;
66+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
67+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
7068} ) ;
7169
7270test ( 'stripTypeScriptTypes source map when mode is transform and sourceUrl' , ( ) => {
73- const source = `
74- namespace MathUtil {
75- export const add = (a: number, b: number) => a + b;
76- }` ;
77- const result = stripTypeScriptTypes ( source , { mode : 'transform' , sourceMap : true , sourceUrl : 'test.ts' } ) ;
71+ const result = stripTypeScriptTypes ( sourceToBeTransformed , {
72+ mode : 'transform' ,
73+ sourceMap : true ,
74+ sourceUrl : 'test.ts'
75+ } ) ;
76+ const script = new vm . Script ( result ) ;
77+ const sourceMap =
78+ {
79+ version : 3 ,
80+ sources : [ 'test.ts' ] ,
81+ names : [ ] ,
82+ mappings : sourceToBeTransformedMapping ,
83+ } ;
84+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
85+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
86+ } ) ;
87+
88+ test ( 'stripTypeScriptTypes source map when mode is transform and sourceUrl with non-latin-1 chars' , ( ) => {
89+ const sourceUrl = 'dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`.cts' ;
90+ const result = stripTypeScriptTypes ( sourceToBeTransformed , {
91+ mode : 'transform' ,
92+ sourceMap : true ,
93+ sourceUrl,
94+ } ) ;
7895 const script = new vm . Script ( result ) ;
7996 const sourceMap =
8097 {
8198 version : 3 ,
82- sources : [
83- 'test.ts' ,
84- ] ,
85- sourcesContent : [
86- '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }' ,
87- ] ,
99+ sources : [ sourceUrl ] ,
88100 names : [ ] ,
89- mappings : ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA'
101+ mappings : sourceToBeTransformedMapping ,
90102 } ;
91- assert ( script . sourceMapURL , `sourceMappingURL=data:application/json;base64,${ JSON . stringify ( sourceMap ) } ` ) ;
103+ const inlinedSourceMap = Buffer . from ( JSON . stringify ( sourceMap ) ) . toString ( 'base64' ) ;
104+ assert . strictEqual ( script . sourceMapURL , `data:application/json;base64,${ inlinedSourceMap } ` ) ;
92105} ) ;
0 commit comments