react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / 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 objects'] = 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 = new SourceMapConsumer(util.testMap);25var sources = map.sources;2627assert.equal(sources[0], '/the/root/one.js');28assert.equal(sources[1], '/the/root/two.js');29assert.equal(sources.length, 2);30};3132exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {33var map = new SourceMapConsumer(util.testMap);34var mapping;3536mapping = map.originalPositionFor({37line: 2,38column: 139});40assert.equal(mapping.source, '/the/root/two.js');4142mapping = map.originalPositionFor({43line: 1,44column: 145});46assert.equal(mapping.source, '/the/root/one.js');47};4849exports['test mapping tokens back exactly'] = function (assert, util) {50var map = new SourceMapConsumer(util.testMap);5152util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);53util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);54util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);55util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);56util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);57util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);58util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);5960util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);61util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);62util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);63util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);64util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);65util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);66};6768exports['test mapping tokens fuzzy'] = function (assert, util) {69var map = new SourceMapConsumer(util.testMap);7071// Finding original positions72util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);73util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);74util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);7576// Finding generated positions77util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);78util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);79util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);80};8182exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {83assert.doesNotThrow(function () {84var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));85});86};8788exports['test eachMapping'] = function (assert, util) {89var map = new SourceMapConsumer(util.testMap);90var previousLine = -Infinity;91var previousColumn = -Infinity;92map.eachMapping(function (mapping) {93assert.ok(mapping.generatedLine >= previousLine);9495if (mapping.source) {96assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0);97}9899if (mapping.generatedLine === previousLine) {100assert.ok(mapping.generatedColumn >= previousColumn);101previousColumn = mapping.generatedColumn;102}103else {104previousLine = mapping.generatedLine;105previousColumn = -Infinity;106}107});108};109110exports['test iterating over mappings in a different order'] = function (assert, util) {111var map = new SourceMapConsumer(util.testMap);112var previousLine = -Infinity;113var previousColumn = -Infinity;114var previousSource = "";115map.eachMapping(function (mapping) {116assert.ok(mapping.source >= previousSource);117118if (mapping.source === previousSource) {119assert.ok(mapping.originalLine >= previousLine);120121if (mapping.originalLine === previousLine) {122assert.ok(mapping.originalColumn >= previousColumn);123previousColumn = mapping.originalColumn;124}125else {126previousLine = mapping.originalLine;127previousColumn = -Infinity;128}129}130else {131previousSource = mapping.source;132previousLine = -Infinity;133previousColumn = -Infinity;134}135}, null, SourceMapConsumer.ORIGINAL_ORDER);136};137138exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {139var map = new SourceMapConsumer(util.testMap);140var context = {};141map.eachMapping(function () {142assert.equal(this, context);143}, context);144};145146exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {147var map = new SourceMapConsumer(util.testMapWithSourcesContent);148var sourcesContent = map.sourcesContent;149150assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');151assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');152assert.equal(sourcesContent.length, 2);153};154155exports['test that we can get the original sources for the sources'] = function (assert, util) {156var map = new SourceMapConsumer(util.testMapWithSourcesContent);157var sources = map.sources;158159assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');160assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');161assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');162assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');163assert.throws(function () {164map.sourceContentFor("");165}, Error);166assert.throws(function () {167map.sourceContentFor("/the/root/three.js");168}, Error);169assert.throws(function () {170map.sourceContentFor("three.js");171}, Error);172};173174exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {175var map = new SourceMapGenerator({176sourceRoot: 'foo/bar',177file: 'baz.js'178});179map.addMapping({180original: { line: 1, column: 1 },181generated: { line: 2, column: 2 },182source: 'bang.coffee'183});184map.addMapping({185original: { line: 5, column: 5 },186generated: { line: 6, column: 6 },187source: 'bang.coffee'188});189map = new SourceMapConsumer(map.toString());190191// Should handle without sourceRoot.192var pos = map.generatedPositionFor({193line: 1,194column: 1,195source: 'bang.coffee'196});197198assert.equal(pos.line, 2);199assert.equal(pos.column, 2);200201// Should handle with sourceRoot.202var pos = map.generatedPositionFor({203line: 1,204column: 1,205source: 'foo/bar/bang.coffee'206});207208assert.equal(pos.line, 2);209assert.equal(pos.column, 2);210};211212exports['test sourceRoot + originalPositionFor'] = function (assert, util) {213var map = new SourceMapGenerator({214sourceRoot: 'foo/bar',215file: 'baz.js'216});217map.addMapping({218original: { line: 1, column: 1 },219generated: { line: 2, column: 2 },220source: 'bang.coffee'221});222map = new SourceMapConsumer(map.toString());223224var pos = map.originalPositionFor({225line: 2,226column: 2,227});228229// Should always have the prepended source root230assert.equal(pos.source, 'foo/bar/bang.coffee');231assert.equal(pos.line, 1);232assert.equal(pos.column, 1);233};234235exports['test github issue #56'] = function (assert, util) {236var map = new SourceMapGenerator({237sourceRoot: 'http://',238file: 'www.example.com/foo.js'239});240map.addMapping({241original: { line: 1, column: 1 },242generated: { line: 2, column: 2 },243source: 'www.example.com/original.js'244});245map = new SourceMapConsumer(map.toString());246247var sources = map.sources;248assert.equal(sources.length, 1);249assert.equal(sources[0], 'http://www.example.com/original.js');250};251252exports['test github issue #43'] = function (assert, util) {253var map = new SourceMapGenerator({254sourceRoot: 'http://example.com',255file: 'foo.js'256});257map.addMapping({258original: { line: 1, column: 1 },259generated: { line: 2, column: 2 },260source: 'http://cdn.example.com/original.js'261});262map = new SourceMapConsumer(map.toString());263264var sources = map.sources;265assert.equal(sources.length, 1,266'Should only be one source.');267assert.equal(sources[0], 'http://cdn.example.com/original.js',268'Should not be joined with the sourceRoot.');269};270271exports['test absolute path, but same host sources'] = function (assert, util) {272var map = new SourceMapGenerator({273sourceRoot: 'http://example.com/foo/bar',274file: 'foo.js'275});276map.addMapping({277original: { line: 1, column: 1 },278generated: { line: 2, column: 2 },279source: '/original.js'280});281map = new SourceMapConsumer(map.toString());282283var sources = map.sources;284assert.equal(sources.length, 1,285'Should only be one source.');286assert.equal(sources[0], 'http://example.com/original.js',287'Source should be relative the host of the source root.');288};289290exports['test github issue #64'] = function (assert, util) {291var map = new SourceMapConsumer({292"version": 3,293"file": "foo.js",294"sourceRoot": "http://example.com/",295"sources": ["/a"],296"names": [],297"mappings": "AACA",298"sourcesContent": ["foo"]299});300301assert.equal(map.sourceContentFor("a"), "foo");302assert.equal(map.sourceContentFor("/a"), "foo");303};304305exports['test bug 885597'] = function (assert, util) {306var map = new SourceMapConsumer({307"version": 3,308"file": "foo.js",309"sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",310"sources": ["/a"],311"names": [],312"mappings": "AACA",313"sourcesContent": ["foo"]314});315316var s = map.sources[0];317assert.equal(map.sourceContentFor(s), "foo");318};319320exports['test github issue #72, duplicate sources'] = function (assert, util) {321var map = new SourceMapConsumer({322"version": 3,323"file": "foo.js",324"sources": ["source1.js", "source1.js", "source3.js"],325"names": [],326"mappings": ";EAAC;;IAEE;;MEEE",327"sourceRoot": "http://example.com"328});329330var pos = map.originalPositionFor({331line: 2,332column: 2333});334assert.equal(pos.source, 'http://example.com/source1.js');335assert.equal(pos.line, 1);336assert.equal(pos.column, 1);337338var pos = map.originalPositionFor({339line: 4,340column: 4341});342assert.equal(pos.source, 'http://example.com/source1.js');343assert.equal(pos.line, 3);344assert.equal(pos.column, 3);345346var pos = map.originalPositionFor({347line: 6,348column: 6349});350assert.equal(pos.source, 'http://example.com/source3.js');351assert.equal(pos.line, 5);352assert.equal(pos.column, 5);353};354355exports['test github issue #72, duplicate names'] = function (assert, util) {356var map = new SourceMapConsumer({357"version": 3,358"file": "foo.js",359"sources": ["source.js"],360"names": ["name1", "name1", "name3"],361"mappings": ";EAACA;;IAEEA;;MAEEE",362"sourceRoot": "http://example.com"363});364365var pos = map.originalPositionFor({366line: 2,367column: 2368});369assert.equal(pos.name, 'name1');370assert.equal(pos.line, 1);371assert.equal(pos.column, 1);372373var pos = map.originalPositionFor({374line: 4,375column: 4376});377assert.equal(pos.name, 'name1');378assert.equal(pos.line, 3);379assert.equal(pos.column, 3);380381var pos = map.originalPositionFor({382line: 6,383column: 6384});385assert.equal(pos.name, 'name3');386assert.equal(pos.line, 5);387assert.equal(pos.column, 5);388};389390exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {391var smg = new SourceMapGenerator({392sourceRoot: 'http://example.com/',393file: 'foo.js'394});395smg.addMapping({396original: { line: 1, column: 1 },397generated: { line: 2, column: 2 },398source: 'bar.js'399});400smg.addMapping({401original: { line: 2, column: 2 },402generated: { line: 4, column: 4 },403source: 'baz.js',404name: 'dirtMcGirt'405});406smg.setSourceContent('baz.js', 'baz.js content');407408var smc = SourceMapConsumer.fromSourceMap(smg);409assert.equal(smc.file, 'foo.js');410assert.equal(smc.sourceRoot, 'http://example.com/');411assert.equal(smc.sources.length, 2);412assert.equal(smc.sources[0], 'http://example.com/bar.js');413assert.equal(smc.sources[1], 'http://example.com/baz.js');414assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');415416var pos = smc.originalPositionFor({417line: 2,418column: 2419});420assert.equal(pos.line, 1);421assert.equal(pos.column, 1);422assert.equal(pos.source, 'http://example.com/bar.js');423assert.equal(pos.name, null);424425pos = smc.generatedPositionFor({426line: 1,427column: 1,428source: 'http://example.com/bar.js'429});430assert.equal(pos.line, 2);431assert.equal(pos.column, 2);432433pos = smc.originalPositionFor({434line: 4,435column: 4436});437assert.equal(pos.line, 2);438assert.equal(pos.column, 2);439assert.equal(pos.source, 'http://example.com/baz.js');440assert.equal(pos.name, 'dirtMcGirt');441442pos = smc.generatedPositionFor({443line: 2,444column: 2,445source: 'http://example.com/baz.js'446});447assert.equal(pos.line, 4);448assert.equal(pos.column, 4);449};450});451452453