Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80765 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
var map = new SourceMapGenerator();
25
assert.ok(true);
26
};
27
28
exports['test JSON serialization'] = function (assert, util) {
29
var map = new SourceMapGenerator({
30
file: 'foo.js',
31
sourceRoot: '.'
32
});
33
assert.equal(map.toString(), JSON.stringify(map));
34
};
35
36
exports['test adding mappings (case 1)'] = function (assert, util) {
37
var map = new SourceMapGenerator({
38
file: 'generated-foo.js',
39
sourceRoot: '.'
40
});
41
42
assert.doesNotThrow(function () {
43
map.addMapping({
44
generated: { line: 1, column: 1 }
45
});
46
});
47
};
48
49
exports['test adding mappings (case 2)'] = function (assert, util) {
50
var map = new SourceMapGenerator({
51
file: 'generated-foo.js',
52
sourceRoot: '.'
53
});
54
55
assert.doesNotThrow(function () {
56
map.addMapping({
57
generated: { line: 1, column: 1 },
58
source: 'bar.js',
59
original: { line: 1, column: 1 }
60
});
61
});
62
};
63
64
exports['test adding mappings (case 3)'] = function (assert, util) {
65
var map = new SourceMapGenerator({
66
file: 'generated-foo.js',
67
sourceRoot: '.'
68
});
69
70
assert.doesNotThrow(function () {
71
map.addMapping({
72
generated: { line: 1, column: 1 },
73
source: 'bar.js',
74
original: { line: 1, column: 1 },
75
name: 'someToken'
76
});
77
});
78
};
79
80
exports['test adding mappings (invalid)'] = function (assert, util) {
81
var map = new SourceMapGenerator({
82
file: 'generated-foo.js',
83
sourceRoot: '.'
84
});
85
86
// Not enough info.
87
assert.throws(function () {
88
map.addMapping({});
89
});
90
91
// Original file position, but no source.
92
assert.throws(function () {
93
map.addMapping({
94
generated: { line: 1, column: 1 },
95
original: { line: 1, column: 1 }
96
});
97
});
98
};
99
100
exports['test that the correct mappings are being generated'] = function (assert, util) {
101
var map = new SourceMapGenerator({
102
file: 'min.js',
103
sourceRoot: '/the/root'
104
});
105
106
map.addMapping({
107
generated: { line: 1, column: 1 },
108
original: { line: 1, column: 1 },
109
source: 'one.js'
110
});
111
map.addMapping({
112
generated: { line: 1, column: 5 },
113
original: { line: 1, column: 5 },
114
source: 'one.js'
115
});
116
map.addMapping({
117
generated: { line: 1, column: 9 },
118
original: { line: 1, column: 11 },
119
source: 'one.js'
120
});
121
map.addMapping({
122
generated: { line: 1, column: 18 },
123
original: { line: 1, column: 21 },
124
source: 'one.js',
125
name: 'bar'
126
});
127
map.addMapping({
128
generated: { line: 1, column: 21 },
129
original: { line: 2, column: 3 },
130
source: 'one.js'
131
});
132
map.addMapping({
133
generated: { line: 1, column: 28 },
134
original: { line: 2, column: 10 },
135
source: 'one.js',
136
name: 'baz'
137
});
138
map.addMapping({
139
generated: { line: 1, column: 32 },
140
original: { line: 2, column: 14 },
141
source: 'one.js',
142
name: 'bar'
143
});
144
145
map.addMapping({
146
generated: { line: 2, column: 1 },
147
original: { line: 1, column: 1 },
148
source: 'two.js'
149
});
150
map.addMapping({
151
generated: { line: 2, column: 5 },
152
original: { line: 1, column: 5 },
153
source: 'two.js'
154
});
155
map.addMapping({
156
generated: { line: 2, column: 9 },
157
original: { line: 1, column: 11 },
158
source: 'two.js'
159
});
160
map.addMapping({
161
generated: { line: 2, column: 18 },
162
original: { line: 1, column: 21 },
163
source: 'two.js',
164
name: 'n'
165
});
166
map.addMapping({
167
generated: { line: 2, column: 21 },
168
original: { line: 2, column: 3 },
169
source: 'two.js'
170
});
171
map.addMapping({
172
generated: { line: 2, column: 28 },
173
original: { line: 2, column: 10 },
174
source: 'two.js',
175
name: 'n'
176
});
177
178
map = JSON.parse(map.toString());
179
180
util.assertEqualMaps(assert, map, util.testMap);
181
};
182
183
exports['test that source content can be set'] = function (assert, util) {
184
var map = new SourceMapGenerator({
185
file: 'min.js',
186
sourceRoot: '/the/root'
187
});
188
map.addMapping({
189
generated: { line: 1, column: 1 },
190
original: { line: 1, column: 1 },
191
source: 'one.js'
192
});
193
map.addMapping({
194
generated: { line: 2, column: 1 },
195
original: { line: 1, column: 1 },
196
source: 'two.js'
197
});
198
map.setSourceContent('one.js', 'one file content');
199
200
map = JSON.parse(map.toString());
201
assert.equal(map.sources[0], 'one.js');
202
assert.equal(map.sources[1], 'two.js');
203
assert.equal(map.sourcesContent[0], 'one file content');
204
assert.equal(map.sourcesContent[1], null);
205
};
206
207
exports['test .fromSourceMap'] = function (assert, util) {
208
var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));
209
util.assertEqualMaps(assert, map.toJSON(), util.testMap);
210
};
211
212
exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {
213
var map = SourceMapGenerator.fromSourceMap(
214
new SourceMapConsumer(util.testMapWithSourcesContent));
215
util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);
216
};
217
218
exports['test applySourceMap'] = function (assert, util) {
219
var node = new SourceNode(null, null, null, [
220
new SourceNode(2, 0, 'fileX', 'lineX2\n'),
221
'genA1\n',
222
new SourceNode(2, 0, 'fileY', 'lineY2\n'),
223
'genA2\n',
224
new SourceNode(1, 0, 'fileX', 'lineX1\n'),
225
'genA3\n',
226
new SourceNode(1, 0, 'fileY', 'lineY1\n')
227
]);
228
var mapStep1 = node.toStringWithSourceMap({
229
file: 'fileA'
230
}).map;
231
mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');
232
mapStep1 = mapStep1.toJSON();
233
234
node = new SourceNode(null, null, null, [
235
'gen1\n',
236
new SourceNode(1, 0, 'fileA', 'lineA1\n'),
237
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
238
new SourceNode(3, 0, 'fileA', 'lineA3\n'),
239
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
240
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
241
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
242
'gen2\n'
243
]);
244
var mapStep2 = node.toStringWithSourceMap({
245
file: 'fileGen'
246
}).map;
247
mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');
248
mapStep2 = mapStep2.toJSON();
249
250
node = new SourceNode(null, null, null, [
251
'gen1\n',
252
new SourceNode(2, 0, 'fileX', 'lineA1\n'),
253
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
254
new SourceNode(2, 0, 'fileY', 'lineA3\n'),
255
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
256
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
257
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
258
'gen2\n'
259
]);
260
var expectedMap = node.toStringWithSourceMap({
261
file: 'fileGen'
262
}).map;
263
expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');
264
expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');
265
expectedMap = expectedMap.toJSON();
266
267
// apply source map "mapStep1" to "mapStep2"
268
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
269
generator.applySourceMap(new SourceMapConsumer(mapStep1));
270
var actualMap = generator.toJSON();
271
272
util.assertEqualMaps(assert, actualMap, expectedMap);
273
};
274
275
exports['test applySourceMap throws when file is missing'] = function (assert, util) {
276
var map = new SourceMapGenerator({
277
file: 'test.js'
278
});
279
var map2 = new SourceMapGenerator();
280
assert.throws(function() {
281
map.applySourceMap(new SourceMapConsumer(map2.toJSON()));
282
});
283
};
284
285
exports['test the two additional parameters of applySourceMap'] = function (assert, util) {
286
// Assume the following directory structure:
287
//
288
// http://foo.org/
289
// bar.coffee
290
// app/
291
// coffee/
292
// foo.coffee
293
// temp/
294
// bundle.js
295
// temp_maps/
296
// bundle.js.map
297
// public/
298
// bundle.min.js
299
// bundle.min.js.map
300
//
301
// http://www.example.com/
302
// baz.coffee
303
304
var bundleMap = new SourceMapGenerator({
305
file: 'bundle.js'
306
});
307
bundleMap.addMapping({
308
generated: { line: 3, column: 3 },
309
original: { line: 2, column: 2 },
310
source: '../coffee/foo.coffee'
311
});
312
bundleMap.setSourceContent('../coffee/foo.coffee', 'foo coffee');
313
bundleMap.addMapping({
314
generated: { line: 13, column: 13 },
315
original: { line: 12, column: 12 },
316
source: '/bar.coffee'
317
});
318
bundleMap.setSourceContent('/bar.coffee', 'bar coffee');
319
bundleMap.addMapping({
320
generated: { line: 23, column: 23 },
321
original: { line: 22, column: 22 },
322
source: 'http://www.example.com/baz.coffee'
323
});
324
bundleMap.setSourceContent(
325
'http://www.example.com/baz.coffee',
326
'baz coffee'
327
);
328
bundleMap = new SourceMapConsumer(bundleMap.toJSON());
329
330
var minifiedMap = new SourceMapGenerator({
331
file: 'bundle.min.js',
332
sourceRoot: '..'
333
});
334
minifiedMap.addMapping({
335
generated: { line: 1, column: 1 },
336
original: { line: 3, column: 3 },
337
source: 'temp/bundle.js'
338
});
339
minifiedMap.addMapping({
340
generated: { line: 11, column: 11 },
341
original: { line: 13, column: 13 },
342
source: 'temp/bundle.js'
343
});
344
minifiedMap.addMapping({
345
generated: { line: 21, column: 21 },
346
original: { line: 23, column: 23 },
347
source: 'temp/bundle.js'
348
});
349
minifiedMap = new SourceMapConsumer(minifiedMap.toJSON());
350
351
var expectedMap = function (sources) {
352
var map = new SourceMapGenerator({
353
file: 'bundle.min.js',
354
sourceRoot: '..'
355
});
356
map.addMapping({
357
generated: { line: 1, column: 1 },
358
original: { line: 2, column: 2 },
359
source: sources[0]
360
});
361
map.setSourceContent(sources[0], 'foo coffee');
362
map.addMapping({
363
generated: { line: 11, column: 11 },
364
original: { line: 12, column: 12 },
365
source: sources[1]
366
});
367
map.setSourceContent(sources[1], 'bar coffee');
368
map.addMapping({
369
generated: { line: 21, column: 21 },
370
original: { line: 22, column: 22 },
371
source: sources[2]
372
});
373
map.setSourceContent(sources[2], 'baz coffee');
374
return map.toJSON();
375
}
376
377
var actualMap = function (aSourceMapPath) {
378
var map = SourceMapGenerator.fromSourceMap(minifiedMap);
379
// Note that relying on `bundleMap.file` (which is simply 'bundle.js')
380
// instead of supplying the second parameter wouldn't work here.
381
map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath);
382
return map.toJSON();
383
}
384
385
util.assertEqualMaps(assert, actualMap('../temp_maps'), expectedMap([
386
'coffee/foo.coffee',
387
'/bar.coffee',
388
'http://www.example.com/baz.coffee'
389
]));
390
391
util.assertEqualMaps(assert, actualMap('/app/temp_maps'), expectedMap([
392
'/app/coffee/foo.coffee',
393
'/bar.coffee',
394
'http://www.example.com/baz.coffee'
395
]));
396
397
util.assertEqualMaps(assert, actualMap('http://foo.org/app/temp_maps'), expectedMap([
398
'http://foo.org/app/coffee/foo.coffee',
399
'http://foo.org/bar.coffee',
400
'http://www.example.com/baz.coffee'
401
]));
402
};
403
404
exports['test sorting with duplicate generated mappings'] = function (assert, util) {
405
var map = new SourceMapGenerator({
406
file: 'test.js'
407
});
408
map.addMapping({
409
generated: { line: 3, column: 0 },
410
original: { line: 2, column: 0 },
411
source: 'a.js'
412
});
413
map.addMapping({
414
generated: { line: 2, column: 0 }
415
});
416
map.addMapping({
417
generated: { line: 2, column: 0 }
418
});
419
map.addMapping({
420
generated: { line: 1, column: 0 },
421
original: { line: 1, column: 0 },
422
source: 'a.js'
423
});
424
425
util.assertEqualMaps(assert, map.toJSON(), {
426
version: 3,
427
file: 'test.js',
428
sources: ['a.js'],
429
names: [],
430
mappings: 'AAAA;A;AACA'
431
});
432
};
433
434
exports['test ignore duplicate mappings.'] = function (assert, util) {
435
var init = { file: 'min.js', sourceRoot: '/the/root' };
436
var map1, map2;
437
438
// null original source location
439
var nullMapping1 = {
440
generated: { line: 1, column: 0 }
441
};
442
var nullMapping2 = {
443
generated: { line: 2, column: 2 }
444
};
445
446
map1 = new SourceMapGenerator(init);
447
map2 = new SourceMapGenerator(init);
448
449
map1.addMapping(nullMapping1);
450
map1.addMapping(nullMapping1);
451
452
map2.addMapping(nullMapping1);
453
454
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
455
456
map1.addMapping(nullMapping2);
457
map1.addMapping(nullMapping1);
458
459
map2.addMapping(nullMapping2);
460
461
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
462
463
// original source location
464
var srcMapping1 = {
465
generated: { line: 1, column: 0 },
466
original: { line: 11, column: 0 },
467
source: 'srcMapping1.js'
468
};
469
var srcMapping2 = {
470
generated: { line: 2, column: 2 },
471
original: { line: 11, column: 0 },
472
source: 'srcMapping2.js'
473
};
474
475
map1 = new SourceMapGenerator(init);
476
map2 = new SourceMapGenerator(init);
477
478
map1.addMapping(srcMapping1);
479
map1.addMapping(srcMapping1);
480
481
map2.addMapping(srcMapping1);
482
483
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
484
485
map1.addMapping(srcMapping2);
486
map1.addMapping(srcMapping1);
487
488
map2.addMapping(srcMapping2);
489
490
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
491
492
// full original source and name information
493
var fullMapping1 = {
494
generated: { line: 1, column: 0 },
495
original: { line: 11, column: 0 },
496
source: 'fullMapping1.js',
497
name: 'fullMapping1'
498
};
499
var fullMapping2 = {
500
generated: { line: 2, column: 2 },
501
original: { line: 11, column: 0 },
502
source: 'fullMapping2.js',
503
name: 'fullMapping2'
504
};
505
506
map1 = new SourceMapGenerator(init);
507
map2 = new SourceMapGenerator(init);
508
509
map1.addMapping(fullMapping1);
510
map1.addMapping(fullMapping1);
511
512
map2.addMapping(fullMapping1);
513
514
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
515
516
map1.addMapping(fullMapping2);
517
map1.addMapping(fullMapping1);
518
519
map2.addMapping(fullMapping2);
520
521
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
522
};
523
524
exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {
525
var map = new SourceMapGenerator({
526
file: 'test.js'
527
});
528
map.addMapping({
529
generated: { line: 1, column: 1 },
530
original: { line: 2, column: 2 },
531
source: 'a.js',
532
name: 'foo'
533
});
534
map.addMapping({
535
generated: { line: 3, column: 3 },
536
original: { line: 4, column: 4 },
537
source: 'a.js',
538
name: 'foo'
539
});
540
util.assertEqualMaps(assert, map.toJSON(), {
541
version: 3,
542
file: 'test.js',
543
sources: ['a.js'],
544
names: ['foo'],
545
mappings: 'CACEA;;GAEEA'
546
});
547
};
548
549
});
550
551