Files
palemoon27/devtools/shared/sourcemap/tests/unit/test_source_node.js
T
roytam1 4be8027b21 (WIP) import changes from `dev' branch of rmottola/Arctic-Fox:
- adapted version of Bug 912121 - Migrate major DevTools directories (8ff521cb21)
- Bug 1187831 - Refactor the highlighters into separate modules; r=pbrosset (fdca9d5f16)
- Bug 1157906 - Can't return arrays as a root response, fixes inspect button. r=bgrins (f684642158)
- Bug 912121 - Adjust build configs and test manifests. r=glandium (304066d667)
- Bug 912121 - Package DevTools client content in devtools.jar. rs=devtools (967caca3e0)
- Bug 1074180 - Convert touch simulator to frame script for e10s. r=ochameau (be9559c94c)
- Bug 1178851 - Move testing flag to DevToolsUtils. r=ochameau (9eb0bb908f)
- Bug 1158634 - Modify font-inspector tests for better code sharing. r=pbrosset (6e5f42e1fc)
- Bug 859058 - Export content of the Network panel as HAR; r=jsantell, r=jlongster (629f4c8f63)
- fix some module paths and Telemetry init (ed5131d7e1)
- Bug 1167945 - Remove spidermonkey specific JS from canvasdebugger; r=jryans (4865569865)
- Bug 1188401 - Expose require out of Loader.jsm and use it. r=jryans (464953c07a)
- Bug 1175760 - Set recording configurations based on UI prefs, but also target support overrides UI prefs. r=vp (137e7940db)
- Bug 1172180 - Create a PerformanceActor, and migrate existing pseudo PerformanceFront to a legacy front. r=vp (0fa20d2d20)
- Bug 1172180 - Add polling events for profiler/buffer status on the profiler itself. r=vp (ef4bb72f73)
- Bug 1172180 - part 2 - add ability to auto drain allocation logs in memory component via a timer and on GC. r=fitzgen (46ae8dd7eb)
- Bug 1190857 - Add loader alias to DevTools loader exports. r=jryans (fbe9e189b0)
- Bug 1195825 - Replace Cu.import(Promise.jsm) by require(promise) in devtools. r=jryans (be39379f9a)
- Bug 1153305 - change css-tokenizer.js to use CSSLexer r=pbrosset (584a825504)
- Bug 1178535 - Part 1: Parse the pseudoclasses and attributes from the selector text r=tromney (9d761b2ecf)
- Bug 1173298 - Disable add rule button for non-element nodes and anonymous elements. r=bgrins (1c60a6580c)
- move themes from toolkit to devtools (97989c5768)
- Bug 1190452 - Always import toolkit/devtools/server/main.js as CommonJS module. r=jryans (4b651f7076)
- Bug 1192863 - Use client/main.js instead of dbg-server.jsm. r=jryans (becdb5489e)
- minor cleanup (ba24674685)
- Bug 999568 - Bring devtools window to front on F12. r=pbrosset (8e945fb777)
- move devtool locales (52ddf02403)
- Bug 912121 - Adjust ESLint files. r=pbrosset (59724e31e3)
- Bug 1204182 - Use a single jar manifest for shared theme resources. r=dao (8b396bafe1)
- fix double entry (e64c5bb511)
- Bug 1077339 - Display keyboard shortcuts when hovering panel tabs. r=janx f=bgrins (1422fe5127)
- Bug 912121 - Rewrite require / import to match source tree. rs=devtools (09c4767e9f)
- Bug 1193313 - Remove projecteditor promise alias in favor of require(promise). r=bgrins (8ed93e1a8f)
- Bug 1193313 - Cleanup promises. r=jryans (38508b18ea)
- Bug 1193313 - Make gcli use DOM Promise. r=jwalker (405e49ec4e)
- Bug 1160361 - Skip Telemetry calls for non-Firefox. r=bgrins (942761962e)
- Bug 1189386 - Paint Flashing button on Developers Toolbar is checked on new tabs. r=pbrosset (d2cd46a7f2)
- Bug 1197394 - GCLI's number-type doesn't ignore min or max-values of 0. r=jwalker (5bda36049b)
- Bug 1046049 - Fix :w for saving in webide. r=jryans (8b353db8ca)
- Bug 1148086 - Style Editor: Don't jump to the first line unconditionally when an editor is shown. r=bgrins (25cd83f12f)
- Bug 1049199 - Style editor should show XBL stylesheets. r=pbrosset Bug 1148770 - Wait for asynchronous operations to complete before resolving in StyleSheetEditor.fetchSource. r=ejpbruel (0d3f4ff86f)
- Bug 1148770 - Don't load CodeMirror editor if StyleSheetEditor is destroyed before load is called. r=bgrins (5a665dca6e)
- Bug 999299 - Remove '[]' from the autoCloseBrackets config for Style Editor sourceeditor;r=mratcliffe (58aaf86e1d)
- Bug 1177891 - Introduce redux-style UI architecture in the debugger and refactor event listeners to use it. r=fitzgen Bug 1204173 - Replace Fluxify with Redux. r=jlongster (145adb12e4)
- Bug 1176981 - Part 0: Remove the tracing debugger UI; r=vporof (92d7fb4f3d)
- Bug 1176981 - Part 1: Remove the tracing debugger actor from the server; r=vporof (6548c82f8e)
- Bug 1176981 - Part 3: Actually remove the tracing debugger pref; r=vporof (731656b547)
- Bug 912121 - Create shims for popular DevTools modules in add-ons. rs=devtools (4635ccf492)
- partial of Bug 912121 - Update misc. DevTools paths and comments. rs=devtools (3b00d87f89)
- Bug 1189128 - Rename devtools .inc.css files that are no longer included to .css;r=jryans (b0fffb3be1)
- Bug 912121 - Misc. DevTools test fixes after migration. rs=devtools (6285f7ccd3)
- Bug 912121 - Package DevTools client themes in devtools.jar. rs=devtools (697be51a28)
- Bug 912121 - Create shims for popular DevTools themes in add-ons. rs=devtools (903c0e721e)
- Bug 912121 - Migrate DevTools themes. rs=devtools (a64b30b44f)
- Bug 1201597 - Part 0: Make saveHeapSnapshot return the file path rather than take it as a parameter; r=bholley (85652707da)
- Bug 1200446 - Add a method for saving heap snapshots to MemoryActor; r=jryans (112af304d1)
- Bug 1201213 - Add a HeapAnalyses{Worker,Client} for running heap analyses; r=jsantell (3e43273721)
- Bug 1201597 - Part 1: Implement an nsIOutputStream for ipc::FileDescriptor; r=mrbkap (e659f8514d)
- Bug 1201597 - Part 2: Add an IPDL subprotocol for opening core dump files to (f9c9b2b747)
- missing bit of Bug 1188401 - Expose require out of Loader.jsm (c7174fce60)
- Bug 1191916 - only enable step in/over/out buttons if actually paused at a location r=bgrins Bug 789430 - Pause on next bytecode instead of immediately;r=fitzgen (d51da724b3)
- Bug 1201700 - Create skeleton memory tool. r=fitzgen (4217e71fd4)
- missing bit of Bug 1178851 (1804598f62)
- add missing bit (c72fbab8a6)
- fix (2ea8917252)
- Bug 1182194 - Remove DevToolsUtils.jsm (in favor of its module). r=jwalker (16cd60ce9a)
- Bug 1182194 - Convert LayoutHelpers.jsm into a commonjs module. r=jwalker Bug 1132475 - Refactor LayoutHelpers.jsm to avoid dependencies. r=pbrosset (5409fcdaac)
- Bug 935366 - Remove unused Require.jsm. r=fitzgen (20ce6ce7f4)
- remove unbuild profiler and timeline, but build shims (c0d9a9048a)
- Bug 1169135 - Make Graphs.jsm a require module, r=jsantell (8bd010851b)
- Bug 1137487 - Make tests wait for the canvas graph's destruction to finish before cleaning up, r=me (ba472b7c63)
- Bug 1151246 - flame graph header should render above the flame graph. r=vp (6f0ca96884)
- Bug 1066504 - Allow timeline range selection to continue even after mouseout of the graph window. r=vporof Bug 1145784 - Bind graph mouse movement to the top level window;r=vporof (7556cd2cf7)
- Bug 1141719 - Lots of "TypeError: ownerWindow is null" when shutting down the new perf tool on debug builds, r=jsantell (12761630dc)
- Bug 1175686 - Pull line and bar graph widgets outside of Graphs.js into their own files, r=jsantell, a=Mossop (e6e895604a)
- Bug 1174264 - Stacked Mountain Chart Widget, r=jsantell, a=Mossop (c1ed5dfed1)
- Bug 980006 - Add MDN tooltips for CSS properties. r=pbrosset (dc7102fa19)
- Bug 1159109 - The MDN tooltip should be controlled by a pref. r=pbrosset (95da8e51a5)
- missing bits of Bug 1182194 - Use DevToolsUtils module instead of JSM. (355c4e2f80)
- Bug 1141615 - Use before declaration for scaledStep in FlameGraph.jsm, r=jsantell (993c41aae5)
- Bug 1145784 - Use testX/testY for coordinates during tests for the Graph and FlameGraph;r=vporof (a004e9c7f0)
- Bug 1090950 - Properly bound fillRect call for Graphs with selections that start or end out of bounds. r=vporof (3fbf750cbd)
- Bug 1164327 - Make resizing a graph maintain the drawn selection. r=vporof (147485835b)
- Don't dump errors in tests that are already handled by the parser (bug 1138784). r=vporof (e0671fd8b3)
- Bug 913617 - Reflect.parse: rename ArrowExpression to ArrowFunctionExpression. r=jorendorff (78eb0f718b)
- Bug 1008380 - Can't remove a breakpoint after it slides to a new line;r=jlong (4fd797cf9a)
- Bug 1131756 - properly display anonymous sources when they are needed in the debugger r=ejpbruel Bug 1183895 - Part 2: Add a toolbar button for toggling the promise debugger r=fitzgen (5a01328a1e)
- Bug 1183895 - Part 1: Add a new pane for the promise debugger r=fitzgen (20a1f49b59)
- Bug 1179977 - Vertical layout for JSDebugger in small window host. r=jlong (4bb82acdb6)
- some profiler/performance leftover (921b05d184)
- Bug 1155553 - Fixes animationinspector regression with actor-capabilities detection; r=bgrins (12095c1afc)
- Bug 1169563 - 1 - Minor ESLint code cleanup; r=ednapiranha (c6c808ca1b)
- Bug 1169563 - 2 - Add a scrubber element to timeline animation UI; r=zer0 (44caa900cd)
- Bug 1169563 - 3 - Move the scrubber in the animation timeline to set the time; r=tromey (71ee83e303)
- Bug 1169563 - 5 - Resize animations in the timeline UI to show their rates; r=miker (63709f41b6)
- Bug 1169563 - 6 - Animate the scrubber when animations are playing; r=miker (3f63035572)
- Bug 1174060 - 4 - Minor eslint changes to animation-inspector; r=ednapiranha (42c3242e87)
- missing eslint stuff (c4ad512ec9)
- Bug 1090478 - Add a keyboard shortcut for the Browser Toolbox. r=jryans (f5ec8fce1d)
- Bug 1135635 - Position sidebar allTabs menu absolutely to avoid oveflow jittering; r=bgrins (7339c7344a)
- Bug 1182083 - Don't hide tabpanels in the DevTools sidebar. r=vporof (9ed0784990)
- some telemetry stuff (3844f60f89)
- Bug 1172010 - Fix tools reload gcli command. r=jwalker (c3c2ab47ae)
- Bug 1107656 - Generate unique prefixes for e10s parent-child server connections; r=ochameau (da59ead659)
- fix image paths (085de78f74)
- Bug 116953 - 4 - Move traits out of controller; r=tromey (80e5d0ba8d)
- Bug 1174060 - 1 - Fix auto-removed animations that have the same name but different targets; r=miker (0ac111d790)
- Bug 1174060 - 2 - Display negative delays like normal delays; r=bgrins (530b611f97)
- Bug 1174060 - 5 - Reverts typo introduced by bug 1180134 that overrided the background-image; r=tromey (104def3b4d)
- Bug 1180134 - Color code animations and transitions so they look different; r=bgrins (fdfa5b131c)
- Bug 1174060 - 6 - Tests for how delays are displayed in the timeline; r=ochameau (26422f6438)
-  Bu 1155661 - 1 - Rename toolbar to global-toolbar; r=zer0 (ccf6e8c304)
- Bug 1155661 - 2 - Add a new timeline toolbar shown only when the timeline is displayed and non empty; r=zer0 (1a4de3a552)
- Bug 1202443 - Prevent pushing the timeline header up when there are many animations; r=zer0 (7eb9645dd8)
- fix some paths (fd03ff798b)
- Bug 1155661 - 3 - Add a way to play/pause the current animations at the same time; r=miker (582c82e3a9)
- Bug 1156757 - Turn ON the animation inspector UI v3 by default and remove v2; r=past (0093d6e0cc)
- Bug 1155663 - More tests for the timeline-based animation inspector UI; r=bgrins (5138da1bf1)
- Bug 1155661 - 4 - Add the timeline play/pause button; r=bgrins (2af3664fdd)
- Bug 1155661 - 5 - Make the timeline emit data about its current state and update the play/pause button based on this; r=miker (2b56df070d)
- Bug 1155661 - 6 - Implement the behavior behind the timeline play/pause button; r=miker (783e49d1ae)
- Bug 1173761 - Display each animation's time data like duration in a tooltip; r=miker (312b319c21)
- Bug 1155651 - Return all players below a node in getAnimationPlayersForNode; r=miker (38c6e3b365)
- Bug 1170806 - Fix GeneratedLocation documentation comment DONTBUILD; r=me (3f1f605e15)
- Bug 1150814 - Rulers graduations are not displayed. r=mratcliffe (48b79dae20)
- Bug 938188 - Make highlighter capable of highlighting only one region, fading out others. r=bgrins (ceb4211e87)
- Bug 1151956 - Add tooltips with refs to source css rules in the layout-view; r=miker (b72cab14da)
- Bug 1167617 - Remove SpiderMonkey specific JS syntax from inspector; r=jryans (9c95101e22)
- Bug 1149346 - First word in selector-search also matches classes and ids; r=harth (82159de025)
- Bug 1196776 - Fix dynamic actors in browser content toolbox. r=jryans (3370eef446)
- Bug 1174821 - 1 - eslint cleanup of view.js; r=janx (fa3ecba9a2)
- Bug 1174821 - 2 - Show only 1 box-model region on hover; r=janx (ad1ddab34a)
- Bug 1174821 - 3 - Consolidate and cleanup layoutview's css files; r=janx (8baa3fd80b)
- Bug 1174821 - 4 - Refactor layoutview's dimming; r=janx (37d5849be5)
- Bug 1174821 - 5 - More eslint cleanup of view.js; r=janx (82200173a0)
- Bug 1173373 - Change DevTools tab tooltips from Cmd+Alt* to Cmd+Opt+*;r=janx (23f9900033)
- Bug 1138591 - Remove the chrome-enabled pref. r=past (8bfd0a8d14)
- Bug 1195162 - Remove unused pref layout.debug.enable_data_xbl (r=sicking) (04c4996c90)
- fix misspatch (975349f11b)
- fix misspatch (c9ef2c63de)
2022-07-05 22:25:33 +08:00

621 lines
20 KiB
JavaScript

/*
* WARNING!
*
* Do not edit this file directly, it is built from the sources at
* https://github.com/mozilla/source-map/
*/
Components.utils.import('resource://test/Utils.jsm');
/* -*- Mode: js; js-indent-level: 2; -*- */
/*
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/
define("test/source-map/test-source-node", ["require", "exports", "module"], function (require, exports, module) {
var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
var SourceNode = require('source-map/source-node').SourceNode;
function forEachNewline(fn) {
return function (assert, util) {
['\n', '\r\n'].forEach(fn.bind(null, assert, util));
}
}
exports['test .add()'] = function (assert, util) {
var node = new SourceNode(null, null, null);
// Adding a string works.
node.add('function noop() {}');
// Adding another source node works.
node.add(new SourceNode(null, null, null));
// Adding an array works.
node.add(['function foo() {',
new SourceNode(null, null, null,
'return 10;'),
'}']);
// Adding other stuff doesn't.
assert.throws(function () {
node.add({});
});
assert.throws(function () {
node.add(function () {});
});
};
exports['test .prepend()'] = function (assert, util) {
var node = new SourceNode(null, null, null);
// Prepending a string works.
node.prepend('function noop() {}');
assert.equal(node.children[0], 'function noop() {}');
assert.equal(node.children.length, 1);
// Prepending another source node works.
node.prepend(new SourceNode(null, null, null));
assert.equal(node.children[0], '');
assert.equal(node.children[1], 'function noop() {}');
assert.equal(node.children.length, 2);
// Prepending an array works.
node.prepend(['function foo() {',
new SourceNode(null, null, null,
'return 10;'),
'}']);
assert.equal(node.children[0], 'function foo() {');
assert.equal(node.children[1], 'return 10;');
assert.equal(node.children[2], '}');
assert.equal(node.children[3], '');
assert.equal(node.children[4], 'function noop() {}');
assert.equal(node.children.length, 5);
// Prepending other stuff doesn't.
assert.throws(function () {
node.prepend({});
});
assert.throws(function () {
node.prepend(function () {});
});
};
exports['test .toString()'] = function (assert, util) {
assert.equal((new SourceNode(null, null, null,
['function foo() {',
new SourceNode(null, null, null, 'return 10;'),
'}'])).toString(),
'function foo() {return 10;}');
};
exports['test .join()'] = function (assert, util) {
assert.equal((new SourceNode(null, null, null,
['a', 'b', 'c', 'd'])).join(', ').toString(),
'a, b, c, d');
};
exports['test .walk()'] = function (assert, util) {
var node = new SourceNode(null, null, null,
['(function () {\n',
' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
'}());']);
var expected = [
{ str: '(function () {\n', source: null, line: null, column: null },
{ str: ' ', source: null, line: null, column: null },
{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },
{ str: ';\n', source: null, line: null, column: null },
{ str: ' ', source: null, line: null, column: null },
{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },
{ str: ';\n', source: null, line: null, column: null },
{ str: '}());', source: null, line: null, column: null },
];
var i = 0;
node.walk(function (chunk, loc) {
assert.equal(expected[i].str, chunk);
assert.equal(expected[i].source, loc.source);
assert.equal(expected[i].line, loc.line);
assert.equal(expected[i].column, loc.column);
i++;
});
};
exports['test .replaceRight'] = function (assert, util) {
var node;
// Not nested
node = new SourceNode(null, null, null, 'hello world');
node.replaceRight(/world/, 'universe');
assert.equal(node.toString(), 'hello universe');
// Nested
node = new SourceNode(null, null, null,
[new SourceNode(null, null, null, 'hey sexy mama, '),
new SourceNode(null, null, null, 'want to kill all humans?')]);
node.replaceRight(/kill all humans/, 'watch Futurama');
assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
};
exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
var node = new SourceNode(null, null, null,
['(function () {' + nl,
' ',
new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
new SourceNode(1, 8, 'a.js', '()'),
';' + nl,
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,
'}());']);
var result = node.toStringWithSourceMap({
file: 'foo.js'
});
assert.equal(result.code, [
'(function () {',
' someCall();',
' if (foo) bar();',
'}());'
].join(nl));
var map = result.map;
var mapWithoutOptions = node.toStringWithSourceMap().map;
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
assert.ok(mapWithoutOptions instanceof SourceMapGenerator, 'mapWithoutOptions instanceof SourceMapGenerator');
assert.ok(!('file' in mapWithoutOptions));
mapWithoutOptions._file = 'foo.js';
util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());
map = new SourceMapConsumer(map.toString());
var actual;
actual = map.originalPositionFor({
line: 1,
column: 4
});
assert.equal(actual.source, null);
assert.equal(actual.line, null);
assert.equal(actual.column, null);
actual = map.originalPositionFor({
line: 2,
column: 2
});
assert.equal(actual.source, 'a.js');
assert.equal(actual.line, 1);
assert.equal(actual.column, 0);
assert.equal(actual.name, 'originalCall');
actual = map.originalPositionFor({
line: 3,
column: 2
});
assert.equal(actual.source, 'b.js');
assert.equal(actual.line, 2);
assert.equal(actual.column, 0);
actual = map.originalPositionFor({
line: 3,
column: 16
});
assert.equal(actual.source, null);
assert.equal(actual.line, null);
assert.equal(actual.column, null);
actual = map.originalPositionFor({
line: 4,
column: 2
});
assert.equal(actual.source, null);
assert.equal(actual.line, null);
assert.equal(actual.column, null);
});
exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
var testCode = util.testGeneratedCode.replace(/\n/g, nl);
var node = SourceNode.fromStringWithSourceMap(
testCode,
new SourceMapConsumer(util.testMap));
var result = node.toStringWithSourceMap({
file: 'min.js'
});
var map = result.map;
var code = result.code;
assert.equal(code, testCode);
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
map = map.toJSON();
assert.equal(map.version, util.testMap.version);
assert.equal(map.file, util.testMap.file);
assert.equal(map.mappings, util.testMap.mappings);
});
exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {
var node = SourceNode.fromStringWithSourceMap(
util.testGeneratedCode.replace(/\n/g, nl),
new SourceMapConsumer(util.emptyMap));
var result = node.toStringWithSourceMap({
file: 'min.js'
});
var map = result.map;
var code = result.code;
assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
map = map.toJSON();
assert.equal(map.version, util.emptyMap.version);
assert.equal(map.file, util.emptyMap.file);
assert.equal(map.mappings.length, util.emptyMap.mappings.length);
assert.equal(map.mappings, util.emptyMap.mappings);
});
exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {
var input = new SourceNode(null, null, null, [
"(function() {" + nl,
" var Test = {};" + nl,
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,
"}());" + nl,
"/* Generated Source */"]);
input = input.toStringWithSourceMap({
file: 'foo.js'
});
var node = SourceNode.fromStringWithSourceMap(
input.code,
new SourceMapConsumer(input.map.toString()));
var result = node.toStringWithSourceMap({
file: 'foo.js'
});
var map = result.map;
var code = result.code;
assert.equal(code, input.code);
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
map = map.toJSON();
var inputMap = input.map.toJSON();
util.assertEqualMaps(assert, map, inputMap);
});
exports['test .fromStringWithSourceMap() third argument'] = function (assert, util) {
// Assume the following directory structure:
//
// http://foo.org/
// bar.coffee
// app/
// coffee/
// foo.coffee
// coffeeBundle.js # Made from {foo,bar,baz}.coffee
// maps/
// coffeeBundle.js.map
// js/
// foo.js
// public/
// app.js # Made from {foo,coffeeBundle}.js
// app.js.map
//
// http://www.example.com/
// baz.coffee
var coffeeBundle = new SourceNode(1, 0, 'foo.coffee', 'foo(coffee);\n');
coffeeBundle.setSourceContent('foo.coffee', 'foo coffee');
coffeeBundle.add(new SourceNode(2, 0, '/bar.coffee', 'bar(coffee);\n'));
coffeeBundle.add(new SourceNode(3, 0, 'http://www.example.com/baz.coffee', 'baz(coffee);'));
coffeeBundle = coffeeBundle.toStringWithSourceMap({
file: 'foo.js',
sourceRoot: '..'
});
var foo = new SourceNode(1, 0, 'foo.js', 'foo(js);');
var test = function(relativePath, expectedSources) {
var app = new SourceNode();
app.add(SourceNode.fromStringWithSourceMap(
coffeeBundle.code,
new SourceMapConsumer(coffeeBundle.map.toString()),
relativePath));
app.add(foo);
var i = 0;
app.walk(function (chunk, loc) {
assert.equal(loc.source, expectedSources[i]);
i++;
});
app.walkSourceContents(function (sourceFile, sourceContent) {
assert.equal(sourceFile, expectedSources[0]);
assert.equal(sourceContent, 'foo coffee');
})
};
test('../coffee/maps', [
'../coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee',
'foo.js'
]);
// If the third parameter is omitted or set to the current working
// directory we get incorrect source paths:
test(undefined, [
'../foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee',
'foo.js'
]);
test('', [
'../foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee',
'foo.js'
]);
test('.', [
'../foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee',
'foo.js'
]);
test('./', [
'../foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee',
'foo.js'
]);
};
exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {
var input = new SourceNode(null, null, null, [
new SourceNode(1, 0, "a.js", "(function"),
new SourceNode(1, 0, "a.js", "() {" + nl),
" ",
new SourceNode(1, 0, "a.js", "var Test = "),
new SourceNode(1, 0, "b.js", "{};" + nl),
new SourceNode(2, 0, "b.js", "Test"),
new SourceNode(2, 0, "b.js", ".A", "A"),
new SourceNode(2, 20, "b.js", " = { value: ", "A"),
"1234",
new SourceNode(2, 40, "b.js", " };" + nl, "A"),
"}());" + nl,
"/* Generated Source */"
]);
input = input.toStringWithSourceMap({
file: 'foo.js'
});
assert.equal(input.code, [
"(function() {",
" var Test = {};",
"Test.A = { value: 1234 };",
"}());",
"/* Generated Source */"
].join(nl))
var correctMap = new SourceMapGenerator({
file: 'foo.js'
});
correctMap.addMapping({
generated: { line: 1, column: 0 },
source: 'a.js',
original: { line: 1, column: 0 }
});
// Here is no need for a empty mapping,
// because mappings ends at eol
correctMap.addMapping({
generated: { line: 2, column: 2 },
source: 'a.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 2, column: 13 },
source: 'b.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 3, column: 0 },
source: 'b.js',
original: { line: 2, column: 0 }
});
correctMap.addMapping({
generated: { line: 3, column: 4 },
source: 'b.js',
name: 'A',
original: { line: 2, column: 0 }
});
correctMap.addMapping({
generated: { line: 3, column: 6 },
source: 'b.js',
name: 'A',
original: { line: 2, column: 20 }
});
// This empty mapping is required,
// because there is a hole in the middle of the line
correctMap.addMapping({
generated: { line: 3, column: 18 }
});
correctMap.addMapping({
generated: { line: 3, column: 22 },
source: 'b.js',
name: 'A',
original: { line: 2, column: 40 }
});
// Here is no need for a empty mapping,
// because mappings ends at eol
var inputMap = input.map.toJSON();
correctMap = correctMap.toJSON();
util.assertEqualMaps(assert, inputMap, correctMap);
});
exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {
var input = new SourceNode(null, null, null, [
new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),
new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),
new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),
new SourceNode(2, 2, "b.js", "anotherLine();" + nl),
"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,
new SourceNode(3, 4, "c.js", "anotherLine();" + nl),
"/*" + nl + "Generated" + nl + "Source" + nl + "*/"
]);
input = input.toStringWithSourceMap({
file: 'foo.js'
});
assert.equal(input.code, [
"(function() {",
"var nextLine = 1;",
"anotherLine();",
"Test.call(this, 123);",
"this['stuff'] = 'v';",
"anotherLine();",
"/*",
"Generated",
"Source",
"*/",
"anotherLine();",
"/*",
"Generated",
"Source",
"*/"
].join(nl));
var correctMap = new SourceMapGenerator({
file: 'foo.js'
});
correctMap.addMapping({
generated: { line: 1, column: 0 },
source: 'a.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 2, column: 0 },
source: 'a.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 3, column: 0 },
source: 'a.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 4, column: 0 },
source: 'b.js',
original: { line: 2, column: 2 }
});
correctMap.addMapping({
generated: { line: 5, column: 0 },
source: 'b.js',
original: { line: 2, column: 2 }
});
correctMap.addMapping({
generated: { line: 6, column: 0 },
source: 'b.js',
original: { line: 2, column: 2 }
});
correctMap.addMapping({
generated: { line: 11, column: 0 },
source: 'c.js',
original: { line: 3, column: 4 }
});
var inputMap = input.map.toJSON();
correctMap = correctMap.toJSON();
util.assertEqualMaps(assert, inputMap, correctMap);
});
exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {
var node = new SourceNode(1, 0, 'empty.js', '');
var result = node.toStringWithSourceMap();
assert.equal(result.code, '');
};
exports['test .toStringWithSourceMap() with consecutive newlines'] = forEachNewline(function (assert, util, nl) {
var input = new SourceNode(null, null, null, [
"/***/" + nl + nl,
new SourceNode(1, 0, "a.js", "'use strict';" + nl),
new SourceNode(2, 0, "a.js", "a();"),
]);
input = input.toStringWithSourceMap({
file: 'foo.js'
});
assert.equal(input.code, [
"/***/",
"",
"'use strict';",
"a();",
].join(nl));
var correctMap = new SourceMapGenerator({
file: 'foo.js'
});
correctMap.addMapping({
generated: { line: 3, column: 0 },
source: 'a.js',
original: { line: 1, column: 0 }
});
correctMap.addMapping({
generated: { line: 4, column: 0 },
source: 'a.js',
original: { line: 2, column: 0 }
});
var inputMap = input.map.toJSON();
correctMap = correctMap.toJSON();
util.assertEqualMaps(assert, inputMap, correctMap);
});
exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
var aNode = new SourceNode(1, 1, 'a.js', 'a');
aNode.setSourceContent('a.js', 'someContent');
var node = new SourceNode(null, null, null,
['(function () {\n',
' ', aNode,
' ', new SourceNode(1, 1, 'b.js', 'b'),
'}());']);
node.setSourceContent('b.js', 'otherContent');
var map = node.toStringWithSourceMap({
file: 'foo.js'
}).map;
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
map = new SourceMapConsumer(map.toString());
assert.equal(map.sources.length, 2);
assert.equal(map.sources[0], 'a.js');
assert.equal(map.sources[1], 'b.js');
assert.equal(map.sourcesContent.length, 2);
assert.equal(map.sourcesContent[0], 'someContent');
assert.equal(map.sourcesContent[1], 'otherContent');
};
exports['test walkSourceContents'] = function (assert, util) {
var aNode = new SourceNode(1, 1, 'a.js', 'a');
aNode.setSourceContent('a.js', 'someContent');
var node = new SourceNode(null, null, null,
['(function () {\n',
' ', aNode,
' ', new SourceNode(1, 1, 'b.js', 'b'),
'}());']);
node.setSourceContent('b.js', 'otherContent');
var results = [];
node.walkSourceContents(function (sourceFile, sourceContent) {
results.push([sourceFile, sourceContent]);
});
assert.equal(results.length, 2);
assert.equal(results[0][0], 'a.js');
assert.equal(results[0][1], 'someContent');
assert.equal(results[1][0], 'b.js');
assert.equal(results[1][1], 'otherContent');
};
});
function run_test() {
runSourceMapTests('test/source-map/test-source-node', do_throw);
}