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