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('from source', function (t) {
45
var foo = [
46
'function foo() {'
47
, ' console.log("hello I am foo");'
48
, ' console.log("who are you");'
49
, '}'
50
, ''
51
, 'foo();'
52
, ''
53
].join('\n')
54
, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
55
, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
56
57
function getComment(src) {
58
var map = convert.fromSource(src);
59
return map ? map.toComment() : null;
60
}
61
62
t.equal(getComment(foo), null, 'no comment returns null')
63
t.equal(getComment(foo + map), map, 'beginning of last line')
64
t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
65
t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
66
t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
67
t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
68
t.end()
69
})
70
71
test('remove comments', function (t) {
72
var foo = [
73
'function foo() {'
74
, ' console.log("hello I am foo");'
75
, ' console.log("who are you");'
76
, '}'
77
, ''
78
, 'foo();'
79
, ''
80
].join('\n')
81
// this one is old spec on purpose
82
, map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
83
, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
84
, extraCode = '\nconsole.log("more code");\nfoo()\n'
85
86
t.equal(convert.removeComments(foo + map), foo, 'from last line')
87
t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
88
t.equal(convert.removeComments(foo + otherMap + extraCode + map + map), foo + extraCode, 'multiple comments from the middle of code')
89
t.end()
90
})
91
92
test('remove map file comments', function (t) {
93
var foo = [
94
'function foo() {'
95
, ' console.log("hello I am foo");'
96
, ' console.log("who are you");'
97
, '}'
98
, ''
99
, 'foo();'
100
, ''
101
].join('\n')
102
, fileMap1 = '//# sourceMappingURL=foo.js.map'
103
, fileMap2 = '/*# sourceMappingURL=foo.js.map */';
104
105
t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
106
t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
107
t.end()
108
})
109
110
test('pretty json', function (t) {
111
var mod = convert.fromJSON(json).toJSON(2)
112
, expected = JSON.stringify(obj, null, 2);
113
114
t.equal(
115
mod
116
, expected
117
, 'pretty prints json when space is given')
118
t.end()
119
})
120
121
test('adding properties', function (t) {
122
var mod = convert
123
.fromJSON(json)
124
.addProperty('foo', 'bar')
125
.toJSON()
126
, expected = JSON.parse(json);
127
expected.foo = 'bar';
128
t.equal(
129
mod
130
, JSON.stringify(expected)
131
, 'includes added property'
132
)
133
t.end()
134
})
135
136
test('setting properties', function (t) {
137
var mod = convert
138
.fromJSON(json)
139
.setProperty('version', '2')
140
.setProperty('mappings', ';;;UACG')
141
.setProperty('should add', 'this')
142
.toJSON()
143
, expected = JSON.parse(json);
144
expected.version = '2';
145
expected.mappings = ';;;UACG';
146
expected['should add'] = 'this';
147
t.equal(
148
mod
149
, JSON.stringify(expected)
150
, 'includes new property and changes existing properties'
151
)
152
t.end()
153
})
154
155
test('getting properties', function (t) {
156
var sm = convert.fromJSON(json)
157
158
t.equal(sm.getProperty('version'), 3, 'gets version')
159
t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
160
t.end()
161
})
162
163