react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / node_modules / source-map / test / source-map / test-source-node.js
80766 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');161mapWithoutOptions._file = 'foo.js';162util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());163164map = new SourceMapConsumer(map.toString());165166var actual;167168actual = map.originalPositionFor({169line: 1,170column: 4171});172assert.equal(actual.source, null);173assert.equal(actual.line, null);174assert.equal(actual.column, null);175176actual = map.originalPositionFor({177line: 2,178column: 2179});180assert.equal(actual.source, 'a.js');181assert.equal(actual.line, 1);182assert.equal(actual.column, 0);183assert.equal(actual.name, 'originalCall');184185actual = map.originalPositionFor({186line: 3,187column: 2188});189assert.equal(actual.source, 'b.js');190assert.equal(actual.line, 2);191assert.equal(actual.column, 0);192193actual = map.originalPositionFor({194line: 3,195column: 16196});197assert.equal(actual.source, null);198assert.equal(actual.line, null);199assert.equal(actual.column, null);200201actual = map.originalPositionFor({202line: 4,203column: 2204});205assert.equal(actual.source, null);206assert.equal(actual.line, null);207assert.equal(actual.column, null);208});209210exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {211var testCode = util.testGeneratedCode.replace(/\n/g, nl);212var node = SourceNode.fromStringWithSourceMap(213testCode,214new SourceMapConsumer(util.testMap));215216var result = node.toStringWithSourceMap({217file: 'min.js'218});219var map = result.map;220var code = result.code;221222assert.equal(code, testCode);223assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');224map = map.toJSON();225assert.equal(map.version, util.testMap.version);226assert.equal(map.file, util.testMap.file);227assert.equal(map.mappings, util.testMap.mappings);228});229230exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {231var node = SourceNode.fromStringWithSourceMap(232util.testGeneratedCode.replace(/\n/g, nl),233new SourceMapConsumer(util.emptyMap));234var result = node.toStringWithSourceMap({235file: 'min.js'236});237var map = result.map;238var code = result.code;239240assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));241assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');242map = map.toJSON();243assert.equal(map.version, util.emptyMap.version);244assert.equal(map.file, util.emptyMap.file);245assert.equal(map.mappings.length, util.emptyMap.mappings.length);246assert.equal(map.mappings, util.emptyMap.mappings);247});248249exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {250var input = new SourceNode(null, null, null, [251"(function() {" + nl,252" var Test = {};" + nl,253" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),254" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,255"}());" + nl,256"/* Generated Source */"]);257input = input.toStringWithSourceMap({258file: 'foo.js'259});260261var node = SourceNode.fromStringWithSourceMap(262input.code,263new SourceMapConsumer(input.map.toString()));264265var result = node.toStringWithSourceMap({266file: 'foo.js'267});268var map = result.map;269var code = result.code;270271assert.equal(code, input.code);272assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');273map = map.toJSON();274var inputMap = input.map.toJSON();275util.assertEqualMaps(assert, map, inputMap);276});277278exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {279var input = new SourceNode(null, null, null, [280new SourceNode(1, 0, "a.js", "(function"),281new SourceNode(1, 0, "a.js", "() {" + nl),282" ",283new SourceNode(1, 0, "a.js", "var Test = "),284new SourceNode(1, 0, "b.js", "{};" + nl),285new SourceNode(2, 0, "b.js", "Test"),286new SourceNode(2, 0, "b.js", ".A", "A"),287new SourceNode(2, 20, "b.js", " = { value: ", "A"),288"1234",289new SourceNode(2, 40, "b.js", " };" + nl, "A"),290"}());" + nl,291"/* Generated Source */"292]);293input = input.toStringWithSourceMap({294file: 'foo.js'295});296297assert.equal(input.code, [298"(function() {",299" var Test = {};",300"Test.A = { value: 1234 };",301"}());",302"/* Generated Source */"303].join(nl))304305var correctMap = new SourceMapGenerator({306file: 'foo.js'307});308correctMap.addMapping({309generated: { line: 1, column: 0 },310source: 'a.js',311original: { line: 1, column: 0 }312});313// Here is no need for a empty mapping,314// because mappings ends at eol315correctMap.addMapping({316generated: { line: 2, column: 2 },317source: 'a.js',318original: { line: 1, column: 0 }319});320correctMap.addMapping({321generated: { line: 2, column: 13 },322source: 'b.js',323original: { line: 1, column: 0 }324});325correctMap.addMapping({326generated: { line: 3, column: 0 },327source: 'b.js',328original: { line: 2, column: 0 }329});330correctMap.addMapping({331generated: { line: 3, column: 4 },332source: 'b.js',333name: 'A',334original: { line: 2, column: 0 }335});336correctMap.addMapping({337generated: { line: 3, column: 6 },338source: 'b.js',339name: 'A',340original: { line: 2, column: 20 }341});342// This empty mapping is required,343// because there is a hole in the middle of the line344correctMap.addMapping({345generated: { line: 3, column: 18 }346});347correctMap.addMapping({348generated: { line: 3, column: 22 },349source: 'b.js',350name: 'A',351original: { line: 2, column: 40 }352});353// Here is no need for a empty mapping,354// because mappings ends at eol355356var inputMap = input.map.toJSON();357correctMap = correctMap.toJSON();358util.assertEqualMaps(assert, inputMap, correctMap);359});360361exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {362var input = new SourceNode(null, null, null, [363new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),364new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),365new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),366new SourceNode(2, 2, "b.js", "anotherLine();" + nl),367"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,368new SourceNode(3, 4, "c.js", "anotherLine();" + nl),369"/*" + nl + "Generated" + nl + "Source" + nl + "*/"370]);371input = input.toStringWithSourceMap({372file: 'foo.js'373});374375assert.equal(input.code, [376"(function() {",377"var nextLine = 1;",378"anotherLine();",379"Test.call(this, 123);",380"this['stuff'] = 'v';",381"anotherLine();",382"/*",383"Generated",384"Source",385"*/",386"anotherLine();",387"/*",388"Generated",389"Source",390"*/"391].join(nl));392393var correctMap = new SourceMapGenerator({394file: 'foo.js'395});396correctMap.addMapping({397generated: { line: 1, column: 0 },398source: 'a.js',399original: { line: 1, column: 0 }400});401correctMap.addMapping({402generated: { line: 2, column: 0 },403source: 'a.js',404original: { line: 1, column: 0 }405});406correctMap.addMapping({407generated: { line: 3, column: 0 },408source: 'a.js',409original: { line: 1, column: 0 }410});411correctMap.addMapping({412generated: { line: 4, column: 0 },413source: 'b.js',414original: { line: 2, column: 2 }415});416correctMap.addMapping({417generated: { line: 5, column: 0 },418source: 'b.js',419original: { line: 2, column: 2 }420});421correctMap.addMapping({422generated: { line: 6, column: 0 },423source: 'b.js',424original: { line: 2, column: 2 }425});426correctMap.addMapping({427generated: { line: 11, column: 0 },428source: 'c.js',429original: { line: 3, column: 4 }430});431432var inputMap = input.map.toJSON();433correctMap = correctMap.toJSON();434util.assertEqualMaps(assert, inputMap, correctMap);435});436437exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {438var node = new SourceNode(1, 0, 'empty.js', '');439var result = node.toStringWithSourceMap();440assert.equal(result.code, '');441};442443exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {444var aNode = new SourceNode(1, 1, 'a.js', 'a');445aNode.setSourceContent('a.js', 'someContent');446var node = new SourceNode(null, null, null,447['(function () {\n',448' ', aNode,449' ', new SourceNode(1, 1, 'b.js', 'b'),450'}());']);451node.setSourceContent('b.js', 'otherContent');452var map = node.toStringWithSourceMap({453file: 'foo.js'454}).map;455456assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');457map = new SourceMapConsumer(map.toString());458459assert.equal(map.sources.length, 2);460assert.equal(map.sources[0], 'a.js');461assert.equal(map.sources[1], 'b.js');462assert.equal(map.sourcesContent.length, 2);463assert.equal(map.sourcesContent[0], 'someContent');464assert.equal(map.sourcesContent[1], 'otherContent');465};466467exports['test walkSourceContents'] = function (assert, util) {468var aNode = new SourceNode(1, 1, 'a.js', 'a');469aNode.setSourceContent('a.js', 'someContent');470var node = new SourceNode(null, null, null,471['(function () {\n',472' ', aNode,473' ', new SourceNode(1, 1, 'b.js', 'b'),474'}());']);475node.setSourceContent('b.js', 'otherContent');476var results = [];477node.walkSourceContents(function (sourceFile, sourceContent) {478results.push([sourceFile, sourceContent]);479});480assert.equal(results.length, 2);481assert.equal(results[0][0], 'a.js');482assert.equal(results[0][1], 'someContent');483assert.equal(results[1][0], 'b.js');484assert.equal(results[1][1], 'otherContent');485};486});487488489