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-generator.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 SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;12var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;13var SourceNode = require('../../lib/source-map/source-node').SourceNode;14var util = require('./util');1516exports['test some simple stuff'] = function (assert, util) {17var map = new SourceMapGenerator({18file: 'foo.js',19sourceRoot: '.'20});21assert.ok(true);2223var map = new SourceMapGenerator().toJSON();24assert.ok(!('file' in map));25assert.ok(!('sourceRoot' in map));26};2728exports['test JSON serialization'] = function (assert, util) {29var map = new SourceMapGenerator({30file: 'foo.js',31sourceRoot: '.'32});33assert.equal(map.toString(), JSON.stringify(map));34};3536exports['test adding mappings (case 1)'] = function (assert, util) {37var map = new SourceMapGenerator({38file: 'generated-foo.js',39sourceRoot: '.'40});4142assert.doesNotThrow(function () {43map.addMapping({44generated: { line: 1, column: 1 }45});46});47};4849exports['test adding mappings (case 2)'] = function (assert, util) {50var map = new SourceMapGenerator({51file: 'generated-foo.js',52sourceRoot: '.'53});5455assert.doesNotThrow(function () {56map.addMapping({57generated: { line: 1, column: 1 },58source: 'bar.js',59original: { line: 1, column: 1 }60});61});62};6364exports['test adding mappings (case 3)'] = function (assert, util) {65var map = new SourceMapGenerator({66file: 'generated-foo.js',67sourceRoot: '.'68});6970assert.doesNotThrow(function () {71map.addMapping({72generated: { line: 1, column: 1 },73source: 'bar.js',74original: { line: 1, column: 1 },75name: 'someToken'76});77});78};7980exports['test adding mappings (invalid)'] = function (assert, util) {81var map = new SourceMapGenerator({82file: 'generated-foo.js',83sourceRoot: '.'84});8586// Not enough info.87assert.throws(function () {88map.addMapping({});89});9091// Original file position, but no source.92assert.throws(function () {93map.addMapping({94generated: { line: 1, column: 1 },95original: { line: 1, column: 1 }96});97});98};99100exports['test adding mappings with skipValidation'] = function (assert, util) {101var map = new SourceMapGenerator({102file: 'generated-foo.js',103sourceRoot: '.',104skipValidation: true105});106107// Not enough info, caught by `util.getArgs`108assert.throws(function () {109map.addMapping({});110});111112// Original file position, but no source. Not checked.113assert.doesNotThrow(function () {114map.addMapping({115generated: { line: 1, column: 1 },116original: { line: 1, column: 1 }117});118});119};120121exports['test that the correct mappings are being generated'] = function (assert, util) {122var map = new SourceMapGenerator({123file: 'min.js',124sourceRoot: '/the/root'125});126127map.addMapping({128generated: { line: 1, column: 1 },129original: { line: 1, column: 1 },130source: 'one.js'131});132map.addMapping({133generated: { line: 1, column: 5 },134original: { line: 1, column: 5 },135source: 'one.js'136});137map.addMapping({138generated: { line: 1, column: 9 },139original: { line: 1, column: 11 },140source: 'one.js'141});142map.addMapping({143generated: { line: 1, column: 18 },144original: { line: 1, column: 21 },145source: 'one.js',146name: 'bar'147});148map.addMapping({149generated: { line: 1, column: 21 },150original: { line: 2, column: 3 },151source: 'one.js'152});153map.addMapping({154generated: { line: 1, column: 28 },155original: { line: 2, column: 10 },156source: 'one.js',157name: 'baz'158});159map.addMapping({160generated: { line: 1, column: 32 },161original: { line: 2, column: 14 },162source: 'one.js',163name: 'bar'164});165166map.addMapping({167generated: { line: 2, column: 1 },168original: { line: 1, column: 1 },169source: 'two.js'170});171map.addMapping({172generated: { line: 2, column: 5 },173original: { line: 1, column: 5 },174source: 'two.js'175});176map.addMapping({177generated: { line: 2, column: 9 },178original: { line: 1, column: 11 },179source: 'two.js'180});181map.addMapping({182generated: { line: 2, column: 18 },183original: { line: 1, column: 21 },184source: 'two.js',185name: 'n'186});187map.addMapping({188generated: { line: 2, column: 21 },189original: { line: 2, column: 3 },190source: 'two.js'191});192map.addMapping({193generated: { line: 2, column: 28 },194original: { line: 2, column: 10 },195source: 'two.js',196name: 'n'197});198199map = JSON.parse(map.toString());200201util.assertEqualMaps(assert, map, util.testMap);202};203204exports['test that adding a mapping with an empty string name does not break generation'] = function (assert, util) {205var map = new SourceMapGenerator({206file: 'generated-foo.js',207sourceRoot: '.'208});209210map.addMapping({211generated: { line: 1, column: 1 },212source: 'bar.js',213original: { line: 1, column: 1 },214name: ''215});216217assert.doesNotThrow(function () {218JSON.parse(map.toString());219});220};221222exports['test that source content can be set'] = function (assert, util) {223var map = new SourceMapGenerator({224file: 'min.js',225sourceRoot: '/the/root'226});227map.addMapping({228generated: { line: 1, column: 1 },229original: { line: 1, column: 1 },230source: 'one.js'231});232map.addMapping({233generated: { line: 2, column: 1 },234original: { line: 1, column: 1 },235source: 'two.js'236});237map.setSourceContent('one.js', 'one file content');238239map = JSON.parse(map.toString());240assert.equal(map.sources[0], 'one.js');241assert.equal(map.sources[1], 'two.js');242assert.equal(map.sourcesContent[0], 'one file content');243assert.equal(map.sourcesContent[1], null);244};245246exports['test .fromSourceMap'] = function (assert, util) {247var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));248util.assertEqualMaps(assert, map.toJSON(), util.testMap);249};250251exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {252var map = SourceMapGenerator.fromSourceMap(253new SourceMapConsumer(util.testMapWithSourcesContent));254util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);255};256257exports['test applySourceMap'] = function (assert, util) {258var node = new SourceNode(null, null, null, [259new SourceNode(2, 0, 'fileX', 'lineX2\n'),260'genA1\n',261new SourceNode(2, 0, 'fileY', 'lineY2\n'),262'genA2\n',263new SourceNode(1, 0, 'fileX', 'lineX1\n'),264'genA3\n',265new SourceNode(1, 0, 'fileY', 'lineY1\n')266]);267var mapStep1 = node.toStringWithSourceMap({268file: 'fileA'269}).map;270mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');271mapStep1 = mapStep1.toJSON();272273node = new SourceNode(null, null, null, [274'gen1\n',275new SourceNode(1, 0, 'fileA', 'lineA1\n'),276new SourceNode(2, 0, 'fileA', 'lineA2\n'),277new SourceNode(3, 0, 'fileA', 'lineA3\n'),278new SourceNode(4, 0, 'fileA', 'lineA4\n'),279new SourceNode(1, 0, 'fileB', 'lineB1\n'),280new SourceNode(2, 0, 'fileB', 'lineB2\n'),281'gen2\n'282]);283var mapStep2 = node.toStringWithSourceMap({284file: 'fileGen'285}).map;286mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');287mapStep2 = mapStep2.toJSON();288289node = new SourceNode(null, null, null, [290'gen1\n',291new SourceNode(2, 0, 'fileX', 'lineA1\n'),292new SourceNode(2, 0, 'fileA', 'lineA2\n'),293new SourceNode(2, 0, 'fileY', 'lineA3\n'),294new SourceNode(4, 0, 'fileA', 'lineA4\n'),295new SourceNode(1, 0, 'fileB', 'lineB1\n'),296new SourceNode(2, 0, 'fileB', 'lineB2\n'),297'gen2\n'298]);299var expectedMap = node.toStringWithSourceMap({300file: 'fileGen'301}).map;302expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');303expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');304expectedMap = expectedMap.toJSON();305306// apply source map "mapStep1" to "mapStep2"307var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));308generator.applySourceMap(new SourceMapConsumer(mapStep1));309var actualMap = generator.toJSON();310311util.assertEqualMaps(assert, actualMap, expectedMap);312};313314exports['test applySourceMap throws when file is missing'] = function (assert, util) {315var map = new SourceMapGenerator({316file: 'test.js'317});318var map2 = new SourceMapGenerator();319assert.throws(function() {320map.applySourceMap(new SourceMapConsumer(map2.toJSON()));321});322};323324exports['test the two additional parameters of applySourceMap'] = function (assert, util) {325// Assume the following directory structure:326//327// http://foo.org/328// bar.coffee329// app/330// coffee/331// foo.coffee332// temp/333// bundle.js334// temp_maps/335// bundle.js.map336// public/337// bundle.min.js338// bundle.min.js.map339//340// http://www.example.com/341// baz.coffee342343var bundleMap = new SourceMapGenerator({344file: 'bundle.js'345});346bundleMap.addMapping({347generated: { line: 3, column: 3 },348original: { line: 2, column: 2 },349source: '../../coffee/foo.coffee'350});351bundleMap.setSourceContent('../../coffee/foo.coffee', 'foo coffee');352bundleMap.addMapping({353generated: { line: 13, column: 13 },354original: { line: 12, column: 12 },355source: '/bar.coffee'356});357bundleMap.setSourceContent('/bar.coffee', 'bar coffee');358bundleMap.addMapping({359generated: { line: 23, column: 23 },360original: { line: 22, column: 22 },361source: 'http://www.example.com/baz.coffee'362});363bundleMap.setSourceContent(364'http://www.example.com/baz.coffee',365'baz coffee'366);367bundleMap = new SourceMapConsumer(bundleMap.toJSON());368369var minifiedMap = new SourceMapGenerator({370file: 'bundle.min.js',371sourceRoot: '..'372});373minifiedMap.addMapping({374generated: { line: 1, column: 1 },375original: { line: 3, column: 3 },376source: 'temp/bundle.js'377});378minifiedMap.addMapping({379generated: { line: 11, column: 11 },380original: { line: 13, column: 13 },381source: 'temp/bundle.js'382});383minifiedMap.addMapping({384generated: { line: 21, column: 21 },385original: { line: 23, column: 23 },386source: 'temp/bundle.js'387});388minifiedMap = new SourceMapConsumer(minifiedMap.toJSON());389390var expectedMap = function (sources) {391var map = new SourceMapGenerator({392file: 'bundle.min.js',393sourceRoot: '..'394});395map.addMapping({396generated: { line: 1, column: 1 },397original: { line: 2, column: 2 },398source: sources[0]399});400map.setSourceContent(sources[0], 'foo coffee');401map.addMapping({402generated: { line: 11, column: 11 },403original: { line: 12, column: 12 },404source: sources[1]405});406map.setSourceContent(sources[1], 'bar coffee');407map.addMapping({408generated: { line: 21, column: 21 },409original: { line: 22, column: 22 },410source: sources[2]411});412map.setSourceContent(sources[2], 'baz coffee');413return map.toJSON();414}415416var actualMap = function (aSourceMapPath) {417var map = SourceMapGenerator.fromSourceMap(minifiedMap);418// Note that relying on `bundleMap.file` (which is simply 'bundle.js')419// instead of supplying the second parameter wouldn't work here.420map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath);421return map.toJSON();422}423424util.assertEqualMaps(assert, actualMap('../temp/temp_maps'), expectedMap([425'coffee/foo.coffee',426'/bar.coffee',427'http://www.example.com/baz.coffee'428]));429430util.assertEqualMaps(assert, actualMap('/app/temp/temp_maps'), expectedMap([431'/app/coffee/foo.coffee',432'/bar.coffee',433'http://www.example.com/baz.coffee'434]));435436util.assertEqualMaps(assert, actualMap('http://foo.org/app/temp/temp_maps'), expectedMap([437'http://foo.org/app/coffee/foo.coffee',438'http://foo.org/bar.coffee',439'http://www.example.com/baz.coffee'440]));441442// If the third parameter is omitted or set to the current working443// directory we get incorrect source paths:444445util.assertEqualMaps(assert, actualMap(), expectedMap([446'../coffee/foo.coffee',447'/bar.coffee',448'http://www.example.com/baz.coffee'449]));450451util.assertEqualMaps(assert, actualMap(''), expectedMap([452'../coffee/foo.coffee',453'/bar.coffee',454'http://www.example.com/baz.coffee'455]));456457util.assertEqualMaps(assert, actualMap('.'), expectedMap([458'../coffee/foo.coffee',459'/bar.coffee',460'http://www.example.com/baz.coffee'461]));462463util.assertEqualMaps(assert, actualMap('./'), expectedMap([464'../coffee/foo.coffee',465'/bar.coffee',466'http://www.example.com/baz.coffee'467]));468};469470exports['test applySourceMap name handling'] = function (assert, util) {471// Imagine some CoffeeScript code being compiled into JavaScript and then472// minified.473474var assertName = function(coffeeName, jsName, expectedName) {475var minifiedMap = new SourceMapGenerator({476file: 'test.js.min'477});478minifiedMap.addMapping({479generated: { line: 1, column: 4 },480original: { line: 1, column: 4 },481source: 'test.js',482name: jsName483});484485var coffeeMap = new SourceMapGenerator({486file: 'test.js'487});488coffeeMap.addMapping({489generated: { line: 1, column: 4 },490original: { line: 1, column: 0 },491source: 'test.coffee',492name: coffeeName493});494495minifiedMap.applySourceMap(new SourceMapConsumer(coffeeMap.toJSON()));496497new SourceMapConsumer(minifiedMap.toJSON()).eachMapping(function(mapping) {498assert.equal(mapping.name, expectedName);499});500};501502// `foo = 1` -> `var foo = 1;` -> `var a=1`503// CoffeeScript doesn’t rename variables, so there’s no need for it to504// provide names in its source maps. Minifiers do rename variables and505// therefore do provide names in their source maps. So that name should be506// retained if the original map lacks names.507assertName(null, 'foo', 'foo');508509// `foo = 1` -> `var coffee$foo = 1;` -> `var a=1`510// Imagine that CoffeeScript prefixed all variables with `coffee$`. Even511// though the minifier then also provides a name, the original name is512// what corresponds to the source.513assertName('foo', 'coffee$foo', 'foo');514515// `foo = 1` -> `var coffee$foo = 1;` -> `var coffee$foo=1`516// Minifiers can turn off variable mangling. Then there’s no need to517// provide names in the source map, but the names from the original map are518// still needed.519assertName('foo', null, 'foo');520521// `foo = 1` -> `var foo = 1;` -> `var foo=1`522// No renaming at all.523assertName(null, null, null);524};525526exports['test sorting with duplicate generated mappings'] = function (assert, util) {527var map = new SourceMapGenerator({528file: 'test.js'529});530map.addMapping({531generated: { line: 3, column: 0 },532original: { line: 2, column: 0 },533source: 'a.js'534});535map.addMapping({536generated: { line: 2, column: 0 }537});538map.addMapping({539generated: { line: 2, column: 0 }540});541map.addMapping({542generated: { line: 1, column: 0 },543original: { line: 1, column: 0 },544source: 'a.js'545});546547util.assertEqualMaps(assert, map.toJSON(), {548version: 3,549file: 'test.js',550sources: ['a.js'],551names: [],552mappings: 'AAAA;A;AACA'553});554};555556exports['test ignore duplicate mappings.'] = function (assert, util) {557var init = { file: 'min.js', sourceRoot: '/the/root' };558var map1, map2;559560// null original source location561var nullMapping1 = {562generated: { line: 1, column: 0 }563};564var nullMapping2 = {565generated: { line: 2, column: 2 }566};567568map1 = new SourceMapGenerator(init);569map2 = new SourceMapGenerator(init);570571map1.addMapping(nullMapping1);572map1.addMapping(nullMapping1);573574map2.addMapping(nullMapping1);575576util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());577578map1.addMapping(nullMapping2);579map1.addMapping(nullMapping1);580581map2.addMapping(nullMapping2);582583util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());584585// original source location586var srcMapping1 = {587generated: { line: 1, column: 0 },588original: { line: 11, column: 0 },589source: 'srcMapping1.js'590};591var srcMapping2 = {592generated: { line: 2, column: 2 },593original: { line: 11, column: 0 },594source: 'srcMapping2.js'595};596597map1 = new SourceMapGenerator(init);598map2 = new SourceMapGenerator(init);599600map1.addMapping(srcMapping1);601map1.addMapping(srcMapping1);602603map2.addMapping(srcMapping1);604605util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());606607map1.addMapping(srcMapping2);608map1.addMapping(srcMapping1);609610map2.addMapping(srcMapping2);611612util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());613614// full original source and name information615var fullMapping1 = {616generated: { line: 1, column: 0 },617original: { line: 11, column: 0 },618source: 'fullMapping1.js',619name: 'fullMapping1'620};621var fullMapping2 = {622generated: { line: 2, column: 2 },623original: { line: 11, column: 0 },624source: 'fullMapping2.js',625name: 'fullMapping2'626};627628map1 = new SourceMapGenerator(init);629map2 = new SourceMapGenerator(init);630631map1.addMapping(fullMapping1);632map1.addMapping(fullMapping1);633634map2.addMapping(fullMapping1);635636util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());637638map1.addMapping(fullMapping2);639map1.addMapping(fullMapping1);640641map2.addMapping(fullMapping2);642643util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());644};645646exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {647var map = new SourceMapGenerator({648file: 'test.js'649});650map.addMapping({651generated: { line: 1, column: 1 },652original: { line: 2, column: 2 },653source: 'a.js',654name: 'foo'655});656map.addMapping({657generated: { line: 3, column: 3 },658original: { line: 4, column: 4 },659source: 'a.js',660name: 'foo'661});662util.assertEqualMaps(assert, map.toJSON(), {663version: 3,664file: 'test.js',665sources: ['a.js'],666names: ['foo'],667mappings: 'CACEA;;GAEEA'668});669};670671exports['test setting sourcesContent to null when already null'] = function (assert, util) {672var smg = new SourceMapGenerator({ file: "foo.js" });673assert.doesNotThrow(function() {674smg.setSourceContent("bar.js", null);675});676};677678});679680681