Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
2
/*
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
6
*/
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
13
var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
14
var SourceNode = require('../../lib/source-map/source-node').SourceNode;
15
var util = require('./util');
16
17
exports['test some simple stuff'] = function (assert, util) {
18
var map = new SourceMapGenerator({
19
file: 'foo.js',
20
sourceRoot: '.'
21
});
22
assert.ok(true);
23
};
24
25
exports['test JSON serialization'] = function (assert, util) {
26
var map = new SourceMapGenerator({
27
file: 'foo.js',
28
sourceRoot: '.'
29
});
30
assert.equal(map.toString(), JSON.stringify(map));
31
};
32
33
exports['test adding mappings (case 1)'] = function (assert, util) {
34
var map = new SourceMapGenerator({
35
file: 'generated-foo.js',
36
sourceRoot: '.'
37
});
38
39
assert.doesNotThrow(function () {
40
map.addMapping({
41
generated: { line: 1, column: 1 }
42
});
43
});
44
};
45
46
exports['test adding mappings (case 2)'] = function (assert, util) {
47
var map = new SourceMapGenerator({
48
file: 'generated-foo.js',
49
sourceRoot: '.'
50
});
51
52
assert.doesNotThrow(function () {
53
map.addMapping({
54
generated: { line: 1, column: 1 },
55
source: 'bar.js',
56
original: { line: 1, column: 1 }
57
});
58
});
59
};
60
61
exports['test adding mappings (case 3)'] = function (assert, util) {
62
var map = new SourceMapGenerator({
63
file: 'generated-foo.js',
64
sourceRoot: '.'
65
});
66
67
assert.doesNotThrow(function () {
68
map.addMapping({
69
generated: { line: 1, column: 1 },
70
source: 'bar.js',
71
original: { line: 1, column: 1 },
72
name: 'someToken'
73
});
74
});
75
};
76
77
exports['test adding mappings (invalid)'] = function (assert, util) {
78
var map = new SourceMapGenerator({
79
file: 'generated-foo.js',
80
sourceRoot: '.'
81
});
82
83
// Not enough info.
84
assert.throws(function () {
85
map.addMapping({});
86
});
87
88
// Original file position, but no source.
89
assert.throws(function () {
90
map.addMapping({
91
generated: { line: 1, column: 1 },
92
original: { line: 1, column: 1 }
93
});
94
});
95
};
96
97
exports['test that the correct mappings are being generated'] = function (assert, util) {
98
var map = new SourceMapGenerator({
99
file: 'min.js',
100
sourceRoot: '/the/root'
101
});
102
103
map.addMapping({
104
generated: { line: 1, column: 1 },
105
original: { line: 1, column: 1 },
106
source: 'one.js'
107
});
108
map.addMapping({
109
generated: { line: 1, column: 5 },
110
original: { line: 1, column: 5 },
111
source: 'one.js'
112
});
113
map.addMapping({
114
generated: { line: 1, column: 9 },
115
original: { line: 1, column: 11 },
116
source: 'one.js'
117
});
118
map.addMapping({
119
generated: { line: 1, column: 18 },
120
original: { line: 1, column: 21 },
121
source: 'one.js',
122
name: 'bar'
123
});
124
map.addMapping({
125
generated: { line: 1, column: 21 },
126
original: { line: 2, column: 3 },
127
source: 'one.js'
128
});
129
map.addMapping({
130
generated: { line: 1, column: 28 },
131
original: { line: 2, column: 10 },
132
source: 'one.js',
133
name: 'baz'
134
});
135
map.addMapping({
136
generated: { line: 1, column: 32 },
137
original: { line: 2, column: 14 },
138
source: 'one.js',
139
name: 'bar'
140
});
141
142
map.addMapping({
143
generated: { line: 2, column: 1 },
144
original: { line: 1, column: 1 },
145
source: 'two.js'
146
});
147
map.addMapping({
148
generated: { line: 2, column: 5 },
149
original: { line: 1, column: 5 },
150
source: 'two.js'
151
});
152
map.addMapping({
153
generated: { line: 2, column: 9 },
154
original: { line: 1, column: 11 },
155
source: 'two.js'
156
});
157
map.addMapping({
158
generated: { line: 2, column: 18 },
159
original: { line: 1, column: 21 },
160
source: 'two.js',
161
name: 'n'
162
});
163
map.addMapping({
164
generated: { line: 2, column: 21 },
165
original: { line: 2, column: 3 },
166
source: 'two.js'
167
});
168
map.addMapping({
169
generated: { line: 2, column: 28 },
170
original: { line: 2, column: 10 },
171
source: 'two.js',
172
name: 'n'
173
});
174
175
map = JSON.parse(map.toString());
176
177
util.assertEqualMaps(assert, map, util.testMap);
178
};
179
180
exports['test that source content can be set'] = function (assert, util) {
181
var map = new SourceMapGenerator({
182
file: 'min.js',
183
sourceRoot: '/the/root'
184
});
185
map.addMapping({
186
generated: { line: 1, column: 1 },
187
original: { line: 1, column: 1 },
188
source: 'one.js'
189
});
190
map.addMapping({
191
generated: { line: 2, column: 1 },
192
original: { line: 1, column: 1 },
193
source: 'two.js'
194
});
195
map.setSourceContent('one.js', 'one file content');
196
197
map = JSON.parse(map.toString());
198
assert.equal(map.sources[0], 'one.js');
199
assert.equal(map.sources[1], 'two.js');
200
assert.equal(map.sourcesContent[0], 'one file content');
201
assert.equal(map.sourcesContent[1], null);
202
};
203
204
exports['test .fromSourceMap'] = function (assert, util) {
205
var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));
206
util.assertEqualMaps(assert, map.toJSON(), util.testMap);
207
};
208
209
exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {
210
var map = SourceMapGenerator.fromSourceMap(
211
new SourceMapConsumer(util.testMapWithSourcesContent));
212
util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);
213
};
214
215
exports['test applySourceMap'] = function (assert, util) {
216
var node = new SourceNode(null, null, null, [
217
new SourceNode(2, 0, 'fileX', 'lineX2\n'),
218
'genA1\n',
219
new SourceNode(2, 0, 'fileY', 'lineY2\n'),
220
'genA2\n',
221
new SourceNode(1, 0, 'fileX', 'lineX1\n'),
222
'genA3\n',
223
new SourceNode(1, 0, 'fileY', 'lineY1\n')
224
]);
225
var mapStep1 = node.toStringWithSourceMap({
226
file: 'fileA'
227
}).map;
228
mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');
229
mapStep1 = mapStep1.toJSON();
230
231
node = new SourceNode(null, null, null, [
232
'gen1\n',
233
new SourceNode(1, 0, 'fileA', 'lineA1\n'),
234
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
235
new SourceNode(3, 0, 'fileA', 'lineA3\n'),
236
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
237
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
238
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
239
'gen2\n'
240
]);
241
var mapStep2 = node.toStringWithSourceMap({
242
file: 'fileGen'
243
}).map;
244
mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');
245
mapStep2 = mapStep2.toJSON();
246
247
node = new SourceNode(null, null, null, [
248
'gen1\n',
249
new SourceNode(2, 0, 'fileX', 'lineA1\n'),
250
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
251
new SourceNode(2, 0, 'fileY', 'lineA3\n'),
252
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
253
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
254
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
255
'gen2\n'
256
]);
257
var expectedMap = node.toStringWithSourceMap({
258
file: 'fileGen'
259
}).map;
260
expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');
261
expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');
262
expectedMap = expectedMap.toJSON();
263
264
// apply source map "mapStep1" to "mapStep2"
265
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
266
generator.applySourceMap(new SourceMapConsumer(mapStep1));
267
var actualMap = generator.toJSON();
268
269
util.assertEqualMaps(assert, actualMap, expectedMap);
270
};
271
272
exports['test sorting with duplicate generated mappings'] = function (assert, util) {
273
var map = new SourceMapGenerator({
274
file: 'test.js'
275
});
276
map.addMapping({
277
generated: { line: 3, column: 0 },
278
original: { line: 2, column: 0 },
279
source: 'a.js'
280
});
281
map.addMapping({
282
generated: { line: 2, column: 0 }
283
});
284
map.addMapping({
285
generated: { line: 2, column: 0 }
286
});
287
map.addMapping({
288
generated: { line: 1, column: 0 },
289
original: { line: 1, column: 0 },
290
source: 'a.js'
291
});
292
293
util.assertEqualMaps(assert, map.toJSON(), {
294
version: 3,
295
file: 'test.js',
296
sources: ['a.js'],
297
names: [],
298
mappings: 'AAAA;A;AACA'
299
});
300
};
301
302
exports['test ignore duplicate mappings.'] = function (assert, util) {
303
var init = { file: 'min.js', sourceRoot: '/the/root' };
304
var map1, map2;
305
306
// null original source location
307
var nullMapping1 = {
308
generated: { line: 1, column: 0 }
309
};
310
var nullMapping2 = {
311
generated: { line: 2, column: 2 }
312
};
313
314
map1 = new SourceMapGenerator(init);
315
map2 = new SourceMapGenerator(init);
316
317
map1.addMapping(nullMapping1);
318
map1.addMapping(nullMapping1);
319
320
map2.addMapping(nullMapping1);
321
322
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
323
324
map1.addMapping(nullMapping2);
325
map1.addMapping(nullMapping1);
326
327
map2.addMapping(nullMapping2);
328
329
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
330
331
// original source location
332
var srcMapping1 = {
333
generated: { line: 1, column: 0 },
334
original: { line: 11, column: 0 },
335
source: 'srcMapping1.js'
336
};
337
var srcMapping2 = {
338
generated: { line: 2, column: 2 },
339
original: { line: 11, column: 0 },
340
source: 'srcMapping2.js'
341
};
342
343
map1 = new SourceMapGenerator(init);
344
map2 = new SourceMapGenerator(init);
345
346
map1.addMapping(srcMapping1);
347
map1.addMapping(srcMapping1);
348
349
map2.addMapping(srcMapping1);
350
351
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
352
353
map1.addMapping(srcMapping2);
354
map1.addMapping(srcMapping1);
355
356
map2.addMapping(srcMapping2);
357
358
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
359
360
// full original source and name information
361
var fullMapping1 = {
362
generated: { line: 1, column: 0 },
363
original: { line: 11, column: 0 },
364
source: 'fullMapping1.js',
365
name: 'fullMapping1'
366
};
367
var fullMapping2 = {
368
generated: { line: 2, column: 2 },
369
original: { line: 11, column: 0 },
370
source: 'fullMapping2.js',
371
name: 'fullMapping2'
372
};
373
374
map1 = new SourceMapGenerator(init);
375
map2 = new SourceMapGenerator(init);
376
377
map1.addMapping(fullMapping1);
378
map1.addMapping(fullMapping1);
379
380
map2.addMapping(fullMapping1);
381
382
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
383
384
map1.addMapping(fullMapping2);
385
map1.addMapping(fullMapping1);
386
387
map2.addMapping(fullMapping2);
388
389
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
390
};
391
392
exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {
393
var map = new SourceMapGenerator({
394
file: 'test.js'
395
});
396
map.addMapping({
397
generated: { line: 1, column: 1 },
398
original: { line: 2, column: 2 },
399
source: 'a.js',
400
name: 'foo'
401
});
402
map.addMapping({
403
generated: { line: 3, column: 3 },
404
original: { line: 4, column: 4 },
405
source: 'a.js',
406
name: 'foo'
407
});
408
util.assertEqualMaps(assert, map.toJSON(), {
409
version: 3,
410
file: 'test.js',
411
sources: ['a.js'],
412
names: ['foo'],
413
mappings: 'CACEA;;GAEEA'
414
});
415
};
416
417
});
418
419