You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1254 lines
41 KiB

4 years ago
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. const util = require("./util");
  8. const binarySearch = require("./binary-search");
  9. const ArraySet = require("./array-set").ArraySet;
  10. const base64VLQ = require("./base64-vlq"); // eslint-disable-line no-unused-vars
  11. const readWasm = require("../lib/read-wasm");
  12. const wasm = require("./wasm");
  13. const INTERNAL = Symbol("smcInternal");
  14. class SourceMapConsumer {
  15. constructor(aSourceMap, aSourceMapURL) {
  16. // If the constructor was called by super(), just return Promise<this>.
  17. // Yes, this is a hack to retain the pre-existing API of the base-class
  18. // constructor also being an async factory function.
  19. if (aSourceMap == INTERNAL) {
  20. return Promise.resolve(this);
  21. }
  22. return _factory(aSourceMap, aSourceMapURL);
  23. }
  24. static initialize(opts) {
  25. readWasm.initialize(opts["lib/mappings.wasm"]);
  26. }
  27. static fromSourceMap(aSourceMap, aSourceMapURL) {
  28. return _factoryBSM(aSourceMap, aSourceMapURL);
  29. }
  30. /**
  31. * Construct a new `SourceMapConsumer` from `rawSourceMap` and `sourceMapUrl`
  32. * (see the `SourceMapConsumer` constructor for details. Then, invoke the `async
  33. * function f(SourceMapConsumer) -> T` with the newly constructed consumer, wait
  34. * for `f` to complete, call `destroy` on the consumer, and return `f`'s return
  35. * value.
  36. *
  37. * You must not use the consumer after `f` completes!
  38. *
  39. * By using `with`, you do not have to remember to manually call `destroy` on
  40. * the consumer, since it will be called automatically once `f` completes.
  41. *
  42. * ```js
  43. * const xSquared = await SourceMapConsumer.with(
  44. * myRawSourceMap,
  45. * null,
  46. * async function (consumer) {
  47. * // Use `consumer` inside here and don't worry about remembering
  48. * // to call `destroy`.
  49. *
  50. * const x = await whatever(consumer);
  51. * return x * x;
  52. * }
  53. * );
  54. *
  55. * // You may not use that `consumer` anymore out here; it has
  56. * // been destroyed. But you can use `xSquared`.
  57. * console.log(xSquared);
  58. * ```
  59. */
  60. static with(rawSourceMap, sourceMapUrl, f) {
  61. // Note: The `acorn` version that `webpack` currently depends on doesn't
  62. // support `async` functions, and the nodes that we support don't all have
  63. // `.finally`. Therefore, this is written a bit more convolutedly than it
  64. // should really be.
  65. let consumer = null;
  66. const promise = new SourceMapConsumer(rawSourceMap, sourceMapUrl);
  67. return promise
  68. .then(c => {
  69. consumer = c;
  70. return f(c);
  71. })
  72. .then(x => {
  73. if (consumer) {
  74. consumer.destroy();
  75. }
  76. return x;
  77. }, e => {
  78. if (consumer) {
  79. consumer.destroy();
  80. }
  81. throw e;
  82. });
  83. }
  84. /**
  85. * Parse the mappings in a string in to a data structure which we can easily
  86. * query (the ordered arrays in the `this.__generatedMappings` and
  87. * `this.__originalMappings` properties).
  88. */
  89. _parseMappings(aStr, aSourceRoot) {
  90. throw new Error("Subclasses must implement _parseMappings");
  91. }
  92. /**
  93. * Iterate over each mapping between an original source/line/column and a
  94. * generated line/column in this source map.
  95. *
  96. * @param Function aCallback
  97. * The function that is called with each mapping.
  98. * @param Object aContext
  99. * Optional. If specified, this object will be the value of `this` every
  100. * time that `aCallback` is called.
  101. * @param aOrder
  102. * Either `SourceMapConsumer.GENERATED_ORDER` or
  103. * `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
  104. * iterate over the mappings sorted by the generated file's line/column
  105. * order or the original's source/line/column order, respectively. Defaults to
  106. * `SourceMapConsumer.GENERATED_ORDER`.
  107. */
  108. eachMapping(aCallback, aContext, aOrder) {
  109. throw new Error("Subclasses must implement eachMapping");
  110. }
  111. /**
  112. * Returns all generated line and column information for the original source,
  113. * line, and column provided. If no column is provided, returns all mappings
  114. * corresponding to a either the line we are searching for or the next
  115. * closest line that has any mappings. Otherwise, returns all mappings
  116. * corresponding to the given line and either the column we are searching for
  117. * or the next closest column that has any offsets.
  118. *
  119. * The only argument is an object with the following properties:
  120. *
  121. * - source: The filename of the original source.
  122. * - line: The line number in the original source. The line number is 1-based.
  123. * - column: Optional. the column number in the original source.
  124. * The column number is 0-based.
  125. *
  126. * and an array of objects is returned, each with the following properties:
  127. *
  128. * - line: The line number in the generated source, or null. The
  129. * line number is 1-based.
  130. * - column: The column number in the generated source, or null.
  131. * The column number is 0-based.
  132. */
  133. allGeneratedPositionsFor(aArgs) {
  134. throw new Error("Subclasses must implement allGeneratedPositionsFor");
  135. }
  136. destroy() {
  137. throw new Error("Subclasses must implement destroy");
  138. }
  139. }
  140. /**
  141. * The version of the source mapping spec that we are consuming.
  142. */
  143. SourceMapConsumer.prototype._version = 3;
  144. SourceMapConsumer.GENERATED_ORDER = 1;
  145. SourceMapConsumer.ORIGINAL_ORDER = 2;
  146. SourceMapConsumer.GREATEST_LOWER_BOUND = 1;
  147. SourceMapConsumer.LEAST_UPPER_BOUND = 2;
  148. exports.SourceMapConsumer = SourceMapConsumer;
  149. /**
  150. * A BasicSourceMapConsumer instance represents a parsed source map which we can
  151. * query for information about the original file positions by giving it a file
  152. * position in the generated source.
  153. *
  154. * The first parameter is the raw source map (either as a JSON string, or
  155. * already parsed to an object). According to the spec, source maps have the
  156. * following attributes:
  157. *
  158. * - version: Which version of the source map spec this map is following.
  159. * - sources: An array of URLs to the original source files.
  160. * - names: An array of identifiers which can be referenced by individual mappings.
  161. * - sourceRoot: Optional. The URL root from which all sources are relative.
  162. * - sourcesContent: Optional. An array of contents of the original source files.
  163. * - mappings: A string of base64 VLQs which contain the actual mappings.
  164. * - file: Optional. The generated file this source map is associated with.
  165. *
  166. * Here is an example source map, taken from the source map spec[0]:
  167. *
  168. * {
  169. * version : 3,
  170. * file: "out.js",
  171. * sourceRoot : "",
  172. * sources: ["foo.js", "bar.js"],
  173. * names: ["src", "maps", "are", "fun"],
  174. * mappings: "AA,AB;;ABCDE;"
  175. * }
  176. *
  177. * The second parameter, if given, is a string whose value is the URL
  178. * at which the source map was found. This URL is used to compute the
  179. * sources array.
  180. *
  181. * [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?pli=1#
  182. */
  183. class BasicSourceMapConsumer extends SourceMapConsumer {
  184. constructor(aSourceMap, aSourceMapURL) {
  185. return super(INTERNAL).then(that => {
  186. let sourceMap = aSourceMap;
  187. if (typeof aSourceMap === "string") {
  188. sourceMap = util.parseSourceMapInput(aSourceMap);
  189. }
  190. const version = util.getArg(sourceMap, "version");
  191. let sources = util.getArg(sourceMap, "sources");
  192. // Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which
  193. // requires the array) to play nice here.
  194. const names = util.getArg(sourceMap, "names", []);
  195. let sourceRoot = util.getArg(sourceMap, "sourceRoot", null);
  196. const sourcesContent = util.getArg(sourceMap, "sourcesContent", null);
  197. const mappings = util.getArg(sourceMap, "mappings");
  198. const file = util.getArg(sourceMap, "file", null);
  199. // Once again, Sass deviates from the spec and supplies the version as a
  200. // string rather than a number, so we use loose equality checking here.
  201. if (version != that._version) {
  202. throw new Error("Unsupported version: " + version);
  203. }
  204. if (sourceRoot) {
  205. sourceRoot = util.normalize(sourceRoot);
  206. }
  207. sources = sources
  208. .map(String)
  209. // Some source maps produce relative source paths like "./foo.js" instead of
  210. // "foo.js". Normalize these first so that future comparisons will succeed.
  211. // See bugzil.la/1090768.
  212. .map(util.normalize)
  213. // Always ensure that absolute sources are internally stored relative to
  214. // the source root, if the source root is absolute. Not doing this would
  215. // be particularly problematic when the source root is a prefix of the
  216. // source (valid, but why??). See github issue #199 and bugzil.la/1188982.
  217. .map(function(source) {
  218. return sourceRoot && util.isAbsolute(sourceRoot) && util.isAbsolute(source)
  219. ? util.relative(sourceRoot, source)
  220. : source;
  221. });
  222. // Pass `true` below to allow duplicate names and sources. While source maps
  223. // are intended to be compressed and deduplicated, the TypeScript compiler
  224. // sometimes generates source maps with duplicates in them. See Github issue
  225. // #72 and bugzil.la/889492.
  226. that._names = ArraySet.fromArray(names.map(String), true);
  227. that._sources = ArraySet.fromArray(sources, true);
  228. that._absoluteSources = that._sources.toArray().map(function(s) {
  229. return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
  230. });
  231. that.sourceRoot = sourceRoot;
  232. that.sourcesContent = sourcesContent;
  233. that._mappings = mappings;
  234. that._sourceMapURL = aSourceMapURL;
  235. that.file = file;
  236. that._computedColumnSpans = false;
  237. that._mappingsPtr = 0;
  238. that._wasm = null;
  239. return wasm().then(w => {
  240. that._wasm = w;
  241. return that;
  242. });
  243. });
  244. }
  245. /**
  246. * Utility function to find the index of a source. Returns -1 if not
  247. * found.
  248. */
  249. _findSourceIndex(aSource) {
  250. let relativeSource = aSource;
  251. if (this.sourceRoot != null) {
  252. relativeSource = util.relative(this.sourceRoot, relativeSource);
  253. }
  254. if (this._sources.has(relativeSource)) {
  255. return this._sources.indexOf(relativeSource);
  256. }
  257. // Maybe aSource is an absolute URL as returned by |sources|. In
  258. // this case we can't simply undo the transform.
  259. for (let i = 0; i < this._absoluteSources.length; ++i) {
  260. if (this._absoluteSources[i] == aSource) {
  261. return i;
  262. }
  263. }
  264. return -1;
  265. }
  266. /**
  267. * Create a BasicSourceMapConsumer from a SourceMapGenerator.
  268. *
  269. * @param SourceMapGenerator aSourceMap
  270. * The source map that will be consumed.
  271. * @param String aSourceMapURL
  272. * The URL at which the source map can be found (optional)
  273. * @returns BasicSourceMapConsumer
  274. */
  275. static fromSourceMap(aSourceMap, aSourceMapURL) {
  276. return new BasicSourceMapConsumer(aSourceMap.toString());
  277. }
  278. get sources() {
  279. return this._absoluteSources.slice();
  280. }
  281. _getMappingsPtr() {
  282. if (this._mappingsPtr === 0) {
  283. this._parseMappings(this._mappings, this.sourceRoot);
  284. }
  285. return this._mappingsPtr;
  286. }
  287. /**
  288. * Parse the mappings in a string in to a data structure which we can easily
  289. * query (the ordered arrays in the `this.__generatedMappings` and
  290. * `this.__originalMappings` properties).
  291. */
  292. _parseMappings(aStr, aSourceRoot) {
  293. const size = aStr.length;
  294. const mappingsBufPtr = this._wasm.exports.allocate_mappings(size);
  295. const mappingsBuf = new Uint8Array(this._wasm.exports.memory.buffer, mappingsBufPtr, size);
  296. for (let i = 0; i < size; i++) {
  297. mappingsBuf[i] = aStr.charCodeAt(i);
  298. }
  299. const mappingsPtr = this._wasm.exports.parse_mappings(mappingsBufPtr);
  300. if (!mappingsPtr) {
  301. const error = this._wasm.exports.get_last_error();
  302. let msg = `Error parsing mappings (code ${error}): `;
  303. // XXX: keep these error codes in sync with `fitzgen/source-map-mappings`.
  304. switch (error) {
  305. case 1:
  306. msg += "the mappings contained a negative line, column, source index, or name index";
  307. break;
  308. case 2:
  309. msg += "the mappings contained a number larger than 2**32";
  310. break;
  311. case 3:
  312. msg += "reached EOF while in the middle of parsing a VLQ";
  313. break;
  314. case 4:
  315. msg += "invalid base 64 character while parsing a VLQ";
  316. break;
  317. default:
  318. msg += "unknown error code";
  319. break;
  320. }
  321. throw new Error(msg);
  322. }
  323. this._mappingsPtr = mappingsPtr;
  324. }
  325. eachMapping(aCallback, aContext, aOrder) {
  326. const context = aContext || null;
  327. const order = aOrder || SourceMapConsumer.GENERATED_ORDER;
  328. const sourceRoot = this.sourceRoot;
  329. this._wasm.withMappingCallback(
  330. mapping => {
  331. if (mapping.source !== null) {
  332. mapping.source = this._sources.at(mapping.source);
  333. mapping.source = util.computeSourceURL(sourceRoot, mapping.source, this._sourceMapURL);
  334. if (mapping.name !== null) {
  335. mapping.name = this._names.at(mapping.name);
  336. }
  337. }
  338. aCallback.call(context, mapping);
  339. },
  340. () => {
  341. switch (order) {
  342. case SourceMapConsumer.GENERATED_ORDER:
  343. this._wasm.exports.by_generated_location(this._getMappingsPtr());
  344. break;
  345. case SourceMapConsumer.ORIGINAL_ORDER:
  346. this._wasm.exports.by_original_location(this._getMappingsPtr());
  347. break;
  348. default:
  349. throw new Error("Unknown order of iteration.");
  350. }
  351. }
  352. );
  353. }
  354. allGeneratedPositionsFor(aArgs) {
  355. let source = util.getArg(aArgs, "source");
  356. const originalLine = util.getArg(aArgs, "line");
  357. const originalColumn = aArgs.column || 0;
  358. source = this._findSourceIndex(source);
  359. if (source < 0) {
  360. return [];
  361. }
  362. if (originalLine < 1) {
  363. throw new Error("Line numbers must be >= 1");
  364. }
  365. if (originalColumn < 0) {
  366. throw new Error("Column numbers must be >= 0");
  367. }
  368. const mappings = [];
  369. this._wasm.withMappingCallback(
  370. m => {
  371. let lastColumn = m.lastGeneratedColumn;
  372. if (this._computedColumnSpans && lastColumn === null) {
  373. lastColumn = Infinity;
  374. }
  375. mappings.push({
  376. line: m.generatedLine,
  377. column: m.generatedColumn,
  378. lastColumn,
  379. });
  380. }, () => {
  381. this._wasm.exports.all_generated_locations_for(
  382. this._getMappingsPtr(),
  383. source,
  384. originalLine - 1,
  385. "column" in aArgs,
  386. originalColumn
  387. );
  388. }
  389. );
  390. return mappings;
  391. }
  392. destroy() {
  393. if (this._mappingsPtr !== 0) {
  394. this._wasm.exports.free_mappings(this._mappingsPtr);
  395. this._mappingsPtr = 0;
  396. }
  397. }
  398. /**
  399. * Compute the last column for each generated mapping. The last column is
  400. * inclusive.
  401. */
  402. computeColumnSpans() {
  403. if (this._computedColumnSpans) {
  404. return;
  405. }
  406. this._wasm.exports.compute_column_spans(this._getMappingsPtr());
  407. this._computedColumnSpans = true;
  408. }
  409. /**
  410. * Returns the original source, line, and column information for the generated
  411. * source's line and column positions provided. The only argument is an object
  412. * with the following properties:
  413. *
  414. * - line: The line number in the generated source. The line number
  415. * is 1-based.
  416. * - column: The column number in the generated source. The column
  417. * number is 0-based.
  418. * - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
  419. * 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
  420. * closest element that is smaller than or greater than the one we are
  421. * searching for, respectively, if the exact element cannot be found.
  422. * Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
  423. *
  424. * and an object is returned with the following properties:
  425. *
  426. * - source: The original source file, or null.
  427. * - line: The line number in the original source, or null. The
  428. * line number is 1-based.
  429. * - column: The column number in the original source, or null. The
  430. * column number is 0-based.
  431. * - name: The original identifier, or null.
  432. */
  433. originalPositionFor(aArgs) {
  434. const needle = {
  435. generatedLine: util.getArg(aArgs, "line"),
  436. generatedColumn: util.getArg(aArgs, "column")
  437. };
  438. if (needle.generatedLine < 1) {
  439. throw new Error("Line numbers must be >= 1");
  440. }
  441. if (needle.generatedColumn < 0) {
  442. throw new Error("Column numbers must be >= 0");
  443. }
  444. let bias = util.getArg(aArgs, "bias", SourceMapConsumer.GREATEST_LOWER_BOUND);
  445. if (bias == null) {
  446. bias = SourceMapConsumer.GREATEST_LOWER_BOUND;
  447. }
  448. let mapping;
  449. this._wasm.withMappingCallback(m => mapping = m, () => {
  450. this._wasm.exports.original_location_for(
  451. this._getMappingsPtr(),
  452. needle.generatedLine - 1,
  453. needle.generatedColumn,
  454. bias
  455. );
  456. });
  457. if (mapping) {
  458. if (mapping.generatedLine === needle.generatedLine) {
  459. let source = util.getArg(mapping, "source", null);
  460. if (source !== null) {
  461. source = this._sources.at(source);
  462. source = util.computeSourceURL(this.sourceRoot, source, this._sourceMapURL);
  463. }
  464. let name = util.getArg(mapping, "name", null);
  465. if (name !== null) {
  466. name = this._names.at(name);
  467. }
  468. return {
  469. source,
  470. line: util.getArg(mapping, "originalLine", null),
  471. column: util.getArg(mapping, "originalColumn", null),
  472. name
  473. };
  474. }
  475. }
  476. return {
  477. source: null,
  478. line: null,
  479. column: null,
  480. name: null
  481. };
  482. }
  483. /**
  484. * Return true if we have the source content for every source in the source
  485. * map, false otherwise.
  486. */
  487. hasContentsOfAllSources() {
  488. if (!this.sourcesContent) {
  489. return false;
  490. }
  491. return this.sourcesContent.length >= this._sources.size() &&
  492. !this.sourcesContent.some(function(sc) { return sc == null; });
  493. }
  494. /**
  495. * Returns the original source content. The only argument is the url of the
  496. * original source file. Returns null if no original source content is
  497. * available.
  498. */
  499. sourceContentFor(aSource, nullOnMissing) {
  500. if (!this.sourcesContent) {
  501. return null;
  502. }
  503. const index = this._findSourceIndex(aSource);
  504. if (index >= 0) {
  505. return this.sourcesContent[index];
  506. }
  507. let relativeSource = aSource;
  508. if (this.sourceRoot != null) {
  509. relativeSource = util.relative(this.sourceRoot, relativeSource);
  510. }
  511. let url;
  512. if (this.sourceRoot != null
  513. && (url = util.urlParse(this.sourceRoot))) {
  514. // XXX: file:// URIs and absolute paths lead to unexpected behavior for
  515. // many users. We can help them out when they expect file:// URIs to
  516. // behave like it would if they were running a local HTTP server. See
  517. // https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
  518. const fileUriAbsPath = relativeSource.replace(/^file:\/\//, "");
  519. if (url.scheme == "file"
  520. && this._sources.has(fileUriAbsPath)) {
  521. return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)];
  522. }
  523. if ((!url.path || url.path == "/")
  524. && this._sources.has("/" + relativeSource)) {
  525. return this.sourcesContent[this._sources.indexOf("/" + relativeSource)];
  526. }
  527. }
  528. // This function is used recursively from
  529. // IndexedSourceMapConsumer.prototype.sourceContentFor. In that case, we
  530. // don't want to throw if we can't find the source - we just want to
  531. // return null, so we provide a flag to exit gracefully.
  532. if (nullOnMissing) {
  533. return null;
  534. }
  535. throw new Error('"' + relativeSource + '" is not in the SourceMap.');
  536. }
  537. /**
  538. * Returns the generated line and column information for the original source,
  539. * line, and column positions provided. The only argument is an object with
  540. * the following properties:
  541. *
  542. * - source: The filename of the original source.
  543. * - line: The line number in the original source. The line number
  544. * is 1-based.
  545. * - column: The column number in the original source. The column
  546. * number is 0-based.
  547. * - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
  548. * 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
  549. * closest element that is smaller than or greater than the one we are
  550. * searching for, respectively, if the exact element cannot be found.
  551. * Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
  552. *
  553. * and an object is returned with the following properties:
  554. *
  555. * - line: The line number in the generated source, or null. The
  556. * line number is 1-based.
  557. * - column: The column number in the generated source, or null.
  558. * The column number is 0-based.
  559. */
  560. generatedPositionFor(aArgs) {
  561. let source = util.getArg(aArgs, "source");
  562. source = this._findSourceIndex(source);
  563. if (source < 0) {
  564. return {
  565. line: null,
  566. column: null,
  567. lastColumn: null
  568. };
  569. }
  570. const needle = {
  571. source,
  572. originalLine: util.getArg(aArgs, "line"),
  573. originalColumn: util.getArg(aArgs, "column")
  574. };
  575. if (needle.originalLine < 1) {
  576. throw new Error("Line numbers must be >= 1");
  577. }
  578. if (needle.originalColumn < 0) {
  579. throw new Error("Column numbers must be >= 0");
  580. }
  581. let bias = util.getArg(aArgs, "bias", SourceMapConsumer.GREATEST_LOWER_BOUND);
  582. if (bias == null) {
  583. bias = SourceMapConsumer.GREATEST_LOWER_BOUND;
  584. }
  585. let mapping;
  586. this._wasm.withMappingCallback(m => mapping = m, () => {
  587. this._wasm.exports.generated_location_for(
  588. this._getMappingsPtr(),
  589. needle.source,
  590. needle.originalLine - 1,
  591. needle.originalColumn,
  592. bias
  593. );
  594. });
  595. if (mapping) {
  596. if (mapping.source === needle.source) {
  597. let lastColumn = mapping.lastGeneratedColumn;
  598. if (this._computedColumnSpans && lastColumn === null) {
  599. lastColumn = Infinity;
  600. }
  601. return {
  602. line: util.getArg(mapping, "generatedLine", null),
  603. column: util.getArg(mapping, "generatedColumn", null),
  604. lastColumn,
  605. };
  606. }
  607. }
  608. return {
  609. line: null,
  610. column: null,
  611. lastColumn: null
  612. };
  613. }
  614. }
  615. BasicSourceMapConsumer.prototype.consumer = SourceMapConsumer;
  616. exports.BasicSourceMapConsumer = BasicSourceMapConsumer;
  617. /**
  618. * An IndexedSourceMapConsumer instance represents a parsed source map which
  619. * we can query for information. It differs from BasicSourceMapConsumer in
  620. * that it takes "indexed" source maps (i.e. ones with a "sections" field) as
  621. * input.
  622. *
  623. * The first parameter is a raw source map (either as a JSON string, or already
  624. * parsed to an object). According to the spec for indexed source maps, they
  625. * have the following attributes:
  626. *
  627. * - version: Which version of the source map spec this map is following.
  628. * - file: Optional. The generated file this source map is associated with.
  629. * - sections: A list of section definitions.
  630. *
  631. * Each value under the "sections" field has two fields:
  632. * - offset: The offset into the original specified at which this section
  633. * begins to apply, defined as an object with a "line" and "column"
  634. * field.
  635. * - map: A source map definition. This source map could also be indexed,
  636. * but doesn't have to be.
  637. *
  638. * Instead of the "map" field, it's also possible to have a "url" field
  639. * specifying a URL to retrieve a source map from, but that's currently
  640. * unsupported.
  641. *
  642. * Here's an example source map, taken from the source map spec[0], but
  643. * modified to omit a section which uses the "url" field.
  644. *
  645. * {
  646. * version : 3,
  647. * file: "app.js",
  648. * sections: [{
  649. * offset: {line:100, column:10},
  650. * map: {
  651. * version : 3,
  652. * file: "section.js",
  653. * sources: ["foo.js", "bar.js"],
  654. * names: ["src", "maps", "are", "fun"],
  655. * mappings: "AAAA,E;;ABCDE;"
  656. * }
  657. * }],
  658. * }
  659. *
  660. * The second parameter, if given, is a string whose value is the URL
  661. * at which the source map was found. This URL is used to compute the
  662. * sources array.
  663. *
  664. * [0]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#heading=h.535es3xeprgt
  665. */
  666. class IndexedSourceMapConsumer extends SourceMapConsumer {
  667. constructor(aSourceMap, aSourceMapURL) {
  668. return super(INTERNAL).then(that => {
  669. let sourceMap = aSourceMap;
  670. if (typeof aSourceMap === "string") {
  671. sourceMap = util.parseSourceMapInput(aSourceMap);
  672. }
  673. const version = util.getArg(sourceMap, "version");
  674. const sections = util.getArg(sourceMap, "sections");
  675. if (version != that._version) {
  676. throw new Error("Unsupported version: " + version);
  677. }
  678. that._sources = new ArraySet();
  679. that._names = new ArraySet();
  680. that.__generatedMappings = null;
  681. that.__originalMappings = null;
  682. that.__generatedMappingsUnsorted = null;
  683. that.__originalMappingsUnsorted = null;
  684. let lastOffset = {
  685. line: -1,
  686. column: 0
  687. };
  688. return Promise.all(sections.map(s => {
  689. if (s.url) {
  690. // The url field will require support for asynchronicity.
  691. // See https://github.com/mozilla/source-map/issues/16
  692. throw new Error("Support for url field in sections not implemented.");
  693. }
  694. const offset = util.getArg(s, "offset");
  695. const offsetLine = util.getArg(offset, "line");
  696. const offsetColumn = util.getArg(offset, "column");
  697. if (offsetLine < lastOffset.line ||
  698. (offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
  699. throw new Error("Section offsets must be ordered and non-overlapping.");
  700. }
  701. lastOffset = offset;
  702. const cons = new SourceMapConsumer(util.getArg(s, "map"), aSourceMapURL);
  703. return cons.then(consumer => {
  704. return {
  705. generatedOffset: {
  706. // The offset fields are 0-based, but we use 1-based indices when
  707. // encoding/decoding from VLQ.
  708. generatedLine: offsetLine + 1,
  709. generatedColumn: offsetColumn + 1
  710. },
  711. consumer
  712. };
  713. });
  714. })).then(s => {
  715. that._sections = s;
  716. return that;
  717. });
  718. });
  719. }
  720. // `__generatedMappings` and `__originalMappings` are arrays that hold the
  721. // parsed mapping coordinates from the source map's "mappings" attribute. They
  722. // are lazily instantiated, accessed via the `_generatedMappings` and
  723. // `_originalMappings` getters respectively, and we only parse the mappings
  724. // and create these arrays once queried for a source location. We jump through
  725. // these hoops because there can be many thousands of mappings, and parsing
  726. // them is expensive, so we only want to do it if we must.
  727. //
  728. // Each object in the arrays is of the form:
  729. //
  730. // {
  731. // generatedLine: The line number in the generated code,
  732. // generatedColumn: The column number in the generated code,
  733. // source: The path to the original source file that generated this
  734. // chunk of code,
  735. // originalLine: The line number in the original source that
  736. // corresponds to this chunk of generated code,
  737. // originalColumn: The column number in the original source that
  738. // corresponds to this chunk of generated code,
  739. // name: The name of the original symbol which generated this chunk of
  740. // code.
  741. // }
  742. //
  743. // All properties except for `generatedLine` and `generatedColumn` can be
  744. // `null`.
  745. //
  746. // `_generatedMappings` is ordered by the generated positions.
  747. //
  748. // `_originalMappings` is ordered by the original positions.
  749. get _generatedMappings() {
  750. if (!this.__generatedMappings) {
  751. this._sortGeneratedMappings();
  752. }
  753. return this.__generatedMappings;
  754. }
  755. get _originalMappings() {
  756. if (!this.__originalMappings) {
  757. this._sortOriginalMappings();
  758. }
  759. return this.__originalMappings;
  760. }
  761. get _generatedMappingsUnsorted() {
  762. if (!this.__generatedMappingsUnsorted) {
  763. this._parseMappings(this._mappings, this.sourceRoot);
  764. }
  765. return this.__generatedMappingsUnsorted;
  766. }
  767. get _originalMappingsUnsorted() {
  768. if (!this.__originalMappingsUnsorted) {
  769. this._parseMappings(this._mappings, this.sourceRoot);
  770. }
  771. return this.__originalMappingsUnsorted;
  772. }
  773. _sortGeneratedMappings() {
  774. const mappings = this._generatedMappingsUnsorted;
  775. mappings.sort(util.compareByGeneratedPositionsDeflated);
  776. this.__generatedMappings = mappings;
  777. }
  778. _sortOriginalMappings() {
  779. const mappings = this._originalMappingsUnsorted;
  780. mappings.sort(util.compareByOriginalPositions);
  781. this.__originalMappings = mappings;
  782. }
  783. /**
  784. * The list of original sources.
  785. */
  786. get sources() {
  787. const sources = [];
  788. for (let i = 0; i < this._sections.length; i++) {
  789. for (let j = 0; j < this._sections[i].consumer.sources.length; j++) {
  790. sources.push(this._sections[i].consumer.sources[j]);
  791. }
  792. }
  793. return sources;
  794. }
  795. /**
  796. * Returns the original source, line, and column information for the generated
  797. * source's line and column positions provided. The only argument is an object
  798. * with the following properties:
  799. *
  800. * - line: The line number in the generated source. The line number
  801. * is 1-based.
  802. * - column: The column number in the generated source. The column
  803. * number is 0-based.
  804. *
  805. * and an object is returned with the following properties:
  806. *
  807. * - source: The original source file, or null.
  808. * - line: The line number in the original source, or null. The
  809. * line number is 1-based.
  810. * - column: The column number in the original source, or null. The
  811. * column number is 0-based.
  812. * - name: The original identifier, or null.
  813. */
  814. originalPositionFor(aArgs) {
  815. const needle = {
  816. generatedLine: util.getArg(aArgs, "line"),
  817. generatedColumn: util.getArg(aArgs, "column")
  818. };
  819. // Find the section containing the generated position we're trying to map
  820. // to an original position.
  821. const sectionIndex = binarySearch.search(needle, this._sections,
  822. function(aNeedle, section) {
  823. const cmp = aNeedle.generatedLine - section.generatedOffset.generatedLine;
  824. if (cmp) {
  825. return cmp;
  826. }
  827. return (aNeedle.generatedColumn -
  828. section.generatedOffset.generatedColumn);
  829. });
  830. const section = this._sections[sectionIndex];
  831. if (!section) {
  832. return {
  833. source: null,
  834. line: null,
  835. column: null,
  836. name: null
  837. };
  838. }
  839. return section.consumer.originalPositionFor({
  840. line: needle.generatedLine -
  841. (section.generatedOffset.generatedLine - 1),
  842. column: needle.generatedColumn -
  843. (section.generatedOffset.generatedLine === needle.generatedLine
  844. ? section.generatedOffset.generatedColumn - 1
  845. : 0),
  846. bias: aArgs.bias
  847. });
  848. }
  849. /**
  850. * Return true if we have the source content for every source in the source
  851. * map, false otherwise.
  852. */
  853. hasContentsOfAllSources() {
  854. return this._sections.every(function(s) {
  855. return s.consumer.hasContentsOfAllSources();
  856. });
  857. }
  858. /**
  859. * Returns the original source content. The only argument is the url of the
  860. * original source file. Returns null if no original source content is
  861. * available.
  862. */
  863. sourceContentFor(aSource, nullOnMissing) {
  864. for (let i = 0; i < this._sections.length; i++) {
  865. const section = this._sections[i];
  866. const content = section.consumer.sourceContentFor(aSource, true);
  867. if (content) {
  868. return content;
  869. }
  870. }
  871. if (nullOnMissing) {
  872. return null;
  873. }
  874. throw new Error('"' + aSource + '" is not in the SourceMap.');
  875. }
  876. /**
  877. * Returns the generated line and column information for the original source,
  878. * line, and column positions provided. The only argument is an object with
  879. * the following properties:
  880. *
  881. * - source: The filename of the original source.
  882. * - line: The line number in the original source. The line number
  883. * is 1-based.
  884. * - column: The column number in the original source. The column
  885. * number is 0-based.
  886. *
  887. * and an object is returned with the following properties:
  888. *
  889. * - line: The line number in the generated source, or null. The
  890. * line number is 1-based.
  891. * - column: The column number in the generated source, or null.
  892. * The column number is 0-based.
  893. */
  894. generatedPositionFor(aArgs) {
  895. for (let i = 0; i < this._sections.length; i++) {
  896. const section = this._sections[i];
  897. // Only consider this section if the requested source is in the list of
  898. // sources of the consumer.
  899. if (section.consumer._findSourceIndex(util.getArg(aArgs, "source")) === -1) {
  900. continue;
  901. }
  902. const generatedPosition = section.consumer.generatedPositionFor(aArgs);
  903. if (generatedPosition) {
  904. const ret = {
  905. line: generatedPosition.line +
  906. (section.generatedOffset.generatedLine - 1),
  907. column: generatedPosition.column +
  908. (section.generatedOffset.generatedLine === generatedPosition.line
  909. ? section.generatedOffset.generatedColumn - 1
  910. : 0)
  911. };
  912. return ret;
  913. }
  914. }
  915. return {
  916. line: null,
  917. column: null
  918. };
  919. }
  920. /**
  921. * Parse the mappings in a string in to a data structure which we can easily
  922. * query (the ordered arrays in the `this.__generatedMappings` and
  923. * `this.__originalMappings` properties).
  924. */
  925. _parseMappings(aStr, aSourceRoot) {
  926. const generatedMappings = this.__generatedMappingsUnsorted = [];
  927. const originalMappings = this.__originalMappingsUnsorted = [];
  928. for (let i = 0; i < this._sections.length; i++) {
  929. const section = this._sections[i];
  930. const sectionMappings = [];
  931. section.consumer.eachMapping(m => sectionMappings.push(m));
  932. for (let j = 0; j < sectionMappings.length; j++) {
  933. const mapping = sectionMappings[j];
  934. // TODO: test if null is correct here. The original code used
  935. // `source`, which would actually have gotten used as null because
  936. // var's get hoisted.
  937. // See: https://github.com/mozilla/source-map/issues/333
  938. let source = util.computeSourceURL(section.consumer.sourceRoot, null, this._sourceMapURL);
  939. this._sources.add(source);
  940. source = this._sources.indexOf(source);
  941. let name = null;
  942. if (mapping.name) {
  943. this._names.add(mapping.name);
  944. name = this._names.indexOf(mapping.name);
  945. }
  946. // The mappings coming from the consumer for the section have
  947. // generated positions relative to the start of the section, so we
  948. // need to offset them to be relative to the start of the concatenated
  949. // generated file.
  950. const adjustedMapping = {
  951. source,
  952. generatedLine: mapping.generatedLine +
  953. (section.generatedOffset.generatedLine - 1),
  954. generatedColumn: mapping.generatedColumn +
  955. (section.generatedOffset.generatedLine === mapping.generatedLine
  956. ? section.generatedOffset.generatedColumn - 1
  957. : 0),
  958. originalLine: mapping.originalLine,
  959. originalColumn: mapping.originalColumn,
  960. name
  961. };
  962. generatedMappings.push(adjustedMapping);
  963. if (typeof adjustedMapping.originalLine === "number") {
  964. originalMappings.push(adjustedMapping);
  965. }
  966. }
  967. }
  968. }
  969. eachMapping(aCallback, aContext, aOrder) {
  970. const context = aContext || null;
  971. const order = aOrder || SourceMapConsumer.GENERATED_ORDER;
  972. let mappings;
  973. switch (order) {
  974. case SourceMapConsumer.GENERATED_ORDER:
  975. mappings = this._generatedMappings;
  976. break;
  977. case SourceMapConsumer.ORIGINAL_ORDER:
  978. mappings = this._originalMappings;
  979. break;
  980. default:
  981. throw new Error("Unknown order of iteration.");
  982. }
  983. const sourceRoot = this.sourceRoot;
  984. mappings.map(function(mapping) {
  985. let source = null;
  986. if (mapping.source !== null) {
  987. source = this._sources.at(mapping.source);
  988. source = util.computeSourceURL(sourceRoot, source, this._sourceMapURL);
  989. }
  990. return {
  991. source,
  992. generatedLine: mapping.generatedLine,
  993. generatedColumn: mapping.generatedColumn,
  994. originalLine: mapping.originalLine,
  995. originalColumn: mapping.originalColumn,
  996. name: mapping.name === null ? null : this._names.at(mapping.name)
  997. };
  998. }, this).forEach(aCallback, context);
  999. }
  1000. /**
  1001. * Find the mapping that best matches the hypothetical "needle" mapping that
  1002. * we are searching for in the given "haystack" of mappings.
  1003. */
  1004. _findMapping(aNeedle, aMappings, aLineName,
  1005. aColumnName, aComparator, aBias) {
  1006. // To return the position we are searching for, we must first find the
  1007. // mapping for the given position and then return the opposite position it
  1008. // points to. Because the mappings are sorted, we can use binary search to
  1009. // find the best mapping.
  1010. if (aNeedle[aLineName] <= 0) {
  1011. throw new TypeError("Line must be greater than or equal to 1, got "
  1012. + aNeedle[aLineName]);
  1013. }
  1014. if (aNeedle[aColumnName] < 0) {
  1015. throw new TypeError("Column must be greater than or equal to 0, got "
  1016. + aNeedle[aColumnName]);
  1017. }
  1018. return binarySearch.search(aNeedle, aMappings, aComparator, aBias);
  1019. }
  1020. allGeneratedPositionsFor(aArgs) {
  1021. const line = util.getArg(aArgs, "line");
  1022. // When there is no exact match, BasicSourceMapConsumer.prototype._findMapping
  1023. // returns the index of the closest mapping less than the needle. By
  1024. // setting needle.originalColumn to 0, we thus find the last mapping for
  1025. // the given line, provided such a mapping exists.
  1026. const needle = {
  1027. source: util.getArg(aArgs, "source"),
  1028. originalLine: line,
  1029. originalColumn: util.getArg(aArgs, "column", 0)
  1030. };
  1031. needle.source = this._findSourceIndex(needle.source);
  1032. if (needle.source < 0) {
  1033. return [];
  1034. }
  1035. if (needle.originalLine < 1) {
  1036. throw new Error("Line numbers must be >= 1");
  1037. }
  1038. if (needle.originalColumn < 0) {
  1039. throw new Error("Column numbers must be >= 0");
  1040. }
  1041. const mappings = [];
  1042. let index = this._findMapping(needle,
  1043. this._originalMappings,
  1044. "originalLine",
  1045. "originalColumn",
  1046. util.compareByOriginalPositions,
  1047. binarySearch.LEAST_UPPER_BOUND);
  1048. if (index >= 0) {
  1049. let mapping = this._originalMappings[index];
  1050. if (aArgs.column === undefined) {
  1051. const originalLine = mapping.originalLine;
  1052. // Iterate until either we run out of mappings, or we run into
  1053. // a mapping for a different line than the one we found. Since
  1054. // mappings are sorted, this is guaranteed to find all mappings for
  1055. // the line we found.
  1056. while (mapping && mapping.originalLine === originalLine) {
  1057. let lastColumn = mapping.lastGeneratedColumn;
  1058. if (this._computedColumnSpans && lastColumn === null) {
  1059. lastColumn = Infinity;
  1060. }
  1061. mappings.push({
  1062. line: util.getArg(mapping, "generatedLine", null),
  1063. column: util.getArg(mapping, "generatedColumn", null),
  1064. lastColumn,
  1065. });
  1066. mapping = this._originalMappings[++index];
  1067. }
  1068. } else {
  1069. const originalColumn = mapping.originalColumn;
  1070. // Iterate until either we run out of mappings, or we run into
  1071. // a mapping for a different line than the one we were searching for.
  1072. // Since mappings are sorted, this is guaranteed to find all mappings for
  1073. // the line we are searching for.
  1074. while (mapping &&
  1075. mapping.originalLine === line &&
  1076. mapping.originalColumn == originalColumn) {
  1077. let lastColumn = mapping.lastGeneratedColumn;
  1078. if (this._computedColumnSpans && lastColumn === null) {
  1079. lastColumn = Infinity;
  1080. }
  1081. mappings.push({
  1082. line: util.getArg(mapping, "generatedLine", null),
  1083. column: util.getArg(mapping, "generatedColumn", null),
  1084. lastColumn,
  1085. });
  1086. mapping = this._originalMappings[++index];
  1087. }
  1088. }
  1089. }
  1090. return mappings;
  1091. }
  1092. destroy() {
  1093. for (let i = 0; i < this._sections.length; i++) {
  1094. this._sections[i].consumer.destroy();
  1095. }
  1096. }
  1097. }
  1098. exports.IndexedSourceMapConsumer = IndexedSourceMapConsumer;
  1099. /*
  1100. * Cheat to get around inter-twingled classes. `factory()` can be at the end
  1101. * where it has access to non-hoisted classes, but it gets hoisted itself.
  1102. */
  1103. function _factory(aSourceMap, aSourceMapURL) {
  1104. let sourceMap = aSourceMap;
  1105. if (typeof aSourceMap === "string") {
  1106. sourceMap = util.parseSourceMapInput(aSourceMap);
  1107. }
  1108. const consumer = sourceMap.sections != null
  1109. ? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
  1110. : new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
  1111. return Promise.resolve(consumer);
  1112. }
  1113. function _factoryBSM(aSourceMap, aSourceMapURL) {
  1114. return BasicSourceMapConsumer.fromSourceMap(aSourceMap, aSourceMapURL);
  1115. }