MCPcopy
hub / github.com/mozilla/source-map

github.com/mozilla/source-map @0.7.6 sqlite

repository ↗ · DeepWiki ↗ · release 0.7.6 ↗
198 symbols 460 edges 39 files 75 documented · 38%
README

Source Map

NPM

This is a library to generate and consume the source map format described here.

Use with Node

$ npm install source-map

Use on the Web

<script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
<script>
  sourceMap.SourceMapConsumer.initialize({
    "lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm",
  });
</script>

Table of Contents

Examples

Consuming a source map

const rawSourceMap = {
  version: 3,
  file: "min.js",
  names: ["bar", "baz", "n"],
  sources: ["one.js", "two.js"],
  sourceRoot: "http://example.com/www/js/",
  mappings:
    "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA",
};

const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
  console.log(consumer.sources);
  // [ 'http://example.com/www/js/one.js',
  //   'http://example.com/www/js/two.js' ]

  console.log(
    consumer.originalPositionFor({
      line: 2,
      column: 28,
    })
  );
  // { source: 'http://example.com/www/js/two.js',
  //   line: 2,
  //   column: 10,
  //   name: 'n' }

  console.log(
    consumer.generatedPositionFor({
      source: "http://example.com/www/js/two.js",
      line: 2,
      column: 10,
    })
  );
  // { line: 2, column: 28 }

  consumer.eachMapping(function (m) {
    // ...
  });

  return computeWhatever();
});

Generating a source map

In depth guide: Compiling to JavaScript, and Debugging with Source Maps

With SourceNode (high level API)

function compile(ast) {
  switch (ast.type) {
    case "BinaryExpression":
      return new SourceNode(
        ast.location.line,
        ast.location.column,
        ast.location.source,
        [compile(ast.left), " + ", compile(ast.right)]
      );
    case "Literal":
      return new SourceNode(
        ast.location.line,
        ast.location.column,
        ast.location.source,
        String(ast.value)
      );
    // ...
    default:
      throw new Error("Bad AST");
  }
}

var ast = parse("40 + 2", "add.js");
console.log(
  compile(ast).toStringWithSourceMap({
    file: "add.js",
  })
);
// { code: '40 + 2',
//   map: [object SourceMapGenerator] }

With SourceMapGenerator (low level API)

var map = new SourceMapGenerator({
  file: "source-mapped.js",
});

map.addMapping({
  generated: {
    line: 10,
    column: 35,
  },
  source: "foo.js",
  original: {
    line: 33,
    column: 2,
  },
  name: "christopher",
});

console.log(map.toString());
// '{"version":3,"file":"source-mapped.js","sources":["foo.js"],"names":["christopher"],"mappings":";;;;;;;;;mCAgCEA"}'

API

Get a reference to the module:

// Node.js
var sourceMap = require("source-map");

// Browser builds
var sourceMap = window.sourceMap;

// Inside Firefox
const sourceMap = require("devtools/toolkit/sourcemap/source-map.js");

SourceMapConsumer

A SourceMapConsumer instance represents a parsed source map which we can query for information about the original file positions by giving it a file position in the generated source.

SourceMapConsumer.initialize(options)

When using SourceMapConsumer outside of node.js, for example on the Web, it needs to know from what URL to load lib/mappings.wasm. You must inform it by calling initialize before constructing any SourceMapConsumers.

The options object has the following properties:

  • "lib/mappings.wasm": A String containing the URL of the lib/mappings.wasm file, or an ArrayBuffer with the contents of lib/mappings.wasm.
sourceMap.SourceMapConsumer.initialize({
  "lib/mappings.wasm": "https://example.com/source-map/lib/mappings.wasm",
});

new SourceMapConsumer(rawSourceMap)

The only parameter is the raw source map (either as a string which can be JSON.parse'd, or an object). According to the spec, source maps have the following attributes:

  • version: Which version of the source map spec this map is following.

  • sources: An array of URLs to the original source files.

  • names: An array of identifiers which can be referenced by individual mappings.

  • sourceRoot: Optional. The URL root from which all sources are relative.

  • sourcesContent: Optional. An array of contents of the original source files.

  • mappings: A string of base64 VLQs which contain the actual mappings.

  • file: Optional. The generated filename this source map is associated with.

  • x_google_ignoreList: Optional. An additional extension field which is an array of indices refering to urls in the sources array. This is used to identify third-party sources, that the developer might want to avoid when debugging. Read more

The promise of the constructed souce map consumer is returned.

When the SourceMapConsumer will no longer be used anymore, you must call its destroy method.

const consumer = await new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
doStuffWith(consumer);
consumer.destroy();

Alternatively, you can use SourceMapConsumer.with to avoid needing to remember to call destroy.

SourceMapConsumer.with

Construct a new SourceMapConsumer from rawSourceMap and sourceMapUrl (see the SourceMapConsumer constructor for details. Then, invoke the async function f(SourceMapConsumer) -> T with the newly constructed consumer, wait for f to complete, call destroy on the consumer, and return f's return value.

You must not use the consumer after f completes!

By using with, you do not have to remember to manually call destroy on the consumer, since it will be called automatically once f completes.

const xSquared = await SourceMapConsumer.with(
  myRawSourceMap,
  null,
  async function (consumer) {
    // Use `consumer` inside here and don't worry about remembering
    // to call `destroy`.

    const x = await whatever(consumer);
    return x * x;
  }
);

// You may not use that `consumer` anymore out here; it has
// been destroyed. But you can use `xSquared`.
console.log(xSquared);

SourceMapConsumer.prototype.destroy()

Free this source map consumer's associated wasm data that is manually-managed.

consumer.destroy();

Alternatively, you can use SourceMapConsumer.with to avoid needing to remember to call destroy.

SourceMapConsumer.prototype.computeColumnSpans()

Compute the last column for each generated mapping. The last column is inclusive.

// Before:
consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" });
// [ { line: 2,
//     column: 1 },
//   { line: 2,
//     column: 10 },
//   { line: 2,
//     column: 20 } ]

consumer.computeColumnSpans();

// After:
consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" });
// [ { line: 2,
//     column: 1,
//     lastColumn: 9 },
//   { line: 2,
//     column: 10,
//     lastColumn: 19 },
//   { line: 2,
//     column: 20,
//     lastColumn: Infinity } ]

SourceMapConsumer.prototype.originalPositionFor(generatedPosition)

Returns the original source, line, and column information for the generated source's line and column positions provided. The only argument is an object with the following properties:

  • line: The line number in the generated source. Line numbers in this library are 1-based (note that the underlying source map specification uses 0-based line numbers -- this library handles the translation).

  • column: The column number in the generated source. Column numbers in this library are 0-based.

  • bias: Either SourceMapConsumer.GREATEST_LOWER_BOUND or SourceMapConsumer.LEAST_UPPER_BOUND. Specifies whether to return the closest element that is smaller than or greater than the one we are searching for, respectively, if the exact element cannot be found. Defaults to SourceMapConsumer.GREATEST_LOWER_BOUND.

and an object is returned with the following properties:

  • source: The original source file, or null if this information is not available.

  • line: The line number in the original source, or null if this information is not available. The line number is 1-based.

  • column: The column number in the original source, or null if this information is not available. The column number is 0-based.

  • name: The original identifier, or null if this information is not available.

consumer.originalPositionFor({ line: 2, column: 10 });
// { source: 'foo.coffee',
//   line: 2,
//   column: 2,
//   name: null }

consumer.originalPositionFor({
  line: 99999999999999999,
  column: 999999999999999,
});
// { source: null,
//   line: null,
//   column: null,
//   name: null }

SourceMapConsumer.prototype.generatedPositionFor(originalPosition)

Returns the generated line and column information for the original source, line, and column positions provided. The only argument is an object with the following properties:

  • source: The filename of the original source.

  • line: The line number in the original source. The line number is 1-based.

  • column: The column number in the original source. The column number is 0-based.

and an object is returned with the following properties:

  • line: The line number in the generated source, or null. The line number is 1-based.

  • column: The column number in the generated source, or null. The column number is 0-based.

consumer.generatedPositionFor({ source: "example.js", line: 2, column: 10 });
// { line: 1,
//   column: 56 }

SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)

Returns all generated line and column information for the original source, line, and column provided. If no column is provided, returns all mappings corresponding to a either the line we are searching for or the next closest line that has any mappings. Otherwise, returns all mappings corresponding to the given line and either th

Extension points exported contracts — how you extend this code

BasicSourceMapConsumerConstructor (Interface)
(no doc) [3 implementers]
source-map.d.ts
SourceMapConsumerConstructor (Interface)
(no doc) [1 implementers]
source-map.d.ts
StartOfSourceMap (Interface)
(no doc)
source-map.d.ts
RawSourceMap (Interface)
(no doc)
source-map.d.ts
RawIndexMap (Interface)
(no doc)
source-map.d.ts

Core symbols most depended-on inside this repo

addMapping
called by 131
lib/source-map-generator.js
join
called by 118
lib/source-node.js
destroy
called by 108
source-map.d.ts
toJSON
called by 59
lib/source-map-generator.js
add
called by 36
lib/array-set.js
sourceContentFor
called by 33
source-map.d.ts
toString
called by 32
lib/source-node.js
originalPositionFor
called by 29
source-map.d.ts

Shape

Function 82
Method 80
Class 18
Interface 18

Languages

TypeScript92%
Python8%

Modules by API surface

lib/source-map-consumer.js40 symbols
source-map.d.ts34 symbols
lib/util.js18 symbols
lib/wasm.js16 symbols
lib/source-node.js16 symbols
lib/source-map-generator.js12 symbols
lib/array-set.js10 symbols
wasm-mappings/source-map-mappings-wasm-api/who-calls.py8 symbols
wasm-mappings/source-map-mappings-wasm-api/build.py8 symbols
lib/mapping-list.js7 symbols
test/test-spec-tests.js5 symbols
bench/bench-dom-bindings.js4 symbols

Dependencies from manifests, versioned

c87.12.0 · 1×
doctoc2.2.1 · 1×
eslint8.24.0 · 1×
eslint-config-prettier8.5.0 · 1×
prettier2.7.1 · 1×
webpack4.9.1 · 1×
webpack-cli3.1 · 1×

For agents

$ claude mcp add source-map \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact