react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / source-map / test / source-map / test-source-map-consumer.js
80559 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;12var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;1314exports['test that we can instantiate with a string or an object'] = function (assert, util) {15assert.doesNotThrow(function () {16var map = new SourceMapConsumer(util.testMap);17});18assert.doesNotThrow(function () {19var map = new SourceMapConsumer(JSON.stringify(util.testMap));20});21};2223exports['test that the `sources` field has the original sources'] = function (assert, util) {24var map;25var sources;2627map = new SourceMapConsumer(util.testMap);28sources = map.sources;29assert.equal(sources[0], '/the/root/one.js');30assert.equal(sources[1], '/the/root/two.js');31assert.equal(sources.length, 2);3233map = new SourceMapConsumer(util.testMapNoSourceRoot);34sources = map.sources;35assert.equal(sources[0], 'one.js');36assert.equal(sources[1], 'two.js');37assert.equal(sources.length, 2);3839map = new SourceMapConsumer(util.testMapEmptySourceRoot);40sources = map.sources;41assert.equal(sources[0], 'one.js');42assert.equal(sources[1], 'two.js');43assert.equal(sources.length, 2);44};4546exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {47var map;48var mapping;4950map = new SourceMapConsumer(util.testMap);5152mapping = map.originalPositionFor({53line: 2,54column: 155});56assert.equal(mapping.source, '/the/root/two.js');5758mapping = map.originalPositionFor({59line: 1,60column: 161});62assert.equal(mapping.source, '/the/root/one.js');636465map = new SourceMapConsumer(util.testMapNoSourceRoot);6667mapping = map.originalPositionFor({68line: 2,69column: 170});71assert.equal(mapping.source, 'two.js');7273mapping = map.originalPositionFor({74line: 1,75column: 176});77assert.equal(mapping.source, 'one.js');787980map = new SourceMapConsumer(util.testMapEmptySourceRoot);8182mapping = map.originalPositionFor({83line: 2,84column: 185});86assert.equal(mapping.source, 'two.js');8788mapping = map.originalPositionFor({89line: 1,90column: 191});92assert.equal(mapping.source, 'one.js');93};9495exports['test mapping tokens back exactly'] = function (assert, util) {96var map = new SourceMapConsumer(util.testMap);9798util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);99util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);100util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);101util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);102util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);103util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);104util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);105106util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);107util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);108util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);109util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);110util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);111util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);112};113114exports['test mapping tokens fuzzy'] = function (assert, util) {115var map = new SourceMapConsumer(util.testMap);116117// Finding original positions118util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);119util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);120util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);121122// Finding generated positions123util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);124util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);125util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);126};127128exports['test mappings and end of lines'] = function (assert, util) {129var smg = new SourceMapGenerator({130file: 'foo.js'131});132smg.addMapping({133original: { line: 1, column: 1 },134generated: { line: 1, column: 1 },135source: 'bar.js'136});137smg.addMapping({138original: { line: 2, column: 2 },139generated: { line: 2, column: 2 },140source: 'bar.js'141});142143var map = SourceMapConsumer.fromSourceMap(smg);144145// When finding original positions, mappings end at the end of the line.146util.assertMapping(2, 1, null, null, null, null, map, assert, true)147148// When finding generated positions, mappings do not end at the end of the line.149util.assertMapping(1, 1, 'bar.js', 2, 1, null, map, assert, null, true);150};151152exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {153assert.doesNotThrow(function () {154var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));155});156};157158exports['test eachMapping'] = function (assert, util) {159var map;160161map = new SourceMapConsumer(util.testMap);162var previousLine = -Infinity;163var previousColumn = -Infinity;164map.eachMapping(function (mapping) {165assert.ok(mapping.generatedLine >= previousLine);166167assert.ok(mapping.source === '/the/root/one.js' || mapping.source === '/the/root/two.js');168169if (mapping.generatedLine === previousLine) {170assert.ok(mapping.generatedColumn >= previousColumn);171previousColumn = mapping.generatedColumn;172}173else {174previousLine = mapping.generatedLine;175previousColumn = -Infinity;176}177});178179map = new SourceMapConsumer(util.testMapNoSourceRoot);180map.eachMapping(function (mapping) {181assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');182});183184map = new SourceMapConsumer(util.testMapEmptySourceRoot);185map.eachMapping(function (mapping) {186assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');187});188};189190exports['test iterating over mappings in a different order'] = function (assert, util) {191var map = new SourceMapConsumer(util.testMap);192var previousLine = -Infinity;193var previousColumn = -Infinity;194var previousSource = "";195map.eachMapping(function (mapping) {196assert.ok(mapping.source >= previousSource);197198if (mapping.source === previousSource) {199assert.ok(mapping.originalLine >= previousLine);200201if (mapping.originalLine === previousLine) {202assert.ok(mapping.originalColumn >= previousColumn);203previousColumn = mapping.originalColumn;204}205else {206previousLine = mapping.originalLine;207previousColumn = -Infinity;208}209}210else {211previousSource = mapping.source;212previousLine = -Infinity;213previousColumn = -Infinity;214}215}, null, SourceMapConsumer.ORIGINAL_ORDER);216};217218exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {219var map = new SourceMapConsumer(util.testMap);220var context = {};221map.eachMapping(function () {222assert.equal(this, context);223}, context);224};225226exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {227var map = new SourceMapConsumer(util.testMapWithSourcesContent);228var sourcesContent = map.sourcesContent;229230assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');231assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');232assert.equal(sourcesContent.length, 2);233};234235exports['test that we can get the original sources for the sources'] = function (assert, util) {236var map = new SourceMapConsumer(util.testMapWithSourcesContent);237var sources = map.sources;238239assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');240assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');241assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');242assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');243assert.throws(function () {244map.sourceContentFor("");245}, Error);246assert.throws(function () {247map.sourceContentFor("/the/root/three.js");248}, Error);249assert.throws(function () {250map.sourceContentFor("three.js");251}, Error);252};253254exports['test that we can get the original source content with relative source paths'] = function (assert, util) {255var map = new SourceMapConsumer(util.testMapRelativeSources);256var sources = map.sources;257258assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');259assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');260assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');261assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');262assert.throws(function () {263map.sourceContentFor("");264}, Error);265assert.throws(function () {266map.sourceContentFor("/the/root/three.js");267}, Error);268assert.throws(function () {269map.sourceContentFor("three.js");270}, Error);271};272273exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {274var map = new SourceMapGenerator({275sourceRoot: 'foo/bar',276file: 'baz.js'277});278map.addMapping({279original: { line: 1, column: 1 },280generated: { line: 2, column: 2 },281source: 'bang.coffee'282});283map.addMapping({284original: { line: 5, column: 5 },285generated: { line: 6, column: 6 },286source: 'bang.coffee'287});288map = new SourceMapConsumer(map.toString());289290// Should handle without sourceRoot.291var pos = map.generatedPositionFor({292line: 1,293column: 1,294source: 'bang.coffee'295});296297assert.equal(pos.line, 2);298assert.equal(pos.column, 2);299300// Should handle with sourceRoot.301var pos = map.generatedPositionFor({302line: 1,303column: 1,304source: 'foo/bar/bang.coffee'305});306307assert.equal(pos.line, 2);308assert.equal(pos.column, 2);309};310311exports['test allGeneratedPositionsFor'] = function (assert, util) {312var map = new SourceMapGenerator({313file: 'generated.js'314});315map.addMapping({316original: { line: 1, column: 1 },317generated: { line: 2, column: 2 },318source: 'foo.coffee'319});320map.addMapping({321original: { line: 1, column: 1 },322generated: { line: 2, column: 2 },323source: 'bar.coffee'324});325map.addMapping({326original: { line: 2, column: 1 },327generated: { line: 3, column: 2 },328source: 'bar.coffee'329});330map.addMapping({331original: { line: 2, column: 2 },332generated: { line: 3, column: 3 },333source: 'bar.coffee'334});335map.addMapping({336original: { line: 3, column: 1 },337generated: { line: 4, column: 2 },338source: 'bar.coffee'339});340map = new SourceMapConsumer(map.toString());341342var mappings = map.allGeneratedPositionsFor({343line: 2,344source: 'bar.coffee'345});346347assert.equal(mappings.length, 2);348assert.equal(mappings[0].line, 3);349assert.equal(mappings[0].column, 2);350assert.equal(mappings[1].line, 3);351assert.equal(mappings[1].column, 3);352};353354exports['test allGeneratedPositionsFor for line with no mappings'] = function (assert, util) {355var map = new SourceMapGenerator({356file: 'generated.js'357});358map.addMapping({359original: { line: 1, column: 1 },360generated: { line: 2, column: 2 },361source: 'foo.coffee'362});363map.addMapping({364original: { line: 1, column: 1 },365generated: { line: 2, column: 2 },366source: 'bar.coffee'367});368map.addMapping({369original: { line: 3, column: 1 },370generated: { line: 4, column: 2 },371source: 'bar.coffee'372});373map = new SourceMapConsumer(map.toString());374375var mappings = map.allGeneratedPositionsFor({376line: 2,377source: 'bar.coffee'378});379380assert.equal(mappings.length, 0);381};382383exports['test allGeneratedPositionsFor source map with no mappings'] = function (assert, util) {384var map = new SourceMapGenerator({385file: 'generated.js'386});387map = new SourceMapConsumer(map.toString());388389var mappings = map.allGeneratedPositionsFor({390line: 2,391source: 'bar.coffee'392});393394assert.equal(mappings.length, 0);395};396397exports['test computeColumnSpans'] = function (assert, util) {398var map = new SourceMapGenerator({399file: 'generated.js'400});401map.addMapping({402original: { line: 1, column: 1 },403generated: { line: 1, column: 1 },404source: 'foo.coffee'405});406map.addMapping({407original: { line: 2, column: 1 },408generated: { line: 2, column: 1 },409source: 'foo.coffee'410});411map.addMapping({412original: { line: 2, column: 2 },413generated: { line: 2, column: 10 },414source: 'foo.coffee'415});416map.addMapping({417original: { line: 2, column: 3 },418generated: { line: 2, column: 20 },419source: 'foo.coffee'420});421map.addMapping({422original: { line: 3, column: 1 },423generated: { line: 3, column: 1 },424source: 'foo.coffee'425});426map.addMapping({427original: { line: 3, column: 2 },428generated: { line: 3, column: 2 },429source: 'foo.coffee'430});431map = new SourceMapConsumer(map.toString());432433map.computeColumnSpans();434435var mappings = map.allGeneratedPositionsFor({436line: 1,437source: 'foo.coffee'438});439440assert.equal(mappings.length, 1);441assert.equal(mappings[0].lastColumn, Infinity);442443var mappings = map.allGeneratedPositionsFor({444line: 2,445source: 'foo.coffee'446});447448assert.equal(mappings.length, 3);449assert.equal(mappings[0].lastColumn, 9);450assert.equal(mappings[1].lastColumn, 19);451assert.equal(mappings[2].lastColumn, Infinity);452453var mappings = map.allGeneratedPositionsFor({454line: 3,455source: 'foo.coffee'456});457458assert.equal(mappings.length, 2);459assert.equal(mappings[0].lastColumn, 1);460assert.equal(mappings[1].lastColumn, Infinity);461};462463exports['test sourceRoot + originalPositionFor'] = function (assert, util) {464var map = new SourceMapGenerator({465sourceRoot: 'foo/bar',466file: 'baz.js'467});468map.addMapping({469original: { line: 1, column: 1 },470generated: { line: 2, column: 2 },471source: 'bang.coffee'472});473map = new SourceMapConsumer(map.toString());474475var pos = map.originalPositionFor({476line: 2,477column: 2,478});479480// Should always have the prepended source root481assert.equal(pos.source, 'foo/bar/bang.coffee');482assert.equal(pos.line, 1);483assert.equal(pos.column, 1);484};485486exports['test github issue #56'] = function (assert, util) {487var map = new SourceMapGenerator({488sourceRoot: 'http://',489file: 'www.example.com/foo.js'490});491map.addMapping({492original: { line: 1, column: 1 },493generated: { line: 2, column: 2 },494source: 'www.example.com/original.js'495});496map = new SourceMapConsumer(map.toString());497498var sources = map.sources;499assert.equal(sources.length, 1);500assert.equal(sources[0], 'http://www.example.com/original.js');501};502503exports['test github issue #43'] = function (assert, util) {504var map = new SourceMapGenerator({505sourceRoot: 'http://example.com',506file: 'foo.js'507});508map.addMapping({509original: { line: 1, column: 1 },510generated: { line: 2, column: 2 },511source: 'http://cdn.example.com/original.js'512});513map = new SourceMapConsumer(map.toString());514515var sources = map.sources;516assert.equal(sources.length, 1,517'Should only be one source.');518assert.equal(sources[0], 'http://cdn.example.com/original.js',519'Should not be joined with the sourceRoot.');520};521522exports['test absolute path, but same host sources'] = function (assert, util) {523var map = new SourceMapGenerator({524sourceRoot: 'http://example.com/foo/bar',525file: 'foo.js'526});527map.addMapping({528original: { line: 1, column: 1 },529generated: { line: 2, column: 2 },530source: '/original.js'531});532map = new SourceMapConsumer(map.toString());533534var sources = map.sources;535assert.equal(sources.length, 1,536'Should only be one source.');537assert.equal(sources[0], 'http://example.com/original.js',538'Source should be relative the host of the source root.');539};540541exports['test github issue #64'] = function (assert, util) {542var map = new SourceMapConsumer({543"version": 3,544"file": "foo.js",545"sourceRoot": "http://example.com/",546"sources": ["/a"],547"names": [],548"mappings": "AACA",549"sourcesContent": ["foo"]550});551552assert.equal(map.sourceContentFor("a"), "foo");553assert.equal(map.sourceContentFor("/a"), "foo");554};555556exports['test bug 885597'] = function (assert, util) {557var map = new SourceMapConsumer({558"version": 3,559"file": "foo.js",560"sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",561"sources": ["/a"],562"names": [],563"mappings": "AACA",564"sourcesContent": ["foo"]565});566567var s = map.sources[0];568assert.equal(map.sourceContentFor(s), "foo");569};570571exports['test github issue #72, duplicate sources'] = function (assert, util) {572var map = new SourceMapConsumer({573"version": 3,574"file": "foo.js",575"sources": ["source1.js", "source1.js", "source3.js"],576"names": [],577"mappings": ";EAAC;;IAEE;;MEEE",578"sourceRoot": "http://example.com"579});580581var pos = map.originalPositionFor({582line: 2,583column: 2584});585assert.equal(pos.source, 'http://example.com/source1.js');586assert.equal(pos.line, 1);587assert.equal(pos.column, 1);588589var pos = map.originalPositionFor({590line: 4,591column: 4592});593assert.equal(pos.source, 'http://example.com/source1.js');594assert.equal(pos.line, 3);595assert.equal(pos.column, 3);596597var pos = map.originalPositionFor({598line: 6,599column: 6600});601assert.equal(pos.source, 'http://example.com/source3.js');602assert.equal(pos.line, 5);603assert.equal(pos.column, 5);604};605606exports['test github issue #72, duplicate names'] = function (assert, util) {607var map = new SourceMapConsumer({608"version": 3,609"file": "foo.js",610"sources": ["source.js"],611"names": ["name1", "name1", "name3"],612"mappings": ";EAACA;;IAEEA;;MAEEE",613"sourceRoot": "http://example.com"614});615616var pos = map.originalPositionFor({617line: 2,618column: 2619});620assert.equal(pos.name, 'name1');621assert.equal(pos.line, 1);622assert.equal(pos.column, 1);623624var pos = map.originalPositionFor({625line: 4,626column: 4627});628assert.equal(pos.name, 'name1');629assert.equal(pos.line, 3);630assert.equal(pos.column, 3);631632var pos = map.originalPositionFor({633line: 6,634column: 6635});636assert.equal(pos.name, 'name3');637assert.equal(pos.line, 5);638assert.equal(pos.column, 5);639};640641exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {642var smg = new SourceMapGenerator({643sourceRoot: 'http://example.com/',644file: 'foo.js'645});646smg.addMapping({647original: { line: 1, column: 1 },648generated: { line: 2, column: 2 },649source: 'bar.js'650});651smg.addMapping({652original: { line: 2, column: 2 },653generated: { line: 4, column: 4 },654source: 'baz.js',655name: 'dirtMcGirt'656});657smg.setSourceContent('baz.js', 'baz.js content');658659var smc = SourceMapConsumer.fromSourceMap(smg);660assert.equal(smc.file, 'foo.js');661assert.equal(smc.sourceRoot, 'http://example.com/');662assert.equal(smc.sources.length, 2);663assert.equal(smc.sources[0], 'http://example.com/bar.js');664assert.equal(smc.sources[1], 'http://example.com/baz.js');665assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');666667var pos = smc.originalPositionFor({668line: 2,669column: 2670});671assert.equal(pos.line, 1);672assert.equal(pos.column, 1);673assert.equal(pos.source, 'http://example.com/bar.js');674assert.equal(pos.name, null);675676pos = smc.generatedPositionFor({677line: 1,678column: 1,679source: 'http://example.com/bar.js'680});681assert.equal(pos.line, 2);682assert.equal(pos.column, 2);683684pos = smc.originalPositionFor({685line: 4,686column: 4687});688assert.equal(pos.line, 2);689assert.equal(pos.column, 2);690assert.equal(pos.source, 'http://example.com/baz.js');691assert.equal(pos.name, 'dirtMcGirt');692693pos = smc.generatedPositionFor({694line: 2,695column: 2,696source: 'http://example.com/baz.js'697});698assert.equal(pos.line, 4);699assert.equal(pos.column, 4);700};701});702703704