UNPKG

8.12 kBJavaScriptView Raw
1'use strict';
2/*jshint asi: true */
3
4var test = require('tap').test
5 , generator = require('inline-source-map')
6 , convert = require('..')
7
8var gen = generator()
9 .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
10 .addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })
11
12 , base64 = gen.base64Encode()
13 , comment = gen.inlineMappingUrl()
14 , json = gen.toString()
15 , obj = JSON.parse(json)
16
17test('different formats', function (t) {
18
19 t.equal(convert.fromComment(comment).toComment(), comment, 'comment -> comment')
20 t.equal(convert.fromComment(comment).toBase64(), base64, 'comment -> base64')
21 t.equal(convert.fromComment(comment).toJSON(), json, 'comment -> json')
22 t.deepEqual(convert.fromComment(comment).toObject(), obj, 'comment -> object')
23
24 t.equal(convert.fromBase64(base64).toBase64(), base64, 'base64 -> base64')
25 t.equal(convert.fromBase64(base64).toComment(), comment, 'base64 -> comment')
26 t.equal(convert.fromBase64(base64).toJSON(), json, 'base64 -> json')
27 t.deepEqual(convert.fromBase64(base64).toObject(), obj, 'base64 -> object')
28
29 t.equal(convert.fromJSON(json).toJSON(), json, 'json -> json')
30 t.equal(convert.fromJSON(json).toBase64(), base64, 'json -> base64')
31 t.equal(convert.fromJSON(json).toComment(), comment, 'json -> comment')
32 t.deepEqual(convert.fromJSON(json).toObject(), obj, 'json -> object')
33 t.end()
34})
35
36test('to object returns a copy', function (t) {
37 var c = convert.fromJSON(json)
38 var o = c.toObject()
39 o.version = '99';
40 t.equal(c.toObject().version, 3, 'setting property on returned object does not affect original')
41 t.end()
42})
43
44test('to multi-line map', function (t) {
45 var c = convert.fromObject(obj);
46 var s = c.toComment({ multiline: true });
47 t.similar(s, /^\/\*# sourceMappingURL=.+ \*\/$/);
48 t.end();
49})
50
51test('to map file comment', function (t) {
52 t.equal(convert.generateMapFileComment('index.js.map'), '//# sourceMappingURL=index.js.map');
53 t.equal(convert.generateMapFileComment('index.css.map', { multiline: true }), '/*# sourceMappingURL=index.css.map */');
54 t.end();
55})
56
57test('from source', function (t) {
58 var foo = [
59 'function foo() {'
60 , ' console.log("hello I am foo");'
61 , ' console.log("who are you");'
62 , '}'
63 , ''
64 , 'foo();'
65 , ''
66 ].join('\n')
67 , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
68 , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
69
70 function getComment(src) {
71 var map = convert.fromSource(src);
72 return map ? map.toComment() : null;
73 }
74
75 t.equal(getComment(foo), null, 'no comment returns null')
76 t.equal(getComment(foo + map), map, 'beginning of last line')
77 t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
78 t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
79 t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
80 t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
81 t.end()
82})
83
84test('from source with a large source', function (t) {
85 var foo = [
86 'function foo() {'
87 , ' console.log("hello I am foo");'
88 , ' console.log("who are you");'
89 , '}'
90 , ''
91 , 'foo();'
92 , ''
93 ].join('\n')
94 , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
95 , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
96
97 function getComment(src) {
98 var map = convert.fromSource(src, true);
99 return map ? map.toComment() : null;
100 }
101
102 t.equal(getComment(foo), null, 'no comment returns null')
103 t.equal(getComment(foo + map), map, 'beginning of last line')
104 t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
105 t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
106 t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
107 t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
108 t.end()
109})
110
111test('remove comments', function (t) {
112 var foo = [
113 'function foo() {'
114 , ' console.log("hello I am foo");'
115 , ' console.log("who are you");'
116 , '}'
117 , ''
118 , 'foo();'
119 , ''
120 ].join('\n')
121 // this one is old spec on purpose
122 , map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
123 , otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
124 , extraCode = '\nconsole.log("more code");\nfoo()\n'
125
126 t.equal(convert.removeComments(foo + map), foo, 'from last line')
127 t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
128 t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')
129 t.end()
130})
131
132test('remove map file comments', function (t) {
133 var foo = [
134 'function foo() {'
135 , ' console.log("hello I am foo");'
136 , ' console.log("who are you");'
137 , '}'
138 , ''
139 , 'foo();'
140 , ''
141 ].join('\n')
142 , fileMap1 = '//# sourceMappingURL=foo.js.map'
143 , fileMap2 = '/*# sourceMappingURL=foo.js.map */';
144
145 t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
146 t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
147 t.end()
148})
149
150test('pretty json', function (t) {
151 var mod = convert.fromJSON(json).toJSON(2)
152 , expected = JSON.stringify(obj, null, 2);
153
154 t.equal(
155 mod
156 , expected
157 , 'pretty prints json when space is given')
158 t.end()
159})
160
161test('adding properties', function (t) {
162 var mod = convert
163 .fromJSON(json)
164 .addProperty('foo', 'bar')
165 .toJSON()
166 , expected = JSON.parse(json);
167 expected.foo = 'bar';
168 t.equal(
169 mod
170 , JSON.stringify(expected)
171 , 'includes added property'
172 )
173 t.end()
174})
175
176test('setting properties', function (t) {
177 var mod = convert
178 .fromJSON(json)
179 .setProperty('version', '2')
180 .setProperty('mappings', ';;;UACG')
181 .setProperty('should add', 'this')
182 .toJSON()
183 , expected = JSON.parse(json);
184 expected.version = '2';
185 expected.mappings = ';;;UACG';
186 expected['should add'] = 'this';
187 t.equal(
188 mod
189 , JSON.stringify(expected)
190 , 'includes new property and changes existing properties'
191 )
192 t.end()
193})
194
195test('getting properties', function (t) {
196 var sm = convert.fromJSON(json)
197
198 t.equal(sm.getProperty('version'), 3, 'gets version')
199 t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
200 t.end()
201})
202
203test('return null fromSource when largeSource is true', function(t) {
204 var mod = convert.fromSource('', true)
205 , expected = null;
206
207 t.equal(
208 mod
209 , expected
210 , 'return value should be null'
211 )
212 t.end()
213})