michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: michael@0: /* michael@0: * WARNING! michael@0: * michael@0: * Do not edit this file directly, it is built from the sources at michael@0: * https://github.com/mozilla/source-map/ michael@0: */ michael@0: michael@0: Components.utils.import('resource://gre/modules/devtools/Require.jsm'); michael@0: Components.utils.import('resource://gre/modules/devtools/SourceMap.jsm'); michael@0: michael@0: this.EXPORTED_SYMBOLS = [ "define", "runSourceMapTests" ]; michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('test/source-map/assert', ['exports'], function (exports) { michael@0: michael@0: let do_throw = function (msg) { michael@0: throw new Error(msg); michael@0: }; michael@0: michael@0: exports.init = function (throw_fn) { michael@0: do_throw = throw_fn; michael@0: }; michael@0: michael@0: exports.doesNotThrow = function (fn) { michael@0: try { michael@0: fn(); michael@0: } michael@0: catch (e) { michael@0: do_throw(e.message); michael@0: } michael@0: }; michael@0: michael@0: exports.equal = function (actual, expected, msg) { michael@0: msg = msg || String(actual) + ' != ' + String(expected); michael@0: if (actual != expected) { michael@0: do_throw(msg); michael@0: } michael@0: }; michael@0: michael@0: exports.ok = function (val, msg) { michael@0: msg = msg || String(val) + ' is falsey'; michael@0: if (!Boolean(val)) { michael@0: do_throw(msg); michael@0: } michael@0: }; michael@0: michael@0: exports.strictEqual = function (actual, expected, msg) { michael@0: msg = msg || String(actual) + ' !== ' + String(expected); michael@0: if (actual !== expected) { michael@0: do_throw(msg); michael@0: } michael@0: }; michael@0: michael@0: exports.throws = function (fn) { michael@0: try { michael@0: fn(); michael@0: do_throw('Expected an error to be thrown, but it wasn\'t.'); michael@0: } michael@0: catch (e) { michael@0: } michael@0: }; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('test/source-map/util', ['require', 'exports', 'module' , 'lib/source-map/util'], function(require, exports, module) { michael@0: michael@0: var util = require('source-map/util'); michael@0: michael@0: // This is a test mapping which maps functions from two different files michael@0: // (one.js and two.js) to a minified generated source. michael@0: // michael@0: // Here is one.js: michael@0: // michael@0: // ONE.foo = function (bar) { michael@0: // return baz(bar); michael@0: // }; michael@0: // michael@0: // Here is two.js: michael@0: // michael@0: // TWO.inc = function (n) { michael@0: // return n + 1; michael@0: // }; michael@0: // michael@0: // And here is the generated code (min.js): michael@0: // michael@0: // ONE.foo=function(a){return baz(a);}; michael@0: // TWO.inc=function(a){return a+1;}; michael@0: exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+ michael@0: " TWO.inc=function(a){return a+1;};"; michael@0: exports.testMap = { michael@0: version: 3, michael@0: file: 'min.js', michael@0: names: ['bar', 'baz', 'n'], michael@0: sources: ['one.js', 'two.js'], michael@0: sourceRoot: '/the/root', michael@0: mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA' michael@0: }; michael@0: exports.testMapWithSourcesContent = { michael@0: version: 3, michael@0: file: 'min.js', michael@0: names: ['bar', 'baz', 'n'], michael@0: sources: ['one.js', 'two.js'], michael@0: sourcesContent: [ michael@0: ' ONE.foo = function (bar) {\n' + michael@0: ' return baz(bar);\n' + michael@0: ' };', michael@0: ' TWO.inc = function (n) {\n' + michael@0: ' return n + 1;\n' + michael@0: ' };' michael@0: ], michael@0: sourceRoot: '/the/root', michael@0: mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA' michael@0: }; michael@0: exports.emptyMap = { michael@0: version: 3, michael@0: file: 'min.js', michael@0: names: [], michael@0: sources: [], michael@0: mappings: '' michael@0: }; michael@0: michael@0: michael@0: function assertMapping(generatedLine, generatedColumn, originalSource, michael@0: originalLine, originalColumn, name, map, assert, michael@0: dontTestGenerated, dontTestOriginal) { michael@0: if (!dontTestOriginal) { michael@0: var origMapping = map.originalPositionFor({ michael@0: line: generatedLine, michael@0: column: generatedColumn michael@0: }); michael@0: assert.equal(origMapping.name, name, michael@0: 'Incorrect name, expected ' + JSON.stringify(name) michael@0: + ', got ' + JSON.stringify(origMapping.name)); michael@0: assert.equal(origMapping.line, originalLine, michael@0: 'Incorrect line, expected ' + JSON.stringify(originalLine) michael@0: + ', got ' + JSON.stringify(origMapping.line)); michael@0: assert.equal(origMapping.column, originalColumn, michael@0: 'Incorrect column, expected ' + JSON.stringify(originalColumn) michael@0: + ', got ' + JSON.stringify(origMapping.column)); michael@0: michael@0: var expectedSource; michael@0: michael@0: if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) { michael@0: expectedSource = originalSource; michael@0: } else if (originalSource) { michael@0: expectedSource = map.sourceRoot michael@0: ? util.join(map.sourceRoot, originalSource) michael@0: : originalSource; michael@0: } else { michael@0: expectedSource = null; michael@0: } michael@0: michael@0: assert.equal(origMapping.source, expectedSource, michael@0: 'Incorrect source, expected ' + JSON.stringify(expectedSource) michael@0: + ', got ' + JSON.stringify(origMapping.source)); michael@0: } michael@0: michael@0: if (!dontTestGenerated) { michael@0: var genMapping = map.generatedPositionFor({ michael@0: source: originalSource, michael@0: line: originalLine, michael@0: column: originalColumn michael@0: }); michael@0: assert.equal(genMapping.line, generatedLine, michael@0: 'Incorrect line, expected ' + JSON.stringify(generatedLine) michael@0: + ', got ' + JSON.stringify(genMapping.line)); michael@0: assert.equal(genMapping.column, generatedColumn, michael@0: 'Incorrect column, expected ' + JSON.stringify(generatedColumn) michael@0: + ', got ' + JSON.stringify(genMapping.column)); michael@0: } michael@0: } michael@0: exports.assertMapping = assertMapping; michael@0: michael@0: function assertEqualMaps(assert, actualMap, expectedMap) { michael@0: assert.equal(actualMap.version, expectedMap.version, "version mismatch"); michael@0: assert.equal(actualMap.file, expectedMap.file, "file mismatch"); michael@0: assert.equal(actualMap.names.length, michael@0: expectedMap.names.length, michael@0: "names length mismatch: " + michael@0: actualMap.names.join(", ") + " != " + expectedMap.names.join(", ")); michael@0: for (var i = 0; i < actualMap.names.length; i++) { michael@0: assert.equal(actualMap.names[i], michael@0: expectedMap.names[i], michael@0: "names[" + i + "] mismatch: " + michael@0: actualMap.names.join(", ") + " != " + expectedMap.names.join(", ")); michael@0: } michael@0: assert.equal(actualMap.sources.length, michael@0: expectedMap.sources.length, michael@0: "sources length mismatch: " + michael@0: actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", ")); michael@0: for (var i = 0; i < actualMap.sources.length; i++) { michael@0: assert.equal(actualMap.sources[i], michael@0: expectedMap.sources[i], michael@0: "sources[" + i + "] length mismatch: " + michael@0: actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", ")); michael@0: } michael@0: assert.equal(actualMap.sourceRoot, michael@0: expectedMap.sourceRoot, michael@0: "sourceRoot mismatch: " + michael@0: actualMap.sourceRoot + " != " + expectedMap.sourceRoot); michael@0: assert.equal(actualMap.mappings, expectedMap.mappings, michael@0: "mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings); michael@0: if (actualMap.sourcesContent) { michael@0: assert.equal(actualMap.sourcesContent.length, michael@0: expectedMap.sourcesContent.length, michael@0: "sourcesContent length mismatch"); michael@0: for (var i = 0; i < actualMap.sourcesContent.length; i++) { michael@0: assert.equal(actualMap.sourcesContent[i], michael@0: expectedMap.sourcesContent[i], michael@0: "sourcesContent[" + i + "] mismatch"); michael@0: } michael@0: } michael@0: } michael@0: exports.assertEqualMaps = assertEqualMaps; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define('lib/source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) { michael@0: michael@0: /** michael@0: * This is a helper function for getting values from parameter/options michael@0: * objects. michael@0: * michael@0: * @param args The object we are extracting values from michael@0: * @param name The name of the property we are getting. michael@0: * @param defaultValue An optional value to return if the property is missing michael@0: * from the object. If this is not specified and the property is missing, an michael@0: * error will be thrown. michael@0: */ michael@0: function getArg(aArgs, aName, aDefaultValue) { michael@0: if (aName in aArgs) { michael@0: return aArgs[aName]; michael@0: } else if (arguments.length === 3) { michael@0: return aDefaultValue; michael@0: } else { michael@0: throw new Error('"' + aName + '" is a required argument.'); michael@0: } michael@0: } michael@0: exports.getArg = getArg; michael@0: michael@0: var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/; michael@0: var dataUrlRegexp = /^data:.+\,.+/; michael@0: michael@0: function urlParse(aUrl) { michael@0: var match = aUrl.match(urlRegexp); michael@0: if (!match) { michael@0: return null; michael@0: } michael@0: return { michael@0: scheme: match[1], michael@0: auth: match[3], michael@0: host: match[4], michael@0: port: match[6], michael@0: path: match[7] michael@0: }; michael@0: } michael@0: exports.urlParse = urlParse; michael@0: michael@0: function urlGenerate(aParsedUrl) { michael@0: var url = aParsedUrl.scheme + "://"; michael@0: if (aParsedUrl.auth) { michael@0: url += aParsedUrl.auth + "@" michael@0: } michael@0: if (aParsedUrl.host) { michael@0: url += aParsedUrl.host; michael@0: } michael@0: if (aParsedUrl.port) { michael@0: url += ":" + aParsedUrl.port michael@0: } michael@0: if (aParsedUrl.path) { michael@0: url += aParsedUrl.path; michael@0: } michael@0: return url; michael@0: } michael@0: exports.urlGenerate = urlGenerate; michael@0: michael@0: function join(aRoot, aPath) { michael@0: var url; michael@0: michael@0: if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) { michael@0: return aPath; michael@0: } michael@0: michael@0: if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) { michael@0: url.path = aPath; michael@0: return urlGenerate(url); michael@0: } michael@0: michael@0: return aRoot.replace(/\/$/, '') + '/' + aPath; michael@0: } michael@0: exports.join = join; michael@0: michael@0: /** michael@0: * Because behavior goes wacky when you set `__proto__` on objects, we michael@0: * have to prefix all the strings in our set with an arbitrary character. michael@0: * michael@0: * See https://github.com/mozilla/source-map/pull/31 and michael@0: * https://github.com/mozilla/source-map/issues/30 michael@0: * michael@0: * @param String aStr michael@0: */ michael@0: function toSetString(aStr) { michael@0: return '$' + aStr; michael@0: } michael@0: exports.toSetString = toSetString; michael@0: michael@0: function fromSetString(aStr) { michael@0: return aStr.substr(1); michael@0: } michael@0: exports.fromSetString = fromSetString; michael@0: michael@0: function relative(aRoot, aPath) { michael@0: aRoot = aRoot.replace(/\/$/, ''); michael@0: michael@0: var url = urlParse(aRoot); michael@0: if (aPath.charAt(0) == "/" && url && url.path == "/") { michael@0: return aPath.slice(1); michael@0: } michael@0: michael@0: return aPath.indexOf(aRoot + '/') === 0 michael@0: ? aPath.substr(aRoot.length + 1) michael@0: : aPath; michael@0: } michael@0: exports.relative = relative; michael@0: michael@0: function strcmp(aStr1, aStr2) { michael@0: var s1 = aStr1 || ""; michael@0: var s2 = aStr2 || ""; michael@0: return (s1 > s2) - (s1 < s2); michael@0: } michael@0: michael@0: /** michael@0: * Comparator between two mappings where the original positions are compared. michael@0: * michael@0: * Optionally pass in `true` as `onlyCompareGenerated` to consider two michael@0: * mappings with the same original source/line/column, but different generated michael@0: * line and column the same. Useful when searching for a mapping with a michael@0: * stubbed out mapping. michael@0: */ michael@0: function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) { michael@0: var cmp; michael@0: michael@0: cmp = strcmp(mappingA.source, mappingB.source); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalLine - mappingB.originalLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalColumn - mappingB.originalColumn; michael@0: if (cmp || onlyCompareOriginal) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = strcmp(mappingA.name, mappingB.name); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.generatedLine - mappingB.generatedLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: return mappingA.generatedColumn - mappingB.generatedColumn; michael@0: }; michael@0: exports.compareByOriginalPositions = compareByOriginalPositions; michael@0: michael@0: /** michael@0: * Comparator between two mappings where the generated positions are michael@0: * compared. michael@0: * michael@0: * Optionally pass in `true` as `onlyCompareGenerated` to consider two michael@0: * mappings with the same generated line and column, but different michael@0: * source/name/original line and column the same. Useful when searching for a michael@0: * mapping with a stubbed out mapping. michael@0: */ michael@0: function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) { michael@0: var cmp; michael@0: michael@0: cmp = mappingA.generatedLine - mappingB.generatedLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.generatedColumn - mappingB.generatedColumn; michael@0: if (cmp || onlyCompareGenerated) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = strcmp(mappingA.source, mappingB.source); michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalLine - mappingB.originalLine; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: cmp = mappingA.originalColumn - mappingB.originalColumn; michael@0: if (cmp) { michael@0: return cmp; michael@0: } michael@0: michael@0: return strcmp(mappingA.name, mappingB.name); michael@0: }; michael@0: exports.compareByGeneratedPositions = compareByGeneratedPositions; michael@0: michael@0: }); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: function runSourceMapTests(modName, do_throw) { michael@0: let mod = require(modName); michael@0: let assert = require('test/source-map/assert'); michael@0: let util = require('test/source-map/util'); michael@0: michael@0: assert.init(do_throw); michael@0: michael@0: for (let k in mod) { michael@0: if (/^test/.test(k)) { michael@0: mod[k](assert, util); michael@0: } michael@0: } michael@0: michael@0: } michael@0: this.runSourceMapTests = runSourceMapTests;