Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80556 views
1
'use strict';
2
/*jshint asi: true */
3
4
var test = require('tap').test
5
, generator = require('inline-source-map')
6
, convert = require('..')
7
8
var 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
17
test('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
36
test('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
44
test('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
51
test('from source', function (t) {
52
var foo = [
53
'function foo() {'
54
, ' console.log("hello I am foo");'
55
, ' console.log("who are you");'
56
, '}'
57
, ''
58
, 'foo();'
59
, ''
60
].join('\n')
61
, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
62
, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
63
64
function getComment(src) {
65
var map = convert.fromSource(src);
66
return map ? map.toComment() : null;
67
}
68
69
t.equal(getComment(foo), null, 'no comment returns null')
70
t.equal(getComment(foo + map), map, 'beginning of last line')
71
t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
72
t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
73
t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
74
t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
75
t.end()
76
})
77
78
test('from source with a large source', function (t) {
79
var foo = [
80
'function foo() {'
81
, ' console.log("hello I am foo");'
82
, ' console.log("who are you");'
83
, '}'
84
, ''
85
, 'foo();'
86
, ''
87
].join('\n')
88
, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
89
, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
90
91
function getComment(src) {
92
var map = convert.fromSource(src, true);
93
return map ? map.toComment() : null;
94
}
95
96
t.equal(getComment(foo), null, 'no comment returns null')
97
t.equal(getComment(foo + map), map, 'beginning of last line')
98
t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
99
t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
100
t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
101
t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
102
t.end()
103
})
104
105
test('remove comments', function (t) {
106
var foo = [
107
'function foo() {'
108
, ' console.log("hello I am foo");'
109
, ' console.log("who are you");'
110
, '}'
111
, ''
112
, 'foo();'
113
, ''
114
].join('\n')
115
// this one is old spec on purpose
116
, map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
117
, otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
118
, extraCode = '\nconsole.log("more code");\nfoo()\n'
119
120
t.equal(convert.removeComments(foo + map), foo, 'from last line')
121
t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
122
t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')
123
t.end()
124
})
125
126
test('remove map file comments', function (t) {
127
var foo = [
128
'function foo() {'
129
, ' console.log("hello I am foo");'
130
, ' console.log("who are you");'
131
, '}'
132
, ''
133
, 'foo();'
134
, ''
135
].join('\n')
136
, fileMap1 = '//# sourceMappingURL=foo.js.map'
137
, fileMap2 = '/*# sourceMappingURL=foo.js.map */';
138
139
t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
140
t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
141
t.end()
142
})
143
144
test('pretty json', function (t) {
145
var mod = convert.fromJSON(json).toJSON(2)
146
, expected = JSON.stringify(obj, null, 2);
147
148
t.equal(
149
mod
150
, expected
151
, 'pretty prints json when space is given')
152
t.end()
153
})
154
155
test('adding properties', function (t) {
156
var mod = convert
157
.fromJSON(json)
158
.addProperty('foo', 'bar')
159
.toJSON()
160
, expected = JSON.parse(json);
161
expected.foo = 'bar';
162
t.equal(
163
mod
164
, JSON.stringify(expected)
165
, 'includes added property'
166
)
167
t.end()
168
})
169
170
test('setting properties', function (t) {
171
var mod = convert
172
.fromJSON(json)
173
.setProperty('version', '2')
174
.setProperty('mappings', ';;;UACG')
175
.setProperty('should add', 'this')
176
.toJSON()
177
, expected = JSON.parse(json);
178
expected.version = '2';
179
expected.mappings = ';;;UACG';
180
expected['should add'] = 'this';
181
t.equal(
182
mod
183
, JSON.stringify(expected)
184
, 'includes new property and changes existing properties'
185
)
186
t.end()
187
})
188
189
test('getting properties', function (t) {
190
var sm = convert.fromJSON(json)
191
192
t.equal(sm.getProperty('version'), 3, 'gets version')
193
t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
194
t.end()
195
})
196
197