UNPKG

4.58 kBJavaScriptView Raw
1'use strict';
2/*jshint asi: true */
3
4var test = require('trap').test;
5var convert = require('convert-source-map');
6var parse = require('parse-base64vlq-mappings');
7var commentRegex = require('convert-source-map').commentRegex;
8var combine = require('..');
9
10function checkMappings(foo, sm, lineOffset) {
11 function inspect(obj, depth) {
12 return require('util').inspect(obj, false, depth || 5, true);
13 }
14
15 var fooMappings = parse(foo.mappings);
16 var mappings = parse(sm.mappings);
17
18 var genLinesOffset = true;
19 var origLinesSame = true;
20 for (var i = 0; i < mappings.length; i++) {
21 var fooGen = fooMappings[i].generated;
22 var fooOrig = fooMappings[i].original;
23 var gen = mappings[i].generated
24 var orig = mappings[i].original;
25
26 if (gen.column !== fooGen.column || gen.line !== (fooGen.line + lineOffset)) {
27 console.error(
28 'generated mapping at %s not offset properly:\ninput: [%s]\noutput:[%s]\n\n',
29 i ,
30 inspect(fooGen),
31 inspect(gen)
32 );
33 genLinesOffset = false;
34 }
35
36 if (orig.column !== fooOrig.column || orig.line !== fooOrig.line) {
37 console.error(
38 'original mapping at %s is not the same as the genrated mapping:\ninput: [%s]\noutput:[%s]\n\n',
39 i ,
40 inspect(fooOrig),
41 inspect(orig)
42 );
43 origLinesSame = false;
44 }
45 }
46 return { genLinesOffset: genLinesOffset, origLinesSame: origLinesSame };
47}
48
49var foo = {
50 version : 3,
51 file : 'foo.js',
52 sourceRoot : '',
53 sources : [ 'foo.coffee' ],
54 names : [],
55 mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',
56 sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };
57
58test('add one file with inlined source', function (t) {
59
60 var mapComment = convert.fromObject(foo).toComment();
61 var file = {
62 id: 'xyz'
63 , source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
64 , sourceFile: 'foo.js'
65 };
66
67 var lineOffset = 3
68 var base64 = combine.create()
69 .addFile(file, { line: lineOffset })
70 .base64()
71
72 var sm = convert.fromBase64(base64).toObject();
73 var res = checkMappings(foo, sm, lineOffset);
74
75 t.ok(res.genLinesOffset, 'all generated lines are offset properly and columns unchanged')
76 t.ok(res.origLinesSame, 'all original lines and columns are unchanged')
77 t.equal(sm.sourcesContent[0], foo.sourcesContent[0], 'includes the original source')
78 t.equal(sm.sources[0], 'foo.coffee', 'includes original filename')
79});
80
81
82test('add one file without inlined source', function (t) {
83
84 var mapComment = convert
85 .fromObject(foo)
86 .setProperty('sourcesContent', [])
87 .toComment();
88
89 var file = {
90 id: 'xyz'
91 , source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
92 , sourceFile: 'foo.js'
93 };
94
95 var lineOffset = 3
96 var base64 = combine.create()
97 .addFile(file, { line: lineOffset })
98 .base64()
99
100 var sm = convert.fromBase64(base64).toObject();
101 var mappings = parse(sm.mappings);
102
103 t.equal(sm.sourcesContent[0], file.source, 'includes the generated source')
104 t.equal(sm.sources[0], 'foo.js', 'includes generated filename')
105
106 t.deepEqual(
107 mappings
108 , [ { generated: { line: 4, column: 0 },
109 original: { line: 1, column: 0 } },
110 { generated: { line: 5, column: 0 },
111 original: { line: 2, column: 0 } },
112 { generated: { line: 6, column: 0 },
113 original: { line: 3, column: 0 } },
114 { generated: { line: 7, column: 0 },
115 original: { line: 4, column: 0 } },
116 { generated: { line: 8, column: 0 },
117 original: { line: 5, column: 0 } },
118 { generated: { line: 9, column: 0 },
119 original: { line: 6, column: 0 } },
120 { generated: { line: 10, column: 0 },
121 original: { line: 7, column: 0 } } ]
122 , 'generates mappings offset by the given line'
123 )
124})
125
126test('remove comments', function (t) {
127 var mapComment = convert.fromObject(foo).toComment();
128
129 function sourcemapComments(src) {
130 var matches = src.match(commentRegex);
131 return matches ? matches.length : 0;
132 }
133
134 t.equal(sourcemapComments('var a = 1;\n' + mapComment), 1);
135
136 [ ''
137 , 'var a = 1;\n' + mapComment
138 , 'var a = 1;\n' + mapComment + '\nvar b = 5;\n' + mapComment
139 ] .forEach(function (x) {
140 var removed = combine.removeComments(x)
141 t.equal(sourcemapComments(removed), 0)
142 })
143})