react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / 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;1415exports['test .add()'] = function (assert, util) {16var node = new SourceNode(null, null, null);1718// Adding a string works.19node.add('function noop() {}');2021// Adding another source node works.22node.add(new SourceNode(null, null, null));2324// Adding an array works.25node.add(['function foo() {',26new SourceNode(null, null, null,27'return 10;'),28'}']);2930// Adding other stuff doesn't.31assert.throws(function () {32node.add({});33});34assert.throws(function () {35node.add(function () {});36});37};3839exports['test .prepend()'] = function (assert, util) {40var node = new SourceNode(null, null, null);4142// Prepending a string works.43node.prepend('function noop() {}');44assert.equal(node.children[0], 'function noop() {}');45assert.equal(node.children.length, 1);4647// Prepending another source node works.48node.prepend(new SourceNode(null, null, null));49assert.equal(node.children[0], '');50assert.equal(node.children[1], 'function noop() {}');51assert.equal(node.children.length, 2);5253// Prepending an array works.54node.prepend(['function foo() {',55new SourceNode(null, null, null,56'return 10;'),57'}']);58assert.equal(node.children[0], 'function foo() {');59assert.equal(node.children[1], 'return 10;');60assert.equal(node.children[2], '}');61assert.equal(node.children[3], '');62assert.equal(node.children[4], 'function noop() {}');63assert.equal(node.children.length, 5);6465// Prepending other stuff doesn't.66assert.throws(function () {67node.prepend({});68});69assert.throws(function () {70node.prepend(function () {});71});72};7374exports['test .toString()'] = function (assert, util) {75assert.equal((new SourceNode(null, null, null,76['function foo() {',77new SourceNode(null, null, null, 'return 10;'),78'}'])).toString(),79'function foo() {return 10;}');80};8182exports['test .join()'] = function (assert, util) {83assert.equal((new SourceNode(null, null, null,84['a', 'b', 'c', 'd'])).join(', ').toString(),85'a, b, c, d');86};8788exports['test .walk()'] = function (assert, util) {89var node = new SourceNode(null, null, null,90['(function () {\n',91' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',92' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',93'}());']);94var expected = [95{ str: '(function () {\n', source: null, line: null, column: null },96{ str: ' ', source: null, line: null, column: null },97{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },98{ str: ';\n', source: null, line: null, column: null },99{ str: ' ', source: null, line: null, column: null },100{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },101{ str: ';\n', source: null, line: null, column: null },102{ str: '}());', source: null, line: null, column: null },103];104var i = 0;105node.walk(function (chunk, loc) {106assert.equal(expected[i].str, chunk);107assert.equal(expected[i].source, loc.source);108assert.equal(expected[i].line, loc.line);109assert.equal(expected[i].column, loc.column);110i++;111});112};113114exports['test .replaceRight'] = function (assert, util) {115var node;116117// Not nested118node = new SourceNode(null, null, null, 'hello world');119node.replaceRight(/world/, 'universe');120assert.equal(node.toString(), 'hello universe');121122// Nested123node = new SourceNode(null, null, null,124[new SourceNode(null, null, null, 'hey sexy mama, '),125new SourceNode(null, null, null, 'want to kill all humans?')]);126node.replaceRight(/kill all humans/, 'watch Futurama');127assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');128};129130exports['test .toStringWithSourceMap()'] = function (assert, util) {131var node = new SourceNode(null, null, null,132['(function () {\n',133' ',134new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),135new SourceNode(1, 8, 'a.js', '()'),136';\n',137' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',138'}());']);139var map = node.toStringWithSourceMap({140file: 'foo.js'141}).map;142143assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');144map = new SourceMapConsumer(map.toString());145146var actual;147148actual = map.originalPositionFor({149line: 1,150column: 4151});152assert.equal(actual.source, null);153assert.equal(actual.line, null);154assert.equal(actual.column, null);155156actual = map.originalPositionFor({157line: 2,158column: 2159});160assert.equal(actual.source, 'a.js');161assert.equal(actual.line, 1);162assert.equal(actual.column, 0);163assert.equal(actual.name, 'originalCall');164165actual = map.originalPositionFor({166line: 3,167column: 2168});169assert.equal(actual.source, 'b.js');170assert.equal(actual.line, 2);171assert.equal(actual.column, 0);172173actual = map.originalPositionFor({174line: 3,175column: 16176});177assert.equal(actual.source, null);178assert.equal(actual.line, null);179assert.equal(actual.column, null);180181actual = map.originalPositionFor({182line: 4,183column: 2184});185assert.equal(actual.source, null);186assert.equal(actual.line, null);187assert.equal(actual.column, null);188};189190exports['test .fromStringWithSourceMap()'] = function (assert, util) {191var node = SourceNode.fromStringWithSourceMap(192util.testGeneratedCode,193new SourceMapConsumer(util.testMap));194195var result = node.toStringWithSourceMap({196file: 'min.js'197});198var map = result.map;199var code = result.code;200201assert.equal(code, util.testGeneratedCode);202assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');203map = map.toJSON();204assert.equal(map.version, util.testMap.version);205assert.equal(map.file, util.testMap.file);206assert.equal(map.mappings, util.testMap.mappings);207};208209exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) {210var node = SourceNode.fromStringWithSourceMap(211util.testGeneratedCode,212new SourceMapConsumer(util.emptyMap));213var result = node.toStringWithSourceMap({214file: 'min.js'215});216var map = result.map;217var code = result.code;218219assert.equal(code, util.testGeneratedCode);220assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');221map = map.toJSON();222assert.equal(map.version, util.emptyMap.version);223assert.equal(map.file, util.emptyMap.file);224assert.equal(map.mappings.length, util.emptyMap.mappings.length);225assert.equal(map.mappings, util.emptyMap.mappings);226};227228exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) {229var input = new SourceNode(null, null, null, [230"(function() {\n",231" var Test = {};\n",232" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"),233" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n",234"}());\n",235"/* Generated Source */"]);236input = input.toStringWithSourceMap({237file: 'foo.js'238});239240var node = SourceNode.fromStringWithSourceMap(241input.code,242new SourceMapConsumer(input.map.toString()));243244var result = node.toStringWithSourceMap({245file: 'foo.js'246});247var map = result.map;248var code = result.code;249250assert.equal(code, input.code);251assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');252map = map.toJSON();253var inputMap = input.map.toJSON();254util.assertEqualMaps(assert, map, inputMap);255};256257exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) {258var input = new SourceNode(null, null, null, [259new SourceNode(1, 0, "a.js", "(function"),260new SourceNode(1, 0, "a.js", "() {\n"),261" ",262new SourceNode(1, 0, "a.js", "var Test = "),263new SourceNode(1, 0, "b.js", "{};\n"),264new SourceNode(2, 0, "b.js", "Test"),265new SourceNode(2, 0, "b.js", ".A", "A"),266new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"),267"}());\n",268"/* Generated Source */"269]);270input = input.toStringWithSourceMap({271file: 'foo.js'272});273274var correctMap = new SourceMapGenerator({275file: 'foo.js'276});277correctMap.addMapping({278generated: { line: 1, column: 0 },279source: 'a.js',280original: { line: 1, column: 0 }281});282correctMap.addMapping({283generated: { line: 2, column: 0 }284});285correctMap.addMapping({286generated: { line: 2, column: 2 },287source: 'a.js',288original: { line: 1, column: 0 }289});290correctMap.addMapping({291generated: { line: 2, column: 13 },292source: 'b.js',293original: { line: 1, column: 0 }294});295correctMap.addMapping({296generated: { line: 3, column: 0 },297source: 'b.js',298original: { line: 2, column: 0 }299});300correctMap.addMapping({301generated: { line: 3, column: 4 },302source: 'b.js',303name: 'A',304original: { line: 2, column: 0 }305});306correctMap.addMapping({307generated: { line: 3, column: 6 },308source: 'b.js',309name: 'A',310original: { line: 2, column: 20 }311});312correctMap.addMapping({313generated: { line: 4, column: 0 }314});315316var inputMap = input.map.toJSON();317correctMap = correctMap.toJSON();318util.assertEqualMaps(assert, correctMap, inputMap);319};320321exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {322var aNode = new SourceNode(1, 1, 'a.js', 'a');323aNode.setSourceContent('a.js', 'someContent');324var node = new SourceNode(null, null, null,325['(function () {\n',326' ', aNode,327' ', new SourceNode(1, 1, 'b.js', 'b'),328'}());']);329node.setSourceContent('b.js', 'otherContent');330var map = node.toStringWithSourceMap({331file: 'foo.js'332}).map;333334assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');335map = new SourceMapConsumer(map.toString());336337assert.equal(map.sources.length, 2);338assert.equal(map.sources[0], 'a.js');339assert.equal(map.sources[1], 'b.js');340assert.equal(map.sourcesContent.length, 2);341assert.equal(map.sourcesContent[0], 'someContent');342assert.equal(map.sourcesContent[1], 'otherContent');343};344345exports['test walkSourceContents'] = function (assert, util) {346var aNode = new SourceNode(1, 1, 'a.js', 'a');347aNode.setSourceContent('a.js', 'someContent');348var node = new SourceNode(null, null, null,349['(function () {\n',350' ', aNode,351' ', new SourceNode(1, 1, 'b.js', 'b'),352'}());']);353node.setSourceContent('b.js', 'otherContent');354var results = [];355node.walkSourceContents(function (sourceFile, sourceContent) {356results.push([sourceFile, sourceContent]);357});358assert.equal(results.length, 2);359assert.equal(results[0][0], 'a.js');360assert.equal(results[0][1], 'someContent');361assert.equal(results[1][0], 'b.js');362assert.equal(results[1][1], 'otherContent');363};364});365366367