react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / source-map / test / source-map / test-source-node.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;1415function forEachNewline(fn) {16return function (assert, util) {17['\n', '\r\n'].forEach(fn.bind(null, assert, util));18}19}2021exports['test .add()'] = function (assert, util) {22var node = new SourceNode(null, null, null);2324// Adding a string works.25node.add('function noop() {}');2627// Adding another source node works.28node.add(new SourceNode(null, null, null));2930// Adding an array works.31node.add(['function foo() {',32new SourceNode(null, null, null,33'return 10;'),34'}']);3536// Adding other stuff doesn't.37assert.throws(function () {38node.add({});39});40assert.throws(function () {41node.add(function () {});42});43};4445exports['test .prepend()'] = function (assert, util) {46var node = new SourceNode(null, null, null);4748// Prepending a string works.49node.prepend('function noop() {}');50assert.equal(node.children[0], 'function noop() {}');51assert.equal(node.children.length, 1);5253// Prepending another source node works.54node.prepend(new SourceNode(null, null, null));55assert.equal(node.children[0], '');56assert.equal(node.children[1], 'function noop() {}');57assert.equal(node.children.length, 2);5859// Prepending an array works.60node.prepend(['function foo() {',61new SourceNode(null, null, null,62'return 10;'),63'}']);64assert.equal(node.children[0], 'function foo() {');65assert.equal(node.children[1], 'return 10;');66assert.equal(node.children[2], '}');67assert.equal(node.children[3], '');68assert.equal(node.children[4], 'function noop() {}');69assert.equal(node.children.length, 5);7071// Prepending other stuff doesn't.72assert.throws(function () {73node.prepend({});74});75assert.throws(function () {76node.prepend(function () {});77});78};7980exports['test .toString()'] = function (assert, util) {81assert.equal((new SourceNode(null, null, null,82['function foo() {',83new SourceNode(null, null, null, 'return 10;'),84'}'])).toString(),85'function foo() {return 10;}');86};8788exports['test .join()'] = function (assert, util) {89assert.equal((new SourceNode(null, null, null,90['a', 'b', 'c', 'd'])).join(', ').toString(),91'a, b, c, d');92};9394exports['test .walk()'] = function (assert, util) {95var node = new SourceNode(null, null, null,96['(function () {\n',97' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',98' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',99'}());']);100var expected = [101{ str: '(function () {\n', source: null, line: null, column: null },102{ str: ' ', source: null, line: null, column: null },103{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },104{ str: ';\n', source: null, line: null, column: null },105{ str: ' ', source: null, line: null, column: null },106{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },107{ str: ';\n', source: null, line: null, column: null },108{ str: '}());', source: null, line: null, column: null },109];110var i = 0;111node.walk(function (chunk, loc) {112assert.equal(expected[i].str, chunk);113assert.equal(expected[i].source, loc.source);114assert.equal(expected[i].line, loc.line);115assert.equal(expected[i].column, loc.column);116i++;117});118};119120exports['test .replaceRight'] = function (assert, util) {121var node;122123// Not nested124node = new SourceNode(null, null, null, 'hello world');125node.replaceRight(/world/, 'universe');126assert.equal(node.toString(), 'hello universe');127128// Nested129node = new SourceNode(null, null, null,130[new SourceNode(null, null, null, 'hey sexy mama, '),131new SourceNode(null, null, null, 'want to kill all humans?')]);132node.replaceRight(/kill all humans/, 'watch Futurama');133assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');134};135136exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {137var node = new SourceNode(null, null, null,138['(function () {' + nl,139' ',140new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),141new SourceNode(1, 8, 'a.js', '()'),142';' + nl,143' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,144'}());']);145var result = node.toStringWithSourceMap({146file: 'foo.js'147});148149assert.equal(result.code, [150'(function () {',151' someCall();',152' if (foo) bar();',153'}());'154].join(nl));155156var map = result.map;157var mapWithoutOptions = node.toStringWithSourceMap().map;158159assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');160assert.ok(mapWithoutOptions instanceof SourceMapGenerator, 'mapWithoutOptions instanceof SourceMapGenerator');161assert.ok(!('file' in mapWithoutOptions));162mapWithoutOptions._file = 'foo.js';163util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());164165map = new SourceMapConsumer(map.toString());166167var actual;168169actual = map.originalPositionFor({170line: 1,171column: 4172});173assert.equal(actual.source, null);174assert.equal(actual.line, null);175assert.equal(actual.column, null);176177actual = map.originalPositionFor({178line: 2,179column: 2180});181assert.equal(actual.source, 'a.js');182assert.equal(actual.line, 1);183assert.equal(actual.column, 0);184assert.equal(actual.name, 'originalCall');185186actual = map.originalPositionFor({187line: 3,188column: 2189});190assert.equal(actual.source, 'b.js');191assert.equal(actual.line, 2);192assert.equal(actual.column, 0);193194actual = map.originalPositionFor({195line: 3,196column: 16197});198assert.equal(actual.source, null);199assert.equal(actual.line, null);200assert.equal(actual.column, null);201202actual = map.originalPositionFor({203line: 4,204column: 2205});206assert.equal(actual.source, null);207assert.equal(actual.line, null);208assert.equal(actual.column, null);209});210211exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {212var testCode = util.testGeneratedCode.replace(/\n/g, nl);213var node = SourceNode.fromStringWithSourceMap(214testCode,215new SourceMapConsumer(util.testMap));216217var result = node.toStringWithSourceMap({218file: 'min.js'219});220var map = result.map;221var code = result.code;222223assert.equal(code, testCode);224assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');225map = map.toJSON();226assert.equal(map.version, util.testMap.version);227assert.equal(map.file, util.testMap.file);228assert.equal(map.mappings, util.testMap.mappings);229});230231exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {232var node = SourceNode.fromStringWithSourceMap(233util.testGeneratedCode.replace(/\n/g, nl),234new SourceMapConsumer(util.emptyMap));235var result = node.toStringWithSourceMap({236file: 'min.js'237});238var map = result.map;239var code = result.code;240241assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));242assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');243map = map.toJSON();244assert.equal(map.version, util.emptyMap.version);245assert.equal(map.file, util.emptyMap.file);246assert.equal(map.mappings.length, util.emptyMap.mappings.length);247assert.equal(map.mappings, util.emptyMap.mappings);248});249250exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {251var input = new SourceNode(null, null, null, [252"(function() {" + nl,253" var Test = {};" + nl,254" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),255" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,256"}());" + nl,257"/* Generated Source */"]);258input = input.toStringWithSourceMap({259file: 'foo.js'260});261262var node = SourceNode.fromStringWithSourceMap(263input.code,264new SourceMapConsumer(input.map.toString()));265266var result = node.toStringWithSourceMap({267file: 'foo.js'268});269var map = result.map;270var code = result.code;271272assert.equal(code, input.code);273assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');274map = map.toJSON();275var inputMap = input.map.toJSON();276util.assertEqualMaps(assert, map, inputMap);277});278279exports['test .fromStringWithSourceMap() third argument'] = function (assert, util) {280// Assume the following directory structure:281//282// http://foo.org/283// bar.coffee284// app/285// coffee/286// foo.coffee287// coffeeBundle.js # Made from {foo,bar,baz}.coffee288// maps/289// coffeeBundle.js.map290// js/291// foo.js292// public/293// app.js # Made from {foo,coffeeBundle}.js294// app.js.map295//296// http://www.example.com/297// baz.coffee298299var coffeeBundle = new SourceNode(1, 0, 'foo.coffee', 'foo(coffee);\n');300coffeeBundle.setSourceContent('foo.coffee', 'foo coffee');301coffeeBundle.add(new SourceNode(2, 0, '/bar.coffee', 'bar(coffee);\n'));302coffeeBundle.add(new SourceNode(3, 0, 'http://www.example.com/baz.coffee', 'baz(coffee);'));303coffeeBundle = coffeeBundle.toStringWithSourceMap({304file: 'foo.js',305sourceRoot: '..'306});307308var foo = new SourceNode(1, 0, 'foo.js', 'foo(js);');309310var test = function(relativePath, expectedSources) {311var app = new SourceNode();312app.add(SourceNode.fromStringWithSourceMap(313coffeeBundle.code,314new SourceMapConsumer(coffeeBundle.map.toString()),315relativePath));316app.add(foo);317var i = 0;318app.walk(function (chunk, loc) {319assert.equal(loc.source, expectedSources[i]);320i++;321});322app.walkSourceContents(function (sourceFile, sourceContent) {323assert.equal(sourceFile, expectedSources[0]);324assert.equal(sourceContent, 'foo coffee');325})326};327328test('../coffee/maps', [329'../coffee/foo.coffee',330'/bar.coffee',331'http://www.example.com/baz.coffee',332'foo.js'333]);334335// If the third parameter is omitted or set to the current working336// directory we get incorrect source paths:337338test(undefined, [339'../foo.coffee',340'/bar.coffee',341'http://www.example.com/baz.coffee',342'foo.js'343]);344345test('', [346'../foo.coffee',347'/bar.coffee',348'http://www.example.com/baz.coffee',349'foo.js'350]);351352test('.', [353'../foo.coffee',354'/bar.coffee',355'http://www.example.com/baz.coffee',356'foo.js'357]);358359test('./', [360'../foo.coffee',361'/bar.coffee',362'http://www.example.com/baz.coffee',363'foo.js'364]);365};366367exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {368var input = new SourceNode(null, null, null, [369new SourceNode(1, 0, "a.js", "(function"),370new SourceNode(1, 0, "a.js", "() {" + nl),371" ",372new SourceNode(1, 0, "a.js", "var Test = "),373new SourceNode(1, 0, "b.js", "{};" + nl),374new SourceNode(2, 0, "b.js", "Test"),375new SourceNode(2, 0, "b.js", ".A", "A"),376new SourceNode(2, 20, "b.js", " = { value: ", "A"),377"1234",378new SourceNode(2, 40, "b.js", " };" + nl, "A"),379"}());" + nl,380"/* Generated Source */"381]);382input = input.toStringWithSourceMap({383file: 'foo.js'384});385386assert.equal(input.code, [387"(function() {",388" var Test = {};",389"Test.A = { value: 1234 };",390"}());",391"/* Generated Source */"392].join(nl))393394var correctMap = new SourceMapGenerator({395file: 'foo.js'396});397correctMap.addMapping({398generated: { line: 1, column: 0 },399source: 'a.js',400original: { line: 1, column: 0 }401});402// Here is no need for a empty mapping,403// because mappings ends at eol404correctMap.addMapping({405generated: { line: 2, column: 2 },406source: 'a.js',407original: { line: 1, column: 0 }408});409correctMap.addMapping({410generated: { line: 2, column: 13 },411source: 'b.js',412original: { line: 1, column: 0 }413});414correctMap.addMapping({415generated: { line: 3, column: 0 },416source: 'b.js',417original: { line: 2, column: 0 }418});419correctMap.addMapping({420generated: { line: 3, column: 4 },421source: 'b.js',422name: 'A',423original: { line: 2, column: 0 }424});425correctMap.addMapping({426generated: { line: 3, column: 6 },427source: 'b.js',428name: 'A',429original: { line: 2, column: 20 }430});431// This empty mapping is required,432// because there is a hole in the middle of the line433correctMap.addMapping({434generated: { line: 3, column: 18 }435});436correctMap.addMapping({437generated: { line: 3, column: 22 },438source: 'b.js',439name: 'A',440original: { line: 2, column: 40 }441});442// Here is no need for a empty mapping,443// because mappings ends at eol444445var inputMap = input.map.toJSON();446correctMap = correctMap.toJSON();447util.assertEqualMaps(assert, inputMap, correctMap);448});449450exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {451var input = new SourceNode(null, null, null, [452new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),453new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),454new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),455new SourceNode(2, 2, "b.js", "anotherLine();" + nl),456"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,457new SourceNode(3, 4, "c.js", "anotherLine();" + nl),458"/*" + nl + "Generated" + nl + "Source" + nl + "*/"459]);460input = input.toStringWithSourceMap({461file: 'foo.js'462});463464assert.equal(input.code, [465"(function() {",466"var nextLine = 1;",467"anotherLine();",468"Test.call(this, 123);",469"this['stuff'] = 'v';",470"anotherLine();",471"/*",472"Generated",473"Source",474"*/",475"anotherLine();",476"/*",477"Generated",478"Source",479"*/"480].join(nl));481482var correctMap = new SourceMapGenerator({483file: 'foo.js'484});485correctMap.addMapping({486generated: { line: 1, column: 0 },487source: 'a.js',488original: { line: 1, column: 0 }489});490correctMap.addMapping({491generated: { line: 2, column: 0 },492source: 'a.js',493original: { line: 1, column: 0 }494});495correctMap.addMapping({496generated: { line: 3, column: 0 },497source: 'a.js',498original: { line: 1, column: 0 }499});500correctMap.addMapping({501generated: { line: 4, column: 0 },502source: 'b.js',503original: { line: 2, column: 2 }504});505correctMap.addMapping({506generated: { line: 5, column: 0 },507source: 'b.js',508original: { line: 2, column: 2 }509});510correctMap.addMapping({511generated: { line: 6, column: 0 },512source: 'b.js',513original: { line: 2, column: 2 }514});515correctMap.addMapping({516generated: { line: 11, column: 0 },517source: 'c.js',518original: { line: 3, column: 4 }519});520521var inputMap = input.map.toJSON();522correctMap = correctMap.toJSON();523util.assertEqualMaps(assert, inputMap, correctMap);524});525526exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {527var node = new SourceNode(1, 0, 'empty.js', '');528var result = node.toStringWithSourceMap();529assert.equal(result.code, '');530};531532exports['test .toStringWithSourceMap() with consecutive newlines'] = forEachNewline(function (assert, util, nl) {533var input = new SourceNode(null, null, null, [534"/***/" + nl + nl,535new SourceNode(1, 0, "a.js", "'use strict';" + nl),536new SourceNode(2, 0, "a.js", "a();"),537]);538input = input.toStringWithSourceMap({539file: 'foo.js'540});541542assert.equal(input.code, [543"/***/",544"",545"'use strict';",546"a();",547].join(nl));548549var correctMap = new SourceMapGenerator({550file: 'foo.js'551});552correctMap.addMapping({553generated: { line: 3, column: 0 },554source: 'a.js',555original: { line: 1, column: 0 }556});557correctMap.addMapping({558generated: { line: 4, column: 0 },559source: 'a.js',560original: { line: 2, column: 0 }561});562563var inputMap = input.map.toJSON();564correctMap = correctMap.toJSON();565util.assertEqualMaps(assert, inputMap, correctMap);566});567568exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {569var aNode = new SourceNode(1, 1, 'a.js', 'a');570aNode.setSourceContent('a.js', 'someContent');571var node = new SourceNode(null, null, null,572['(function () {\n',573' ', aNode,574' ', new SourceNode(1, 1, 'b.js', 'b'),575'}());']);576node.setSourceContent('b.js', 'otherContent');577var map = node.toStringWithSourceMap({578file: 'foo.js'579}).map;580581assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');582map = new SourceMapConsumer(map.toString());583584assert.equal(map.sources.length, 2);585assert.equal(map.sources[0], 'a.js');586assert.equal(map.sources[1], 'b.js');587assert.equal(map.sourcesContent.length, 2);588assert.equal(map.sourcesContent[0], 'someContent');589assert.equal(map.sourcesContent[1], 'otherContent');590};591592exports['test walkSourceContents'] = function (assert, util) {593var aNode = new SourceNode(1, 1, 'a.js', 'a');594aNode.setSourceContent('a.js', 'someContent');595var node = new SourceNode(null, null, null,596['(function () {\n',597' ', aNode,598' ', new SourceNode(1, 1, 'b.js', 'b'),599'}());']);600node.setSourceContent('b.js', 'otherContent');601var results = [];602node.walkSourceContents(function (sourceFile, sourceContent) {603results.push([sourceFile, sourceContent]);604});605assert.equal(results.length, 2);606assert.equal(results[0][0], 'a.js');607assert.equal(results[0][1], 'someContent');608assert.equal(results[1][0], 'b.js');609assert.equal(results[1][1], 'otherContent');610};611});612613614