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 SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
13
var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
14
15
exports['test that we can instantiate with a string or an object'] = function (assert, util) {
16
assert.doesNotThrow(function () {
17
var map = new SourceMapConsumer(util.testMap);
18
});
19
assert.doesNotThrow(function () {
20
var map = new SourceMapConsumer(JSON.stringify(util.testMap));
21
});
22
};
23
24
exports['test that the `sources` field has the original sources'] = function (assert, util) {
25
var map;
26
var sources;
27
28
map = new SourceMapConsumer(util.testMap);
29
sources = map.sources;
30
assert.equal(sources[0], '/the/root/one.js');
31
assert.equal(sources[1], '/the/root/two.js');
32
assert.equal(sources.length, 2);
33
34
map = new SourceMapConsumer(util.testMapNoSourceRoot);
35
sources = map.sources;
36
assert.equal(sources[0], 'one.js');
37
assert.equal(sources[1], 'two.js');
38
assert.equal(sources.length, 2);
39
40
map = new SourceMapConsumer(util.testMapEmptySourceRoot);
41
sources = map.sources;
42
assert.equal(sources[0], 'one.js');
43
assert.equal(sources[1], 'two.js');
44
assert.equal(sources.length, 2);
45
};
46
47
exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {
48
var map;
49
var mapping;
50
51
map = new SourceMapConsumer(util.testMap);
52
53
mapping = map.originalPositionFor({
54
line: 2,
55
column: 1
56
});
57
assert.equal(mapping.source, '/the/root/two.js');
58
59
mapping = map.originalPositionFor({
60
line: 1,
61
column: 1
62
});
63
assert.equal(mapping.source, '/the/root/one.js');
64
65
66
map = new SourceMapConsumer(util.testMapNoSourceRoot);
67
68
mapping = map.originalPositionFor({
69
line: 2,
70
column: 1
71
});
72
assert.equal(mapping.source, 'two.js');
73
74
mapping = map.originalPositionFor({
75
line: 1,
76
column: 1
77
});
78
assert.equal(mapping.source, 'one.js');
79
80
81
map = new SourceMapConsumer(util.testMapEmptySourceRoot);
82
83
mapping = map.originalPositionFor({
84
line: 2,
85
column: 1
86
});
87
assert.equal(mapping.source, 'two.js');
88
89
mapping = map.originalPositionFor({
90
line: 1,
91
column: 1
92
});
93
assert.equal(mapping.source, 'one.js');
94
};
95
96
exports['test mapping tokens back exactly'] = function (assert, util) {
97
var map = new SourceMapConsumer(util.testMap);
98
99
util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
100
util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
101
util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
102
util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
103
util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
104
util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
105
util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
106
107
util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
108
util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
109
util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
110
util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
111
util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
112
util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
113
};
114
115
exports['test mapping tokens fuzzy'] = function (assert, util) {
116
var map = new SourceMapConsumer(util.testMap);
117
118
// Finding original positions
119
util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);
120
util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);
121
util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);
122
123
// Finding generated positions
124
util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);
125
util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);
126
util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);
127
};
128
129
exports['test mappings and end of lines'] = function (assert, util) {
130
var smg = new SourceMapGenerator({
131
file: 'foo.js'
132
});
133
smg.addMapping({
134
original: { line: 1, column: 1 },
135
generated: { line: 1, column: 1 },
136
source: 'bar.js'
137
});
138
smg.addMapping({
139
original: { line: 2, column: 2 },
140
generated: { line: 2, column: 2 },
141
source: 'bar.js'
142
});
143
144
var map = SourceMapConsumer.fromSourceMap(smg);
145
146
// When finding original positions, mappings end at the end of the line.
147
util.assertMapping(2, 1, null, null, null, null, map, assert, true)
148
149
// When finding generated positions, mappings do not end at the end of the line.
150
util.assertMapping(1, 1, 'bar.js', 2, 1, null, map, assert, null, true);
151
};
152
153
exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {
154
assert.doesNotThrow(function () {
155
var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));
156
});
157
};
158
159
exports['test eachMapping'] = function (assert, util) {
160
var map;
161
162
map = new SourceMapConsumer(util.testMap);
163
var previousLine = -Infinity;
164
var previousColumn = -Infinity;
165
map.eachMapping(function (mapping) {
166
assert.ok(mapping.generatedLine >= previousLine);
167
168
assert.ok(mapping.source === '/the/root/one.js' || mapping.source === '/the/root/two.js');
169
170
if (mapping.generatedLine === previousLine) {
171
assert.ok(mapping.generatedColumn >= previousColumn);
172
previousColumn = mapping.generatedColumn;
173
}
174
else {
175
previousLine = mapping.generatedLine;
176
previousColumn = -Infinity;
177
}
178
});
179
180
map = new SourceMapConsumer(util.testMapNoSourceRoot);
181
map.eachMapping(function (mapping) {
182
assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
183
});
184
185
map = new SourceMapConsumer(util.testMapEmptySourceRoot);
186
map.eachMapping(function (mapping) {
187
assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
188
});
189
};
190
191
exports['test iterating over mappings in a different order'] = function (assert, util) {
192
var map = new SourceMapConsumer(util.testMap);
193
var previousLine = -Infinity;
194
var previousColumn = -Infinity;
195
var previousSource = "";
196
map.eachMapping(function (mapping) {
197
assert.ok(mapping.source >= previousSource);
198
199
if (mapping.source === previousSource) {
200
assert.ok(mapping.originalLine >= previousLine);
201
202
if (mapping.originalLine === previousLine) {
203
assert.ok(mapping.originalColumn >= previousColumn);
204
previousColumn = mapping.originalColumn;
205
}
206
else {
207
previousLine = mapping.originalLine;
208
previousColumn = -Infinity;
209
}
210
}
211
else {
212
previousSource = mapping.source;
213
previousLine = -Infinity;
214
previousColumn = -Infinity;
215
}
216
}, null, SourceMapConsumer.ORIGINAL_ORDER);
217
};
218
219
exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {
220
var map = new SourceMapConsumer(util.testMap);
221
var context = {};
222
map.eachMapping(function () {
223
assert.equal(this, context);
224
}, context);
225
};
226
227
exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {
228
var map = new SourceMapConsumer(util.testMapWithSourcesContent);
229
var sourcesContent = map.sourcesContent;
230
231
assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');
232
assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');
233
assert.equal(sourcesContent.length, 2);
234
};
235
236
exports['test that we can get the original sources for the sources'] = function (assert, util) {
237
var map = new SourceMapConsumer(util.testMapWithSourcesContent);
238
var sources = map.sources;
239
240
assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
241
assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');
242
assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
243
assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');
244
assert.throws(function () {
245
map.sourceContentFor("");
246
}, Error);
247
assert.throws(function () {
248
map.sourceContentFor("/the/root/three.js");
249
}, Error);
250
assert.throws(function () {
251
map.sourceContentFor("three.js");
252
}, Error);
253
};
254
255
exports['test that we can get the original source content with relative source paths'] = function (assert, util) {
256
var map = new SourceMapConsumer(util.testMapRelativeSources);
257
var sources = map.sources;
258
259
assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
260
assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');
261
assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
262
assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');
263
assert.throws(function () {
264
map.sourceContentFor("");
265
}, Error);
266
assert.throws(function () {
267
map.sourceContentFor("/the/root/three.js");
268
}, Error);
269
assert.throws(function () {
270
map.sourceContentFor("three.js");
271
}, Error);
272
};
273
274
exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {
275
var map = new SourceMapGenerator({
276
sourceRoot: 'foo/bar',
277
file: 'baz.js'
278
});
279
map.addMapping({
280
original: { line: 1, column: 1 },
281
generated: { line: 2, column: 2 },
282
source: 'bang.coffee'
283
});
284
map.addMapping({
285
original: { line: 5, column: 5 },
286
generated: { line: 6, column: 6 },
287
source: 'bang.coffee'
288
});
289
map = new SourceMapConsumer(map.toString());
290
291
// Should handle without sourceRoot.
292
var pos = map.generatedPositionFor({
293
line: 1,
294
column: 1,
295
source: 'bang.coffee'
296
});
297
298
assert.equal(pos.line, 2);
299
assert.equal(pos.column, 2);
300
301
// Should handle with sourceRoot.
302
var pos = map.generatedPositionFor({
303
line: 1,
304
column: 1,
305
source: 'foo/bar/bang.coffee'
306
});
307
308
assert.equal(pos.line, 2);
309
assert.equal(pos.column, 2);
310
};
311
312
exports['test allGeneratedPositionsFor'] = function (assert, util) {
313
var map = new SourceMapGenerator({
314
file: 'generated.js'
315
});
316
map.addMapping({
317
original: { line: 1, column: 1 },
318
generated: { line: 2, column: 2 },
319
source: 'foo.coffee'
320
});
321
map.addMapping({
322
original: { line: 1, column: 1 },
323
generated: { line: 2, column: 2 },
324
source: 'bar.coffee'
325
});
326
map.addMapping({
327
original: { line: 2, column: 1 },
328
generated: { line: 3, column: 2 },
329
source: 'bar.coffee'
330
});
331
map.addMapping({
332
original: { line: 2, column: 2 },
333
generated: { line: 3, column: 3 },
334
source: 'bar.coffee'
335
});
336
map.addMapping({
337
original: { line: 3, column: 1 },
338
generated: { line: 4, column: 2 },
339
source: 'bar.coffee'
340
});
341
map = new SourceMapConsumer(map.toString());
342
343
var mappings = map.allGeneratedPositionsFor({
344
line: 2,
345
source: 'bar.coffee'
346
});
347
348
assert.equal(mappings.length, 2);
349
assert.equal(mappings[0].line, 3);
350
assert.equal(mappings[0].column, 2);
351
assert.equal(mappings[1].line, 3);
352
assert.equal(mappings[1].column, 3);
353
};
354
355
exports['test allGeneratedPositionsFor for line with no mappings'] = function (assert, util) {
356
var map = new SourceMapGenerator({
357
file: 'generated.js'
358
});
359
map.addMapping({
360
original: { line: 1, column: 1 },
361
generated: { line: 2, column: 2 },
362
source: 'foo.coffee'
363
});
364
map.addMapping({
365
original: { line: 1, column: 1 },
366
generated: { line: 2, column: 2 },
367
source: 'bar.coffee'
368
});
369
map.addMapping({
370
original: { line: 3, column: 1 },
371
generated: { line: 4, column: 2 },
372
source: 'bar.coffee'
373
});
374
map = new SourceMapConsumer(map.toString());
375
376
var mappings = map.allGeneratedPositionsFor({
377
line: 2,
378
source: 'bar.coffee'
379
});
380
381
assert.equal(mappings.length, 0);
382
};
383
384
exports['test allGeneratedPositionsFor source map with no mappings'] = function (assert, util) {
385
var map = new SourceMapGenerator({
386
file: 'generated.js'
387
});
388
map = new SourceMapConsumer(map.toString());
389
390
var mappings = map.allGeneratedPositionsFor({
391
line: 2,
392
source: 'bar.coffee'
393
});
394
395
assert.equal(mappings.length, 0);
396
};
397
398
exports['test computeColumnSpans'] = function (assert, util) {
399
var map = new SourceMapGenerator({
400
file: 'generated.js'
401
});
402
map.addMapping({
403
original: { line: 1, column: 1 },
404
generated: { line: 1, column: 1 },
405
source: 'foo.coffee'
406
});
407
map.addMapping({
408
original: { line: 2, column: 1 },
409
generated: { line: 2, column: 1 },
410
source: 'foo.coffee'
411
});
412
map.addMapping({
413
original: { line: 2, column: 2 },
414
generated: { line: 2, column: 10 },
415
source: 'foo.coffee'
416
});
417
map.addMapping({
418
original: { line: 2, column: 3 },
419
generated: { line: 2, column: 20 },
420
source: 'foo.coffee'
421
});
422
map.addMapping({
423
original: { line: 3, column: 1 },
424
generated: { line: 3, column: 1 },
425
source: 'foo.coffee'
426
});
427
map.addMapping({
428
original: { line: 3, column: 2 },
429
generated: { line: 3, column: 2 },
430
source: 'foo.coffee'
431
});
432
map = new SourceMapConsumer(map.toString());
433
434
map.computeColumnSpans();
435
436
var mappings = map.allGeneratedPositionsFor({
437
line: 1,
438
source: 'foo.coffee'
439
});
440
441
assert.equal(mappings.length, 1);
442
assert.equal(mappings[0].lastColumn, Infinity);
443
444
var mappings = map.allGeneratedPositionsFor({
445
line: 2,
446
source: 'foo.coffee'
447
});
448
449
assert.equal(mappings.length, 3);
450
assert.equal(mappings[0].lastColumn, 9);
451
assert.equal(mappings[1].lastColumn, 19);
452
assert.equal(mappings[2].lastColumn, Infinity);
453
454
var mappings = map.allGeneratedPositionsFor({
455
line: 3,
456
source: 'foo.coffee'
457
});
458
459
assert.equal(mappings.length, 2);
460
assert.equal(mappings[0].lastColumn, 1);
461
assert.equal(mappings[1].lastColumn, Infinity);
462
};
463
464
exports['test sourceRoot + originalPositionFor'] = function (assert, util) {
465
var map = new SourceMapGenerator({
466
sourceRoot: 'foo/bar',
467
file: 'baz.js'
468
});
469
map.addMapping({
470
original: { line: 1, column: 1 },
471
generated: { line: 2, column: 2 },
472
source: 'bang.coffee'
473
});
474
map = new SourceMapConsumer(map.toString());
475
476
var pos = map.originalPositionFor({
477
line: 2,
478
column: 2,
479
});
480
481
// Should always have the prepended source root
482
assert.equal(pos.source, 'foo/bar/bang.coffee');
483
assert.equal(pos.line, 1);
484
assert.equal(pos.column, 1);
485
};
486
487
exports['test github issue #56'] = function (assert, util) {
488
var map = new SourceMapGenerator({
489
sourceRoot: 'http://',
490
file: 'www.example.com/foo.js'
491
});
492
map.addMapping({
493
original: { line: 1, column: 1 },
494
generated: { line: 2, column: 2 },
495
source: 'www.example.com/original.js'
496
});
497
map = new SourceMapConsumer(map.toString());
498
499
var sources = map.sources;
500
assert.equal(sources.length, 1);
501
assert.equal(sources[0], 'http://www.example.com/original.js');
502
};
503
504
exports['test github issue #43'] = function (assert, util) {
505
var map = new SourceMapGenerator({
506
sourceRoot: 'http://example.com',
507
file: 'foo.js'
508
});
509
map.addMapping({
510
original: { line: 1, column: 1 },
511
generated: { line: 2, column: 2 },
512
source: 'http://cdn.example.com/original.js'
513
});
514
map = new SourceMapConsumer(map.toString());
515
516
var sources = map.sources;
517
assert.equal(sources.length, 1,
518
'Should only be one source.');
519
assert.equal(sources[0], 'http://cdn.example.com/original.js',
520
'Should not be joined with the sourceRoot.');
521
};
522
523
exports['test absolute path, but same host sources'] = function (assert, util) {
524
var map = new SourceMapGenerator({
525
sourceRoot: 'http://example.com/foo/bar',
526
file: 'foo.js'
527
});
528
map.addMapping({
529
original: { line: 1, column: 1 },
530
generated: { line: 2, column: 2 },
531
source: '/original.js'
532
});
533
map = new SourceMapConsumer(map.toString());
534
535
var sources = map.sources;
536
assert.equal(sources.length, 1,
537
'Should only be one source.');
538
assert.equal(sources[0], 'http://example.com/original.js',
539
'Source should be relative the host of the source root.');
540
};
541
542
exports['test github issue #64'] = function (assert, util) {
543
var map = new SourceMapConsumer({
544
"version": 3,
545
"file": "foo.js",
546
"sourceRoot": "http://example.com/",
547
"sources": ["/a"],
548
"names": [],
549
"mappings": "AACA",
550
"sourcesContent": ["foo"]
551
});
552
553
assert.equal(map.sourceContentFor("a"), "foo");
554
assert.equal(map.sourceContentFor("/a"), "foo");
555
};
556
557
exports['test bug 885597'] = function (assert, util) {
558
var map = new SourceMapConsumer({
559
"version": 3,
560
"file": "foo.js",
561
"sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",
562
"sources": ["/a"],
563
"names": [],
564
"mappings": "AACA",
565
"sourcesContent": ["foo"]
566
});
567
568
var s = map.sources[0];
569
assert.equal(map.sourceContentFor(s), "foo");
570
};
571
572
exports['test github issue #72, duplicate sources'] = function (assert, util) {
573
var map = new SourceMapConsumer({
574
"version": 3,
575
"file": "foo.js",
576
"sources": ["source1.js", "source1.js", "source3.js"],
577
"names": [],
578
"mappings": ";EAAC;;IAEE;;MEEE",
579
"sourceRoot": "http://example.com"
580
});
581
582
var pos = map.originalPositionFor({
583
line: 2,
584
column: 2
585
});
586
assert.equal(pos.source, 'http://example.com/source1.js');
587
assert.equal(pos.line, 1);
588
assert.equal(pos.column, 1);
589
590
var pos = map.originalPositionFor({
591
line: 4,
592
column: 4
593
});
594
assert.equal(pos.source, 'http://example.com/source1.js');
595
assert.equal(pos.line, 3);
596
assert.equal(pos.column, 3);
597
598
var pos = map.originalPositionFor({
599
line: 6,
600
column: 6
601
});
602
assert.equal(pos.source, 'http://example.com/source3.js');
603
assert.equal(pos.line, 5);
604
assert.equal(pos.column, 5);
605
};
606
607
exports['test github issue #72, duplicate names'] = function (assert, util) {
608
var map = new SourceMapConsumer({
609
"version": 3,
610
"file": "foo.js",
611
"sources": ["source.js"],
612
"names": ["name1", "name1", "name3"],
613
"mappings": ";EAACA;;IAEEA;;MAEEE",
614
"sourceRoot": "http://example.com"
615
});
616
617
var pos = map.originalPositionFor({
618
line: 2,
619
column: 2
620
});
621
assert.equal(pos.name, 'name1');
622
assert.equal(pos.line, 1);
623
assert.equal(pos.column, 1);
624
625
var pos = map.originalPositionFor({
626
line: 4,
627
column: 4
628
});
629
assert.equal(pos.name, 'name1');
630
assert.equal(pos.line, 3);
631
assert.equal(pos.column, 3);
632
633
var pos = map.originalPositionFor({
634
line: 6,
635
column: 6
636
});
637
assert.equal(pos.name, 'name3');
638
assert.equal(pos.line, 5);
639
assert.equal(pos.column, 5);
640
};
641
642
exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {
643
var smg = new SourceMapGenerator({
644
sourceRoot: 'http://example.com/',
645
file: 'foo.js'
646
});
647
smg.addMapping({
648
original: { line: 1, column: 1 },
649
generated: { line: 2, column: 2 },
650
source: 'bar.js'
651
});
652
smg.addMapping({
653
original: { line: 2, column: 2 },
654
generated: { line: 4, column: 4 },
655
source: 'baz.js',
656
name: 'dirtMcGirt'
657
});
658
smg.setSourceContent('baz.js', 'baz.js content');
659
660
var smc = SourceMapConsumer.fromSourceMap(smg);
661
assert.equal(smc.file, 'foo.js');
662
assert.equal(smc.sourceRoot, 'http://example.com/');
663
assert.equal(smc.sources.length, 2);
664
assert.equal(smc.sources[0], 'http://example.com/bar.js');
665
assert.equal(smc.sources[1], 'http://example.com/baz.js');
666
assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');
667
668
var pos = smc.originalPositionFor({
669
line: 2,
670
column: 2
671
});
672
assert.equal(pos.line, 1);
673
assert.equal(pos.column, 1);
674
assert.equal(pos.source, 'http://example.com/bar.js');
675
assert.equal(pos.name, null);
676
677
pos = smc.generatedPositionFor({
678
line: 1,
679
column: 1,
680
source: 'http://example.com/bar.js'
681
});
682
assert.equal(pos.line, 2);
683
assert.equal(pos.column, 2);
684
685
pos = smc.originalPositionFor({
686
line: 4,
687
column: 4
688
});
689
assert.equal(pos.line, 2);
690
assert.equal(pos.column, 2);
691
assert.equal(pos.source, 'http://example.com/baz.js');
692
assert.equal(pos.name, 'dirtMcGirt');
693
694
pos = smc.generatedPositionFor({
695
line: 2,
696
column: 2,
697
source: 'http://example.com/baz.js'
698
});
699
assert.equal(pos.line, 4);
700
assert.equal(pos.column, 4);
701
};
702
});
703
704