Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/*
2
* WARNING!
3
*
4
* Do not edit this file directly, it is built from the sources at
5
* https://github.com/mozilla/source-map/
6
*/
7
8
Components.utils.import('resource://test/Utils.jsm');
9
/* -*- Mode: js; js-indent-level: 2; -*- */
10
/*
11
* Copyright 2011 Mozilla Foundation and contributors
12
* Licensed under the New BSD license. See LICENSE or:
13
* http://opensource.org/licenses/BSD-3-Clause
14
*/
15
define("test/source-map/test-source-map-generator", ["require", "exports", "module"], function (require, exports, module) {
16
17
var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
18
var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
19
var SourceNode = require('source-map/source-node').SourceNode;
20
var util = require('source-map/util');
21
22
exports['test some simple stuff'] = function (assert, util) {
23
var map = new SourceMapGenerator({
24
file: 'foo.js',
25
sourceRoot: '.'
26
});
27
assert.ok(true);
28
29
var map = new SourceMapGenerator().toJSON();
30
assert.ok(!('file' in map));
31
assert.ok(!('sourceRoot' in map));
32
};
33
34
exports['test JSON serialization'] = function (assert, util) {
35
var map = new SourceMapGenerator({
36
file: 'foo.js',
37
sourceRoot: '.'
38
});
39
assert.equal(map.toString(), JSON.stringify(map));
40
};
41
42
exports['test adding mappings (case 1)'] = function (assert, util) {
43
var map = new SourceMapGenerator({
44
file: 'generated-foo.js',
45
sourceRoot: '.'
46
});
47
48
assert.doesNotThrow(function () {
49
map.addMapping({
50
generated: { line: 1, column: 1 }
51
});
52
});
53
};
54
55
exports['test adding mappings (case 2)'] = function (assert, util) {
56
var map = new SourceMapGenerator({
57
file: 'generated-foo.js',
58
sourceRoot: '.'
59
});
60
61
assert.doesNotThrow(function () {
62
map.addMapping({
63
generated: { line: 1, column: 1 },
64
source: 'bar.js',
65
original: { line: 1, column: 1 }
66
});
67
});
68
};
69
70
exports['test adding mappings (case 3)'] = function (assert, util) {
71
var map = new SourceMapGenerator({
72
file: 'generated-foo.js',
73
sourceRoot: '.'
74
});
75
76
assert.doesNotThrow(function () {
77
map.addMapping({
78
generated: { line: 1, column: 1 },
79
source: 'bar.js',
80
original: { line: 1, column: 1 },
81
name: 'someToken'
82
});
83
});
84
};
85
86
exports['test adding mappings (invalid)'] = function (assert, util) {
87
var map = new SourceMapGenerator({
88
file: 'generated-foo.js',
89
sourceRoot: '.'
90
});
91
92
// Not enough info.
93
assert.throws(function () {
94
map.addMapping({});
95
});
96
97
// Original file position, but no source.
98
assert.throws(function () {
99
map.addMapping({
100
generated: { line: 1, column: 1 },
101
original: { line: 1, column: 1 }
102
});
103
});
104
};
105
106
exports['test adding mappings with skipValidation'] = function (assert, util) {
107
var map = new SourceMapGenerator({
108
file: 'generated-foo.js',
109
sourceRoot: '.',
110
skipValidation: true
111
});
112
113
// Not enough info, caught by `util.getArgs`
114
assert.throws(function () {
115
map.addMapping({});
116
});
117
118
// Original file position, but no source. Not checked.
119
assert.doesNotThrow(function () {
120
map.addMapping({
121
generated: { line: 1, column: 1 },
122
original: { line: 1, column: 1 }
123
});
124
});
125
};
126
127
exports['test that the correct mappings are being generated'] = function (assert, util) {
128
var map = new SourceMapGenerator({
129
file: 'min.js',
130
sourceRoot: '/the/root'
131
});
132
133
map.addMapping({
134
generated: { line: 1, column: 1 },
135
original: { line: 1, column: 1 },
136
source: 'one.js'
137
});
138
map.addMapping({
139
generated: { line: 1, column: 5 },
140
original: { line: 1, column: 5 },
141
source: 'one.js'
142
});
143
map.addMapping({
144
generated: { line: 1, column: 9 },
145
original: { line: 1, column: 11 },
146
source: 'one.js'
147
});
148
map.addMapping({
149
generated: { line: 1, column: 18 },
150
original: { line: 1, column: 21 },
151
source: 'one.js',
152
name: 'bar'
153
});
154
map.addMapping({
155
generated: { line: 1, column: 21 },
156
original: { line: 2, column: 3 },
157
source: 'one.js'
158
});
159
map.addMapping({
160
generated: { line: 1, column: 28 },
161
original: { line: 2, column: 10 },
162
source: 'one.js',
163
name: 'baz'
164
});
165
map.addMapping({
166
generated: { line: 1, column: 32 },
167
original: { line: 2, column: 14 },
168
source: 'one.js',
169
name: 'bar'
170
});
171
172
map.addMapping({
173
generated: { line: 2, column: 1 },
174
original: { line: 1, column: 1 },
175
source: 'two.js'
176
});
177
map.addMapping({
178
generated: { line: 2, column: 5 },
179
original: { line: 1, column: 5 },
180
source: 'two.js'
181
});
182
map.addMapping({
183
generated: { line: 2, column: 9 },
184
original: { line: 1, column: 11 },
185
source: 'two.js'
186
});
187
map.addMapping({
188
generated: { line: 2, column: 18 },
189
original: { line: 1, column: 21 },
190
source: 'two.js',
191
name: 'n'
192
});
193
map.addMapping({
194
generated: { line: 2, column: 21 },
195
original: { line: 2, column: 3 },
196
source: 'two.js'
197
});
198
map.addMapping({
199
generated: { line: 2, column: 28 },
200
original: { line: 2, column: 10 },
201
source: 'two.js',
202
name: 'n'
203
});
204
205
map = JSON.parse(map.toString());
206
207
util.assertEqualMaps(assert, map, util.testMap);
208
};
209
210
exports['test that adding a mapping with an empty string name does not break generation'] = function (assert, util) {
211
var map = new SourceMapGenerator({
212
file: 'generated-foo.js',
213
sourceRoot: '.'
214
});
215
216
map.addMapping({
217
generated: { line: 1, column: 1 },
218
source: 'bar.js',
219
original: { line: 1, column: 1 },
220
name: ''
221
});
222
223
assert.doesNotThrow(function () {
224
JSON.parse(map.toString());
225
});
226
};
227
228
exports['test that source content can be set'] = function (assert, util) {
229
var map = new SourceMapGenerator({
230
file: 'min.js',
231
sourceRoot: '/the/root'
232
});
233
map.addMapping({
234
generated: { line: 1, column: 1 },
235
original: { line: 1, column: 1 },
236
source: 'one.js'
237
});
238
map.addMapping({
239
generated: { line: 2, column: 1 },
240
original: { line: 1, column: 1 },
241
source: 'two.js'
242
});
243
map.setSourceContent('one.js', 'one file content');
244
245
map = JSON.parse(map.toString());
246
assert.equal(map.sources[0], 'one.js');
247
assert.equal(map.sources[1], 'two.js');
248
assert.equal(map.sourcesContent[0], 'one file content');
249
assert.equal(map.sourcesContent[1], null);
250
};
251
252
exports['test .fromSourceMap'] = function (assert, util) {
253
var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));
254
util.assertEqualMaps(assert, map.toJSON(), util.testMap);
255
};
256
257
exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {
258
var map = SourceMapGenerator.fromSourceMap(
259
new SourceMapConsumer(util.testMapWithSourcesContent));
260
util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);
261
};
262
263
exports['test applySourceMap'] = function (assert, util) {
264
var node = new SourceNode(null, null, null, [
265
new SourceNode(2, 0, 'fileX', 'lineX2\n'),
266
'genA1\n',
267
new SourceNode(2, 0, 'fileY', 'lineY2\n'),
268
'genA2\n',
269
new SourceNode(1, 0, 'fileX', 'lineX1\n'),
270
'genA3\n',
271
new SourceNode(1, 0, 'fileY', 'lineY1\n')
272
]);
273
var mapStep1 = node.toStringWithSourceMap({
274
file: 'fileA'
275
}).map;
276
mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');
277
mapStep1 = mapStep1.toJSON();
278
279
node = new SourceNode(null, null, null, [
280
'gen1\n',
281
new SourceNode(1, 0, 'fileA', 'lineA1\n'),
282
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
283
new SourceNode(3, 0, 'fileA', 'lineA3\n'),
284
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
285
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
286
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
287
'gen2\n'
288
]);
289
var mapStep2 = node.toStringWithSourceMap({
290
file: 'fileGen'
291
}).map;
292
mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');
293
mapStep2 = mapStep2.toJSON();
294
295
node = new SourceNode(null, null, null, [
296
'gen1\n',
297
new SourceNode(2, 0, 'fileX', 'lineA1\n'),
298
new SourceNode(2, 0, 'fileA', 'lineA2\n'),
299
new SourceNode(2, 0, 'fileY', 'lineA3\n'),
300
new SourceNode(4, 0, 'fileA', 'lineA4\n'),
301
new SourceNode(1, 0, 'fileB', 'lineB1\n'),
302
new SourceNode(2, 0, 'fileB', 'lineB2\n'),
303
'gen2\n'
304
]);
305
var expectedMap = node.toStringWithSourceMap({
306
file: 'fileGen'
307
}).map;
308
expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');
309
expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');
310
expectedMap = expectedMap.toJSON();
311
312
// apply source map "mapStep1" to "mapStep2"
313
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
314
generator.applySourceMap(new SourceMapConsumer(mapStep1));
315
var actualMap = generator.toJSON();
316
317
util.assertEqualMaps(assert, actualMap, expectedMap);
318
};
319
320
exports['test applySourceMap throws when file is missing'] = function (assert, util) {
321
var map = new SourceMapGenerator({
322
file: 'test.js'
323
});
324
var map2 = new SourceMapGenerator();
325
assert.throws(function() {
326
map.applySourceMap(new SourceMapConsumer(map2.toJSON()));
327
});
328
};
329
330
exports['test the two additional parameters of applySourceMap'] = function (assert, util) {
331
// Assume the following directory structure:
332
//
333
// http://foo.org/
334
// bar.coffee
335
// app/
336
// coffee/
337
// foo.coffee
338
// temp/
339
// bundle.js
340
// temp_maps/
341
// bundle.js.map
342
// public/
343
// bundle.min.js
344
// bundle.min.js.map
345
//
346
// http://www.example.com/
347
// baz.coffee
348
349
var bundleMap = new SourceMapGenerator({
350
file: 'bundle.js'
351
});
352
bundleMap.addMapping({
353
generated: { line: 3, column: 3 },
354
original: { line: 2, column: 2 },
355
source: '../../coffee/foo.coffee'
356
});
357
bundleMap.setSourceContent('../../coffee/foo.coffee', 'foo coffee');
358
bundleMap.addMapping({
359
generated: { line: 13, column: 13 },
360
original: { line: 12, column: 12 },
361
source: '/bar.coffee'
362
});
363
bundleMap.setSourceContent('/bar.coffee', 'bar coffee');
364
bundleMap.addMapping({
365
generated: { line: 23, column: 23 },
366
original: { line: 22, column: 22 },
367
source: 'http://www.example.com/baz.coffee'
368
});
369
bundleMap.setSourceContent(
370
'http://www.example.com/baz.coffee',
371
'baz coffee'
372
);
373
bundleMap = new SourceMapConsumer(bundleMap.toJSON());
374
375
var minifiedMap = new SourceMapGenerator({
376
file: 'bundle.min.js',
377
sourceRoot: '..'
378
});
379
minifiedMap.addMapping({
380
generated: { line: 1, column: 1 },
381
original: { line: 3, column: 3 },
382
source: 'temp/bundle.js'
383
});
384
minifiedMap.addMapping({
385
generated: { line: 11, column: 11 },
386
original: { line: 13, column: 13 },
387
source: 'temp/bundle.js'
388
});
389
minifiedMap.addMapping({
390
generated: { line: 21, column: 21 },
391
original: { line: 23, column: 23 },
392
source: 'temp/bundle.js'
393
});
394
minifiedMap = new SourceMapConsumer(minifiedMap.toJSON());
395
396
var expectedMap = function (sources) {
397
var map = new SourceMapGenerator({
398
file: 'bundle.min.js',
399
sourceRoot: '..'
400
});
401
map.addMapping({
402
generated: { line: 1, column: 1 },
403
original: { line: 2, column: 2 },
404
source: sources[0]
405
});
406
map.setSourceContent(sources[0], 'foo coffee');
407
map.addMapping({
408
generated: { line: 11, column: 11 },
409
original: { line: 12, column: 12 },
410
source: sources[1]
411
});
412
map.setSourceContent(sources[1], 'bar coffee');
413
map.addMapping({
414
generated: { line: 21, column: 21 },
415
original: { line: 22, column: 22 },
416
source: sources[2]
417
});
418
map.setSourceContent(sources[2], 'baz coffee');
419
return map.toJSON();
420
}
421
422
var actualMap = function (aSourceMapPath) {
423
var map = SourceMapGenerator.fromSourceMap(minifiedMap);
424
// Note that relying on `bundleMap.file` (which is simply 'bundle.js')
425
// instead of supplying the second parameter wouldn't work here.
426
map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath);
427
return map.toJSON();
428
}
429
430
util.assertEqualMaps(assert, actualMap('../temp/temp_maps'), expectedMap([
431
'coffee/foo.coffee',
432
'/bar.coffee',
433
'http://www.example.com/baz.coffee'
434
]));
435
436
util.assertEqualMaps(assert, actualMap('/app/temp/temp_maps'), expectedMap([
437
'/app/coffee/foo.coffee',
438
'/bar.coffee',
439
'http://www.example.com/baz.coffee'
440
]));
441
442
util.assertEqualMaps(assert, actualMap('http://foo.org/app/temp/temp_maps'), expectedMap([
443
'http://foo.org/app/coffee/foo.coffee',
444
'http://foo.org/bar.coffee',
445
'http://www.example.com/baz.coffee'
446
]));
447
448
// If the third parameter is omitted or set to the current working
449
// directory we get incorrect source paths:
450
451
util.assertEqualMaps(assert, actualMap(), expectedMap([
452
'../coffee/foo.coffee',
453
'/bar.coffee',
454
'http://www.example.com/baz.coffee'
455
]));
456
457
util.assertEqualMaps(assert, actualMap(''), expectedMap([
458
'../coffee/foo.coffee',
459
'/bar.coffee',
460
'http://www.example.com/baz.coffee'
461
]));
462
463
util.assertEqualMaps(assert, actualMap('.'), expectedMap([
464
'../coffee/foo.coffee',
465
'/bar.coffee',
466
'http://www.example.com/baz.coffee'
467
]));
468
469
util.assertEqualMaps(assert, actualMap('./'), expectedMap([
470
'../coffee/foo.coffee',
471
'/bar.coffee',
472
'http://www.example.com/baz.coffee'
473
]));
474
};
475
476
exports['test applySourceMap name handling'] = function (assert, util) {
477
// Imagine some CoffeeScript code being compiled into JavaScript and then
478
// minified.
479
480
var assertName = function(coffeeName, jsName, expectedName) {
481
var minifiedMap = new SourceMapGenerator({
482
file: 'test.js.min'
483
});
484
minifiedMap.addMapping({
485
generated: { line: 1, column: 4 },
486
original: { line: 1, column: 4 },
487
source: 'test.js',
488
name: jsName
489
});
490
491
var coffeeMap = new SourceMapGenerator({
492
file: 'test.js'
493
});
494
coffeeMap.addMapping({
495
generated: { line: 1, column: 4 },
496
original: { line: 1, column: 0 },
497
source: 'test.coffee',
498
name: coffeeName
499
});
500
501
minifiedMap.applySourceMap(new SourceMapConsumer(coffeeMap.toJSON()));
502
503
new SourceMapConsumer(minifiedMap.toJSON()).eachMapping(function(mapping) {
504
assert.equal(mapping.name, expectedName);
505
});
506
};
507
508
// `foo = 1` -> `var foo = 1;` -> `var a=1`
509
// CoffeeScript doesn’t rename variables, so there’s no need for it to
510
// provide names in its source maps. Minifiers do rename variables and
511
// therefore do provide names in their source maps. So that name should be
512
// retained if the original map lacks names.
513
assertName(null, 'foo', 'foo');
514
515
// `foo = 1` -> `var coffee$foo = 1;` -> `var a=1`
516
// Imagine that CoffeeScript prefixed all variables with `coffee$`. Even
517
// though the minifier then also provides a name, the original name is
518
// what corresponds to the source.
519
assertName('foo', 'coffee$foo', 'foo');
520
521
// `foo = 1` -> `var coffee$foo = 1;` -> `var coffee$foo=1`
522
// Minifiers can turn off variable mangling. Then there’s no need to
523
// provide names in the source map, but the names from the original map are
524
// still needed.
525
assertName('foo', null, 'foo');
526
527
// `foo = 1` -> `var foo = 1;` -> `var foo=1`
528
// No renaming at all.
529
assertName(null, null, null);
530
};
531
532
exports['test sorting with duplicate generated mappings'] = function (assert, util) {
533
var map = new SourceMapGenerator({
534
file: 'test.js'
535
});
536
map.addMapping({
537
generated: { line: 3, column: 0 },
538
original: { line: 2, column: 0 },
539
source: 'a.js'
540
});
541
map.addMapping({
542
generated: { line: 2, column: 0 }
543
});
544
map.addMapping({
545
generated: { line: 2, column: 0 }
546
});
547
map.addMapping({
548
generated: { line: 1, column: 0 },
549
original: { line: 1, column: 0 },
550
source: 'a.js'
551
});
552
553
util.assertEqualMaps(assert, map.toJSON(), {
554
version: 3,
555
file: 'test.js',
556
sources: ['a.js'],
557
names: [],
558
mappings: 'AAAA;A;AACA'
559
});
560
};
561
562
exports['test ignore duplicate mappings.'] = function (assert, util) {
563
var init = { file: 'min.js', sourceRoot: '/the/root' };
564
var map1, map2;
565
566
// null original source location
567
var nullMapping1 = {
568
generated: { line: 1, column: 0 }
569
};
570
var nullMapping2 = {
571
generated: { line: 2, column: 2 }
572
};
573
574
map1 = new SourceMapGenerator(init);
575
map2 = new SourceMapGenerator(init);
576
577
map1.addMapping(nullMapping1);
578
map1.addMapping(nullMapping1);
579
580
map2.addMapping(nullMapping1);
581
582
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
583
584
map1.addMapping(nullMapping2);
585
map1.addMapping(nullMapping1);
586
587
map2.addMapping(nullMapping2);
588
589
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
590
591
// original source location
592
var srcMapping1 = {
593
generated: { line: 1, column: 0 },
594
original: { line: 11, column: 0 },
595
source: 'srcMapping1.js'
596
};
597
var srcMapping2 = {
598
generated: { line: 2, column: 2 },
599
original: { line: 11, column: 0 },
600
source: 'srcMapping2.js'
601
};
602
603
map1 = new SourceMapGenerator(init);
604
map2 = new SourceMapGenerator(init);
605
606
map1.addMapping(srcMapping1);
607
map1.addMapping(srcMapping1);
608
609
map2.addMapping(srcMapping1);
610
611
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
612
613
map1.addMapping(srcMapping2);
614
map1.addMapping(srcMapping1);
615
616
map2.addMapping(srcMapping2);
617
618
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
619
620
// full original source and name information
621
var fullMapping1 = {
622
generated: { line: 1, column: 0 },
623
original: { line: 11, column: 0 },
624
source: 'fullMapping1.js',
625
name: 'fullMapping1'
626
};
627
var fullMapping2 = {
628
generated: { line: 2, column: 2 },
629
original: { line: 11, column: 0 },
630
source: 'fullMapping2.js',
631
name: 'fullMapping2'
632
};
633
634
map1 = new SourceMapGenerator(init);
635
map2 = new SourceMapGenerator(init);
636
637
map1.addMapping(fullMapping1);
638
map1.addMapping(fullMapping1);
639
640
map2.addMapping(fullMapping1);
641
642
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
643
644
map1.addMapping(fullMapping2);
645
map1.addMapping(fullMapping1);
646
647
map2.addMapping(fullMapping2);
648
649
util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
650
};
651
652
exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {
653
var map = new SourceMapGenerator({
654
file: 'test.js'
655
});
656
map.addMapping({
657
generated: { line: 1, column: 1 },
658
original: { line: 2, column: 2 },
659
source: 'a.js',
660
name: 'foo'
661
});
662
map.addMapping({
663
generated: { line: 3, column: 3 },
664
original: { line: 4, column: 4 },
665
source: 'a.js',
666
name: 'foo'
667
});
668
util.assertEqualMaps(assert, map.toJSON(), {
669
version: 3,
670
file: 'test.js',
671
sources: ['a.js'],
672
names: ['foo'],
673
mappings: 'CACEA;;GAEEA'
674
});
675
};
676
677
exports['test setting sourcesContent to null when already null'] = function (assert, util) {
678
var smg = new SourceMapGenerator({ file: "foo.js" });
679
assert.doesNotThrow(function() {
680
smg.setSourceContent("bar.js", null);
681
});
682
};
683
684
});
685
function run_test() {
686
runSourceMapTests('test/source-map/test-source-map-generator', do_throw);
687
}
688
689