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