react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / source-map / dist / test / test_source_map_generator.js
80559 views/*1* WARNING!2*3* Do not edit this file directly, it is built from the sources at4* https://github.com/mozilla/source-map/5*/67Components.utils.import('resource://test/Utils.jsm');8/* -*- Mode: js; js-indent-level: 2; -*- */9/*10* Copyright 2011 Mozilla Foundation and contributors11* Licensed under the New BSD license. See LICENSE or:12* http://opensource.org/licenses/BSD-3-Clause13*/14define("test/source-map/test-source-map-generator", ["require", "exports", "module"], function (require, exports, module) {1516var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;17var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;18var SourceNode = require('source-map/source-node').SourceNode;19var util = require('source-map/util');2021exports['test some simple stuff'] = function (assert, util) {22var map = new SourceMapGenerator({23file: 'foo.js',24sourceRoot: '.'25});26assert.ok(true);2728var map = new SourceMapGenerator().toJSON();29assert.ok(!('file' in map));30assert.ok(!('sourceRoot' in map));31};3233exports['test JSON serialization'] = function (assert, util) {34var map = new SourceMapGenerator({35file: 'foo.js',36sourceRoot: '.'37});38assert.equal(map.toString(), JSON.stringify(map));39};4041exports['test adding mappings (case 1)'] = function (assert, util) {42var map = new SourceMapGenerator({43file: 'generated-foo.js',44sourceRoot: '.'45});4647assert.doesNotThrow(function () {48map.addMapping({49generated: { line: 1, column: 1 }50});51});52};5354exports['test adding mappings (case 2)'] = function (assert, util) {55var map = new SourceMapGenerator({56file: 'generated-foo.js',57sourceRoot: '.'58});5960assert.doesNotThrow(function () {61map.addMapping({62generated: { line: 1, column: 1 },63source: 'bar.js',64original: { line: 1, column: 1 }65});66});67};6869exports['test adding mappings (case 3)'] = function (assert, util) {70var map = new SourceMapGenerator({71file: 'generated-foo.js',72sourceRoot: '.'73});7475assert.doesNotThrow(function () {76map.addMapping({77generated: { line: 1, column: 1 },78source: 'bar.js',79original: { line: 1, column: 1 },80name: 'someToken'81});82});83};8485exports['test adding mappings (invalid)'] = function (assert, util) {86var map = new SourceMapGenerator({87file: 'generated-foo.js',88sourceRoot: '.'89});9091// Not enough info.92assert.throws(function () {93map.addMapping({});94});9596// Original file position, but no source.97assert.throws(function () {98map.addMapping({99generated: { line: 1, column: 1 },100original: { line: 1, column: 1 }101});102});103};104105exports['test adding mappings with skipValidation'] = function (assert, util) {106var map = new SourceMapGenerator({107file: 'generated-foo.js',108sourceRoot: '.',109skipValidation: true110});111112// Not enough info, caught by `util.getArgs`113assert.throws(function () {114map.addMapping({});115});116117// Original file position, but no source. Not checked.118assert.doesNotThrow(function () {119map.addMapping({120generated: { line: 1, column: 1 },121original: { line: 1, column: 1 }122});123});124};125126exports['test that the correct mappings are being generated'] = function (assert, util) {127var map = new SourceMapGenerator({128file: 'min.js',129sourceRoot: '/the/root'130});131132map.addMapping({133generated: { line: 1, column: 1 },134original: { line: 1, column: 1 },135source: 'one.js'136});137map.addMapping({138generated: { line: 1, column: 5 },139original: { line: 1, column: 5 },140source: 'one.js'141});142map.addMapping({143generated: { line: 1, column: 9 },144original: { line: 1, column: 11 },145source: 'one.js'146});147map.addMapping({148generated: { line: 1, column: 18 },149original: { line: 1, column: 21 },150source: 'one.js',151name: 'bar'152});153map.addMapping({154generated: { line: 1, column: 21 },155original: { line: 2, column: 3 },156source: 'one.js'157});158map.addMapping({159generated: { line: 1, column: 28 },160original: { line: 2, column: 10 },161source: 'one.js',162name: 'baz'163});164map.addMapping({165generated: { line: 1, column: 32 },166original: { line: 2, column: 14 },167source: 'one.js',168name: 'bar'169});170171map.addMapping({172generated: { line: 2, column: 1 },173original: { line: 1, column: 1 },174source: 'two.js'175});176map.addMapping({177generated: { line: 2, column: 5 },178original: { line: 1, column: 5 },179source: 'two.js'180});181map.addMapping({182generated: { line: 2, column: 9 },183original: { line: 1, column: 11 },184source: 'two.js'185});186map.addMapping({187generated: { line: 2, column: 18 },188original: { line: 1, column: 21 },189source: 'two.js',190name: 'n'191});192map.addMapping({193generated: { line: 2, column: 21 },194original: { line: 2, column: 3 },195source: 'two.js'196});197map.addMapping({198generated: { line: 2, column: 28 },199original: { line: 2, column: 10 },200source: 'two.js',201name: 'n'202});203204map = JSON.parse(map.toString());205206util.assertEqualMaps(assert, map, util.testMap);207};208209exports['test that adding a mapping with an empty string name does not break generation'] = function (assert, util) {210var map = new SourceMapGenerator({211file: 'generated-foo.js',212sourceRoot: '.'213});214215map.addMapping({216generated: { line: 1, column: 1 },217source: 'bar.js',218original: { line: 1, column: 1 },219name: ''220});221222assert.doesNotThrow(function () {223JSON.parse(map.toString());224});225};226227exports['test that source content can be set'] = function (assert, util) {228var map = new SourceMapGenerator({229file: 'min.js',230sourceRoot: '/the/root'231});232map.addMapping({233generated: { line: 1, column: 1 },234original: { line: 1, column: 1 },235source: 'one.js'236});237map.addMapping({238generated: { line: 2, column: 1 },239original: { line: 1, column: 1 },240source: 'two.js'241});242map.setSourceContent('one.js', 'one file content');243244map = JSON.parse(map.toString());245assert.equal(map.sources[0], 'one.js');246assert.equal(map.sources[1], 'two.js');247assert.equal(map.sourcesContent[0], 'one file content');248assert.equal(map.sourcesContent[1], null);249};250251exports['test .fromSourceMap'] = function (assert, util) {252var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));253util.assertEqualMaps(assert, map.toJSON(), util.testMap);254};255256exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {257var map = SourceMapGenerator.fromSourceMap(258new SourceMapConsumer(util.testMapWithSourcesContent));259util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);260};261262exports['test applySourceMap'] = function (assert, util) {263var node = new SourceNode(null, null, null, [264new SourceNode(2, 0, 'fileX', 'lineX2\n'),265'genA1\n',266new SourceNode(2, 0, 'fileY', 'lineY2\n'),267'genA2\n',268new SourceNode(1, 0, 'fileX', 'lineX1\n'),269'genA3\n',270new SourceNode(1, 0, 'fileY', 'lineY1\n')271]);272var mapStep1 = node.toStringWithSourceMap({273file: 'fileA'274}).map;275mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');276mapStep1 = mapStep1.toJSON();277278node = new SourceNode(null, null, null, [279'gen1\n',280new SourceNode(1, 0, 'fileA', 'lineA1\n'),281new SourceNode(2, 0, 'fileA', 'lineA2\n'),282new SourceNode(3, 0, 'fileA', 'lineA3\n'),283new SourceNode(4, 0, 'fileA', 'lineA4\n'),284new SourceNode(1, 0, 'fileB', 'lineB1\n'),285new SourceNode(2, 0, 'fileB', 'lineB2\n'),286'gen2\n'287]);288var mapStep2 = node.toStringWithSourceMap({289file: 'fileGen'290}).map;291mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');292mapStep2 = mapStep2.toJSON();293294node = new SourceNode(null, null, null, [295'gen1\n',296new SourceNode(2, 0, 'fileX', 'lineA1\n'),297new SourceNode(2, 0, 'fileA', 'lineA2\n'),298new SourceNode(2, 0, 'fileY', 'lineA3\n'),299new SourceNode(4, 0, 'fileA', 'lineA4\n'),300new SourceNode(1, 0, 'fileB', 'lineB1\n'),301new SourceNode(2, 0, 'fileB', 'lineB2\n'),302'gen2\n'303]);304var expectedMap = node.toStringWithSourceMap({305file: 'fileGen'306}).map;307expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');308expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');309expectedMap = expectedMap.toJSON();310311// apply source map "mapStep1" to "mapStep2"312var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));313generator.applySourceMap(new SourceMapConsumer(mapStep1));314var actualMap = generator.toJSON();315316util.assertEqualMaps(assert, actualMap, expectedMap);317};318319exports['test applySourceMap throws when file is missing'] = function (assert, util) {320var map = new SourceMapGenerator({321file: 'test.js'322});323var map2 = new SourceMapGenerator();324assert.throws(function() {325map.applySourceMap(new SourceMapConsumer(map2.toJSON()));326});327};328329exports['test the two additional parameters of applySourceMap'] = function (assert, util) {330// Assume the following directory structure:331//332// http://foo.org/333// bar.coffee334// app/335// coffee/336// foo.coffee337// temp/338// bundle.js339// temp_maps/340// bundle.js.map341// public/342// bundle.min.js343// bundle.min.js.map344//345// http://www.example.com/346// baz.coffee347348var bundleMap = new SourceMapGenerator({349file: 'bundle.js'350});351bundleMap.addMapping({352generated: { line: 3, column: 3 },353original: { line: 2, column: 2 },354source: '../../coffee/foo.coffee'355});356bundleMap.setSourceContent('../../coffee/foo.coffee', 'foo coffee');357bundleMap.addMapping({358generated: { line: 13, column: 13 },359original: { line: 12, column: 12 },360source: '/bar.coffee'361});362bundleMap.setSourceContent('/bar.coffee', 'bar coffee');363bundleMap.addMapping({364generated: { line: 23, column: 23 },365original: { line: 22, column: 22 },366source: 'http://www.example.com/baz.coffee'367});368bundleMap.setSourceContent(369'http://www.example.com/baz.coffee',370'baz coffee'371);372bundleMap = new SourceMapConsumer(bundleMap.toJSON());373374var minifiedMap = new SourceMapGenerator({375file: 'bundle.min.js',376sourceRoot: '..'377});378minifiedMap.addMapping({379generated: { line: 1, column: 1 },380original: { line: 3, column: 3 },381source: 'temp/bundle.js'382});383minifiedMap.addMapping({384generated: { line: 11, column: 11 },385original: { line: 13, column: 13 },386source: 'temp/bundle.js'387});388minifiedMap.addMapping({389generated: { line: 21, column: 21 },390original: { line: 23, column: 23 },391source: 'temp/bundle.js'392});393minifiedMap = new SourceMapConsumer(minifiedMap.toJSON());394395var expectedMap = function (sources) {396var map = new SourceMapGenerator({397file: 'bundle.min.js',398sourceRoot: '..'399});400map.addMapping({401generated: { line: 1, column: 1 },402original: { line: 2, column: 2 },403source: sources[0]404});405map.setSourceContent(sources[0], 'foo coffee');406map.addMapping({407generated: { line: 11, column: 11 },408original: { line: 12, column: 12 },409source: sources[1]410});411map.setSourceContent(sources[1], 'bar coffee');412map.addMapping({413generated: { line: 21, column: 21 },414original: { line: 22, column: 22 },415source: sources[2]416});417map.setSourceContent(sources[2], 'baz coffee');418return map.toJSON();419}420421var actualMap = function (aSourceMapPath) {422var map = SourceMapGenerator.fromSourceMap(minifiedMap);423// Note that relying on `bundleMap.file` (which is simply 'bundle.js')424// instead of supplying the second parameter wouldn't work here.425map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath);426return map.toJSON();427}428429util.assertEqualMaps(assert, actualMap('../temp/temp_maps'), expectedMap([430'coffee/foo.coffee',431'/bar.coffee',432'http://www.example.com/baz.coffee'433]));434435util.assertEqualMaps(assert, actualMap('/app/temp/temp_maps'), expectedMap([436'/app/coffee/foo.coffee',437'/bar.coffee',438'http://www.example.com/baz.coffee'439]));440441util.assertEqualMaps(assert, actualMap('http://foo.org/app/temp/temp_maps'), expectedMap([442'http://foo.org/app/coffee/foo.coffee',443'http://foo.org/bar.coffee',444'http://www.example.com/baz.coffee'445]));446447// If the third parameter is omitted or set to the current working448// directory we get incorrect source paths:449450util.assertEqualMaps(assert, actualMap(), expectedMap([451'../coffee/foo.coffee',452'/bar.coffee',453'http://www.example.com/baz.coffee'454]));455456util.assertEqualMaps(assert, actualMap(''), expectedMap([457'../coffee/foo.coffee',458'/bar.coffee',459'http://www.example.com/baz.coffee'460]));461462util.assertEqualMaps(assert, actualMap('.'), expectedMap([463'../coffee/foo.coffee',464'/bar.coffee',465'http://www.example.com/baz.coffee'466]));467468util.assertEqualMaps(assert, actualMap('./'), expectedMap([469'../coffee/foo.coffee',470'/bar.coffee',471'http://www.example.com/baz.coffee'472]));473};474475exports['test applySourceMap name handling'] = function (assert, util) {476// Imagine some CoffeeScript code being compiled into JavaScript and then477// minified.478479var assertName = function(coffeeName, jsName, expectedName) {480var minifiedMap = new SourceMapGenerator({481file: 'test.js.min'482});483minifiedMap.addMapping({484generated: { line: 1, column: 4 },485original: { line: 1, column: 4 },486source: 'test.js',487name: jsName488});489490var coffeeMap = new SourceMapGenerator({491file: 'test.js'492});493coffeeMap.addMapping({494generated: { line: 1, column: 4 },495original: { line: 1, column: 0 },496source: 'test.coffee',497name: coffeeName498});499500minifiedMap.applySourceMap(new SourceMapConsumer(coffeeMap.toJSON()));501502new SourceMapConsumer(minifiedMap.toJSON()).eachMapping(function(mapping) {503assert.equal(mapping.name, expectedName);504});505};506507// `foo = 1` -> `var foo = 1;` -> `var a=1`508// CoffeeScript doesn’t rename variables, so there’s no need for it to509// provide names in its source maps. Minifiers do rename variables and510// therefore do provide names in their source maps. So that name should be511// retained if the original map lacks names.512assertName(null, 'foo', 'foo');513514// `foo = 1` -> `var coffee$foo = 1;` -> `var a=1`515// Imagine that CoffeeScript prefixed all variables with `coffee$`. Even516// though the minifier then also provides a name, the original name is517// what corresponds to the source.518assertName('foo', 'coffee$foo', 'foo');519520// `foo = 1` -> `var coffee$foo = 1;` -> `var coffee$foo=1`521// Minifiers can turn off variable mangling. Then there’s no need to522// provide names in the source map, but the names from the original map are523// still needed.524assertName('foo', null, 'foo');525526// `foo = 1` -> `var foo = 1;` -> `var foo=1`527// No renaming at all.528assertName(null, null, null);529};530531exports['test sorting with duplicate generated mappings'] = function (assert, util) {532var map = new SourceMapGenerator({533file: 'test.js'534});535map.addMapping({536generated: { line: 3, column: 0 },537original: { line: 2, column: 0 },538source: 'a.js'539});540map.addMapping({541generated: { line: 2, column: 0 }542});543map.addMapping({544generated: { line: 2, column: 0 }545});546map.addMapping({547generated: { line: 1, column: 0 },548original: { line: 1, column: 0 },549source: 'a.js'550});551552util.assertEqualMaps(assert, map.toJSON(), {553version: 3,554file: 'test.js',555sources: ['a.js'],556names: [],557mappings: 'AAAA;A;AACA'558});559};560561exports['test ignore duplicate mappings.'] = function (assert, util) {562var init = { file: 'min.js', sourceRoot: '/the/root' };563var map1, map2;564565// null original source location566var nullMapping1 = {567generated: { line: 1, column: 0 }568};569var nullMapping2 = {570generated: { line: 2, column: 2 }571};572573map1 = new SourceMapGenerator(init);574map2 = new SourceMapGenerator(init);575576map1.addMapping(nullMapping1);577map1.addMapping(nullMapping1);578579map2.addMapping(nullMapping1);580581util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());582583map1.addMapping(nullMapping2);584map1.addMapping(nullMapping1);585586map2.addMapping(nullMapping2);587588util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());589590// original source location591var srcMapping1 = {592generated: { line: 1, column: 0 },593original: { line: 11, column: 0 },594source: 'srcMapping1.js'595};596var srcMapping2 = {597generated: { line: 2, column: 2 },598original: { line: 11, column: 0 },599source: 'srcMapping2.js'600};601602map1 = new SourceMapGenerator(init);603map2 = new SourceMapGenerator(init);604605map1.addMapping(srcMapping1);606map1.addMapping(srcMapping1);607608map2.addMapping(srcMapping1);609610util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());611612map1.addMapping(srcMapping2);613map1.addMapping(srcMapping1);614615map2.addMapping(srcMapping2);616617util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());618619// full original source and name information620var fullMapping1 = {621generated: { line: 1, column: 0 },622original: { line: 11, column: 0 },623source: 'fullMapping1.js',624name: 'fullMapping1'625};626var fullMapping2 = {627generated: { line: 2, column: 2 },628original: { line: 11, column: 0 },629source: 'fullMapping2.js',630name: 'fullMapping2'631};632633map1 = new SourceMapGenerator(init);634map2 = new SourceMapGenerator(init);635636map1.addMapping(fullMapping1);637map1.addMapping(fullMapping1);638639map2.addMapping(fullMapping1);640641util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());642643map1.addMapping(fullMapping2);644map1.addMapping(fullMapping1);645646map2.addMapping(fullMapping2);647648util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());649};650651exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {652var map = new SourceMapGenerator({653file: 'test.js'654});655map.addMapping({656generated: { line: 1, column: 1 },657original: { line: 2, column: 2 },658source: 'a.js',659name: 'foo'660});661map.addMapping({662generated: { line: 3, column: 3 },663original: { line: 4, column: 4 },664source: 'a.js',665name: 'foo'666});667util.assertEqualMaps(assert, map.toJSON(), {668version: 3,669file: 'test.js',670sources: ['a.js'],671names: ['foo'],672mappings: 'CACEA;;GAEEA'673});674};675676exports['test setting sourcesContent to null when already null'] = function (assert, util) {677var smg = new SourceMapGenerator({ file: "foo.js" });678assert.doesNotThrow(function() {679smg.setSourceContent("bar.js", null);680});681};682683});684function run_test() {685runSourceMapTests('test/source-map/test-source-map-generator', do_throw);686}687688689