1 /* -*- Mode: js; js-indent-level: 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
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
10 define(function (require, exports, module) {
12 var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
13 var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
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);
19 assert.doesNotThrow(function () {
20 var map = new SourceMapConsumer(JSON.stringify(util.testMap));
24 exports['test that the `sources` field has the original sources'] = function (assert, util) {
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);
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);
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);
47 exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {
51 map = new SourceMapConsumer(util.testMap);
53 mapping = map.originalPositionFor({
57 assert.equal(mapping.source, '/the/root/two.js');
59 mapping = map.originalPositionFor({
63 assert.equal(mapping.source, '/the/root/one.js');
66 map = new SourceMapConsumer(util.testMapNoSourceRoot);
68 mapping = map.originalPositionFor({
72 assert.equal(mapping.source, 'two.js');
74 mapping = map.originalPositionFor({
78 assert.equal(mapping.source, 'one.js');
81 map = new SourceMapConsumer(util.testMapEmptySourceRoot);
83 mapping = map.originalPositionFor({
87 assert.equal(mapping.source, 'two.js');
89 mapping = map.originalPositionFor({
93 assert.equal(mapping.source, 'one.js');
96 exports['test mapping tokens back exactly'] = function (assert, util) {
97 var map = new SourceMapConsumer(util.testMap);
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);
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);
115 exports['test mapping tokens fuzzy'] = function (assert, util) {
116 var map = new SourceMapConsumer(util.testMap);
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);
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);
129 exports['test mappings and end of lines'] = function (assert, util) {
130 var smg = new SourceMapGenerator({
134 original: { line: 1, column: 1 },
135 generated: { line: 1, column: 1 },
139 original: { line: 2, column: 2 },
140 generated: { line: 2, column: 2 },
144 var map = SourceMapConsumer.fromSourceMap(smg);
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)
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);
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));
159 exports['test eachMapping'] = function (assert, util) {
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);
168 assert.ok(mapping.source === '/the/root/one.js' || mapping.source === '/the/root/two.js');
170 if (mapping.generatedLine === previousLine) {
171 assert.ok(mapping.generatedColumn >= previousColumn);
172 previousColumn = mapping.generatedColumn;
175 previousLine = mapping.generatedLine;
176 previousColumn = -Infinity;
180 map = new SourceMapConsumer(util.testMapNoSourceRoot);
181 map.eachMapping(function (mapping) {
182 assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
185 map = new SourceMapConsumer(util.testMapEmptySourceRoot);
186 map.eachMapping(function (mapping) {
187 assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
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);
199 if (mapping.source === previousSource) {
200 assert.ok(mapping.originalLine >= previousLine);
202 if (mapping.originalLine === previousLine) {
203 assert.ok(mapping.originalColumn >= previousColumn);
204 previousColumn = mapping.originalColumn;
207 previousLine = mapping.originalLine;
208 previousColumn = -Infinity;
212 previousSource = mapping.source;
213 previousLine = -Infinity;
214 previousColumn = -Infinity;
216 }, null, SourceMapConsumer.ORIGINAL_ORDER);
219 exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {
220 var map = new SourceMapConsumer(util.testMap);
222 map.eachMapping(function () {
223 assert.equal(this, context);
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;
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);
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;
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("");
247 assert.throws(function () {
248 map.sourceContentFor("/the/root/three.js");
250 assert.throws(function () {
251 map.sourceContentFor("three.js");
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;
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("");
266 assert.throws(function () {
267 map.sourceContentFor("/the/root/three.js");
269 assert.throws(function () {
270 map.sourceContentFor("three.js");
274 exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {
275 var map = new SourceMapGenerator({
276 sourceRoot: 'foo/bar',
280 original: { line: 1, column: 1 },
281 generated: { line: 2, column: 2 },
282 source: 'bang.coffee'
285 original: { line: 5, column: 5 },
286 generated: { line: 6, column: 6 },
287 source: 'bang.coffee'
289 map = new SourceMapConsumer(map.toString());
291 // Should handle without sourceRoot.
292 var pos = map.generatedPositionFor({
295 source: 'bang.coffee'
298 assert.equal(pos.line, 2);
299 assert.equal(pos.column, 2);
301 // Should handle with sourceRoot.
302 var pos = map.generatedPositionFor({
305 source: 'foo/bar/bang.coffee'
308 assert.equal(pos.line, 2);
309 assert.equal(pos.column, 2);
312 exports['test allGeneratedPositionsFor'] = function (assert, util) {
313 var map = new SourceMapGenerator({
317 original: { line: 1, column: 1 },
318 generated: { line: 2, column: 2 },
322 original: { line: 1, column: 1 },
323 generated: { line: 2, column: 2 },
327 original: { line: 2, column: 1 },
328 generated: { line: 3, column: 2 },
332 original: { line: 2, column: 2 },
333 generated: { line: 3, column: 3 },
337 original: { line: 3, column: 1 },
338 generated: { line: 4, column: 2 },
341 map = new SourceMapConsumer(map.toString());
343 var mappings = map.allGeneratedPositionsFor({
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);
355 exports['test allGeneratedPositionsFor for line with no mappings'] = function (assert, util) {
356 var map = new SourceMapGenerator({
360 original: { line: 1, column: 1 },
361 generated: { line: 2, column: 2 },
365 original: { line: 1, column: 1 },
366 generated: { line: 2, column: 2 },
370 original: { line: 3, column: 1 },
371 generated: { line: 4, column: 2 },
374 map = new SourceMapConsumer(map.toString());
376 var mappings = map.allGeneratedPositionsFor({
381 assert.equal(mappings.length, 0);
384 exports['test allGeneratedPositionsFor source map with no mappings'] = function (assert, util) {
385 var map = new SourceMapGenerator({
388 map = new SourceMapConsumer(map.toString());
390 var mappings = map.allGeneratedPositionsFor({
395 assert.equal(mappings.length, 0);
398 exports['test computeColumnSpans'] = function (assert, util) {
399 var map = new SourceMapGenerator({
403 original: { line: 1, column: 1 },
404 generated: { line: 1, column: 1 },
408 original: { line: 2, column: 1 },
409 generated: { line: 2, column: 1 },
413 original: { line: 2, column: 2 },
414 generated: { line: 2, column: 10 },
418 original: { line: 2, column: 3 },
419 generated: { line: 2, column: 20 },
423 original: { line: 3, column: 1 },
424 generated: { line: 3, column: 1 },
428 original: { line: 3, column: 2 },
429 generated: { line: 3, column: 2 },
432 map = new SourceMapConsumer(map.toString());
434 map.computeColumnSpans();
436 var mappings = map.allGeneratedPositionsFor({
441 assert.equal(mappings.length, 1);
442 assert.equal(mappings[0].lastColumn, Infinity);
444 var mappings = map.allGeneratedPositionsFor({
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);
454 var mappings = map.allGeneratedPositionsFor({
459 assert.equal(mappings.length, 2);
460 assert.equal(mappings[0].lastColumn, 1);
461 assert.equal(mappings[1].lastColumn, Infinity);
464 exports['test sourceRoot + originalPositionFor'] = function (assert, util) {
465 var map = new SourceMapGenerator({
466 sourceRoot: 'foo/bar',
470 original: { line: 1, column: 1 },
471 generated: { line: 2, column: 2 },
472 source: 'bang.coffee'
474 map = new SourceMapConsumer(map.toString());
476 var pos = map.originalPositionFor({
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);
487 exports['test github issue #56'] = function (assert, util) {
488 var map = new SourceMapGenerator({
489 sourceRoot: 'http://',
490 file: 'www.example.com/foo.js'
493 original: { line: 1, column: 1 },
494 generated: { line: 2, column: 2 },
495 source: 'www.example.com/original.js'
497 map = new SourceMapConsumer(map.toString());
499 var sources = map.sources;
500 assert.equal(sources.length, 1);
501 assert.equal(sources[0], 'http://www.example.com/original.js');
504 exports['test github issue #43'] = function (assert, util) {
505 var map = new SourceMapGenerator({
506 sourceRoot: 'http://example.com',
510 original: { line: 1, column: 1 },
511 generated: { line: 2, column: 2 },
512 source: 'http://cdn.example.com/original.js'
514 map = new SourceMapConsumer(map.toString());
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.');
523 exports['test absolute path, but same host sources'] = function (assert, util) {
524 var map = new SourceMapGenerator({
525 sourceRoot: 'http://example.com/foo/bar',
529 original: { line: 1, column: 1 },
530 generated: { line: 2, column: 2 },
531 source: '/original.js'
533 map = new SourceMapConsumer(map.toString());
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.');
542 exports['test github issue #64'] = function (assert, util) {
543 var map = new SourceMapConsumer({
546 "sourceRoot": "http://example.com/",
550 "sourcesContent": ["foo"]
553 assert.equal(map.sourceContentFor("a"), "foo");
554 assert.equal(map.sourceContentFor("/a"), "foo");
557 exports['test bug 885597'] = function (assert, util) {
558 var map = new SourceMapConsumer({
561 "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",
565 "sourcesContent": ["foo"]
568 var s = map.sources[0];
569 assert.equal(map.sourceContentFor(s), "foo");
572 exports['test github issue #72, duplicate sources'] = function (assert, util) {
573 var map = new SourceMapConsumer({
576 "sources": ["source1.js", "source1.js", "source3.js"],
578 "mappings": ";EAAC;;IAEE;;MEEE",
579 "sourceRoot": "http://example.com"
582 var pos = map.originalPositionFor({
586 assert.equal(pos.source, 'http://example.com/source1.js');
587 assert.equal(pos.line, 1);
588 assert.equal(pos.column, 1);
590 var pos = map.originalPositionFor({
594 assert.equal(pos.source, 'http://example.com/source1.js');
595 assert.equal(pos.line, 3);
596 assert.equal(pos.column, 3);
598 var pos = map.originalPositionFor({
602 assert.equal(pos.source, 'http://example.com/source3.js');
603 assert.equal(pos.line, 5);
604 assert.equal(pos.column, 5);
607 exports['test github issue #72, duplicate names'] = function (assert, util) {
608 var map = new SourceMapConsumer({
611 "sources": ["source.js"],
612 "names": ["name1", "name1", "name3"],
613 "mappings": ";EAACA;;IAEEA;;MAEEE",
614 "sourceRoot": "http://example.com"
617 var pos = map.originalPositionFor({
621 assert.equal(pos.name, 'name1');
622 assert.equal(pos.line, 1);
623 assert.equal(pos.column, 1);
625 var pos = map.originalPositionFor({
629 assert.equal(pos.name, 'name1');
630 assert.equal(pos.line, 3);
631 assert.equal(pos.column, 3);
633 var pos = map.originalPositionFor({
637 assert.equal(pos.name, 'name3');
638 assert.equal(pos.line, 5);
639 assert.equal(pos.column, 5);
642 exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {
643 var smg = new SourceMapGenerator({
644 sourceRoot: 'http://example.com/',
648 original: { line: 1, column: 1 },
649 generated: { line: 2, column: 2 },
653 original: { line: 2, column: 2 },
654 generated: { line: 4, column: 4 },
658 smg.setSourceContent('baz.js', 'baz.js content');
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');
668 var pos = smc.originalPositionFor({
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);
677 pos = smc.generatedPositionFor({
680 source: 'http://example.com/bar.js'
682 assert.equal(pos.line, 2);
683 assert.equal(pos.column, 2);
685 pos = smc.originalPositionFor({
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');
694 pos = smc.generatedPositionFor({
697 source: 'http://example.com/baz.js'
699 assert.equal(pos.line, 4);
700 assert.equal(pos.column, 4);