source-map
Advanced tools
Comparing version 0.4.4 to 0.5.0
{ | ||
"name": "source-map", | ||
"description": "Generates and consumes source maps", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"homepage": "https://github.com/mozilla/source-map", | ||
@@ -46,24 +46,20 @@ "author": "Nick Fitzgerald <nfitzgerald@mozilla.com>", | ||
}, | ||
"directories": { | ||
"lib": "./lib" | ||
}, | ||
"main": "./lib/source-map.js", | ||
"main": "./source-map.js", | ||
"files": [ | ||
"lib/", | ||
"build/" | ||
"source-map.js", | ||
"lib/" | ||
], | ||
"engines": { | ||
"node": ">=0.8.0" | ||
"node": ">=0.10.0" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"dependencies": { | ||
"amdefine": ">=0.0.4" | ||
"scripts": { | ||
"test": "node test/run-tests.js", | ||
"build": "webpack --color", | ||
"toc": "doctoc --title '## Table of Contents' README.md && doctoc --title '## Table of Contents' CONTRIBUTING.md" | ||
}, | ||
"devDependencies": { | ||
"dryice": ">=0.4.8" | ||
}, | ||
"scripts": { | ||
"test": "node test/run-tests.js", | ||
"build": "node Makefile.dryice.js" | ||
"doctoc": "^0.15.0", | ||
"webpack": "^1.12.0" | ||
} | ||
} |
353
README.md
# Source Map | ||
[![Build Status](https://travis-ci.org/mozilla/source-map.png?branch=master)](https://travis-ci.org/mozilla/source-map) | ||
[![NPM](https://nodei.co/npm/source-map.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/source-map) | ||
This is a library to generate and consume the source map format | ||
[described here][format]. | ||
This library is written in the Asynchronous Module Definition format, and works | ||
in the following environments: | ||
[format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit | ||
* Modern Browsers supporting ECMAScript 5 (either after the build, or with an | ||
AMD loader such as RequireJS) | ||
## Use with Node | ||
* Inside Firefox (as a JSM file, after the build) | ||
* With NodeJS versions 0.8.X and higher | ||
## Node | ||
$ npm install source-map | ||
## Building from Source (for everywhere else) | ||
## Use on the Web | ||
Install Node and then run | ||
<script src="https://raw.githubusercontent.com/mozilla/source-map/master/dist/source-map.min.js" defer></script> | ||
$ git clone https://fitzgen@github.com/mozilla/source-map.git | ||
$ cd source-map | ||
$ npm link . | ||
-------------------------------------------------------------------------------- | ||
Next, run | ||
<!-- `npm run toc` to regenerate the Table of Contents --> | ||
$ node Makefile.dryice.js | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Table of Contents | ||
This should spew a bunch of stuff to stdout, and create the following files: | ||
- [Examples](#examples) | ||
- [Consuming a source map](#consuming-a-source-map) | ||
- [Generating a source map](#generating-a-source-map) | ||
- [With SourceNode (high level API)](#with-sourcenode-high-level-api) | ||
- [With SourceMapGenerator (low level API)](#with-sourcemapgenerator-low-level-api) | ||
- [API](#api) | ||
- [SourceMapConsumer](#sourcemapconsumer) | ||
- [new SourceMapConsumer(rawSourceMap)](#new-sourcemapconsumerrawsourcemap) | ||
- [SourceMapConsumer.prototype.computeColumnSpans()](#sourcemapconsumerprototypecomputecolumnspans) | ||
- [SourceMapConsumer.prototype.originalPositionFor(generatedPosition)](#sourcemapconsumerprototypeoriginalpositionforgeneratedposition) | ||
- [SourceMapConsumer.prototype.generatedPositionFor(originalPosition)](#sourcemapconsumerprototypegeneratedpositionfororiginalposition) | ||
- [SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)](#sourcemapconsumerprototypeallgeneratedpositionsfororiginalposition) | ||
- [SourceMapConsumer.prototype.hasContentsOfAllSources()](#sourcemapconsumerprototypehascontentsofallsources) | ||
- [SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])](#sourcemapconsumerprototypesourcecontentforsource-returnnullonmissing) | ||
- [SourceMapConsumer.prototype.eachMapping(callback, context, order)](#sourcemapconsumerprototypeeachmappingcallback-context-order) | ||
- [SourceMapGenerator](#sourcemapgenerator) | ||
- [new SourceMapGenerator([startOfSourceMap])](#new-sourcemapgeneratorstartofsourcemap) | ||
- [SourceMapGenerator.fromSourceMap(sourceMapConsumer)](#sourcemapgeneratorfromsourcemapsourcemapconsumer) | ||
- [SourceMapGenerator.prototype.addMapping(mapping)](#sourcemapgeneratorprototypeaddmappingmapping) | ||
- [SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)](#sourcemapgeneratorprototypesetsourcecontentsourcefile-sourcecontent) | ||
- [SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])](#sourcemapgeneratorprototypeapplysourcemapsourcemapconsumer-sourcefile-sourcemappath) | ||
- [SourceMapGenerator.prototype.toString()](#sourcemapgeneratorprototypetostring) | ||
- [SourceNode](#sourcenode) | ||
- [new SourceNode([line, column, source[, chunk[, name]]])](#new-sourcenodeline-column-source-chunk-name) | ||
- [SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath])](#sourcenodefromstringwithsourcemapcode-sourcemapconsumer-relativepath) | ||
- [SourceNode.prototype.add(chunk)](#sourcenodeprototypeaddchunk) | ||
- [SourceNode.prototype.prepend(chunk)](#sourcenodeprototypeprependchunk) | ||
- [SourceNode.prototype.setSourceContent(sourceFile, sourceContent)](#sourcenodeprototypesetsourcecontentsourcefile-sourcecontent) | ||
- [SourceNode.prototype.walk(fn)](#sourcenodeprototypewalkfn) | ||
- [SourceNode.prototype.walkSourceContents(fn)](#sourcenodeprototypewalksourcecontentsfn) | ||
- [SourceNode.prototype.join(sep)](#sourcenodeprototypejoinsep) | ||
- [SourceNode.prototype.replaceRight(pattern, replacement)](#sourcenodeprototypereplacerightpattern-replacement) | ||
- [SourceNode.prototype.toString()](#sourcenodeprototypetostring) | ||
- [SourceNode.prototype.toStringWithSourceMap([startOfSourceMap])](#sourcenodeprototypetostringwithsourcemapstartofsourcemap) | ||
* `dist/source-map.js` - The unminified browser version. | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
* `dist/source-map.min.js` - The minified browser version. | ||
* `dist/SourceMap.jsm` - The JavaScript Module for inclusion in Firefox source. | ||
## Examples | ||
@@ -148,3 +173,3 @@ | ||
```js | ||
// NodeJS | ||
// Node.js | ||
var sourceMap = require('source-map'); | ||
@@ -156,4 +181,3 @@ | ||
// Inside Firefox | ||
let sourceMap = {}; | ||
Components.utils.import('resource:///modules/devtools/SourceMap.jsm', sourceMap); | ||
const sourceMap = require("devtools/toolkit/sourcemap/source-map.js"); | ||
``` | ||
@@ -188,2 +212,6 @@ | ||
```js | ||
var consumer = new sourceMap.SourceMapConsumer(rawSourceMapJsonData); | ||
``` | ||
#### SourceMapConsumer.prototype.computeColumnSpans() | ||
@@ -194,2 +222,28 @@ | ||
```js | ||
// 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) | ||
@@ -224,2 +278,16 @@ | ||
```js | ||
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) | ||
@@ -243,2 +311,8 @@ | ||
```js | ||
consumer.generatedPositionFor({ source: "example.js", line: 2, column: 10 }) | ||
// { line: 1, | ||
// column: 56 } | ||
``` | ||
#### SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition) | ||
@@ -267,2 +341,12 @@ | ||
```js | ||
consumer.allGeneratedpositionsfor({ line: 2, source: "foo.coffee" }) | ||
// [ { line: 2, | ||
// column: 1 }, | ||
// { line: 2, | ||
// column: 10 }, | ||
// { line: 2, | ||
// column: 20 } ] | ||
``` | ||
#### SourceMapConsumer.prototype.hasContentsOfAllSources() | ||
@@ -273,5 +357,16 @@ | ||
In other words, if this method returns `true`, then `smc.sourceContentFor(s)` | ||
will succeed for every source `s` in `smc.sources`. | ||
In other words, if this method returns `true`, then | ||
`consumer.sourceContentFor(s)` will succeed for every source `s` in | ||
`consumer.sources`. | ||
```js | ||
// ... | ||
if (consumer.hasContentsOfAllSources()) { | ||
consumerReadyCallback(consumer); | ||
} else { | ||
fetchSources(consumer, consumerReadyCallback); | ||
} | ||
// ... | ||
``` | ||
#### SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing]) | ||
@@ -286,2 +381,16 @@ | ||
```js | ||
consumer.sources | ||
// [ "my-cool-lib.clj" ] | ||
consumer.sourceContentFor("my-cool-lib.clj") | ||
// "..." | ||
consumer.sourceContentFor("this is not in the source map"); | ||
// Error: "this is not in the source map" is not in the source map | ||
consumer.sourceContentFor("this is not in the source map", true); | ||
// null | ||
``` | ||
#### SourceMapConsumer.prototype.eachMapping(callback, context, order) | ||
@@ -305,2 +414,19 @@ | ||
```js | ||
consumer.eachMapping(function (m) { console.log(m); }) | ||
// ... | ||
// { source: 'illmatic.js', | ||
// generatedLine: 1, | ||
// generatedColumn: 0, | ||
// originalLine: 1, | ||
// originalColumn: 0, | ||
// name: null } | ||
// { source: 'illmatic.js', | ||
// generatedLine: 2, | ||
// generatedColumn: 0, | ||
// originalLine: 2, | ||
// originalColumn: 0, | ||
// name: null } | ||
// ... | ||
``` | ||
### SourceMapGenerator | ||
@@ -325,8 +451,19 @@ | ||
```js | ||
var generator = new sourceMap.SourceMapGenerator({ | ||
file: "my-generated-javascript-file.js", | ||
sourceRoot: "http://example.com/app/js/" | ||
}); | ||
``` | ||
#### SourceMapGenerator.fromSourceMap(sourceMapConsumer) | ||
Creates a new SourceMapGenerator based on a SourceMapConsumer | ||
Creates a new `SourceMapGenerator` from an existing `SourceMapConsumer` instance. | ||
* `sourceMapConsumer` The SourceMap. | ||
```js | ||
var generator = sourceMap.SourceMapGenerator.fromSourceMap(consumer); | ||
``` | ||
#### SourceMapGenerator.prototype.addMapping(mapping) | ||
@@ -346,2 +483,10 @@ | ||
```js | ||
generator.addMapping({ | ||
source: "module-one.scm", | ||
original: { line: 128, column: 0 }, | ||
generated: { line: 3, column: 456 } | ||
}) | ||
``` | ||
#### SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent) | ||
@@ -355,2 +500,7 @@ | ||
```js | ||
generator.setSourceContent("module-one.scm", | ||
fs.readFileSync("path/to/module-one.scm")) | ||
``` | ||
#### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]]) | ||
@@ -384,2 +534,7 @@ | ||
```js | ||
generator.toString() | ||
// '{"version":3,"sources":["module-one.scm"],"names":[],"mappings":"...snip...","file":"my-generated-javascript-file.js","sourceRoot":"http://example.com/app/js/"}' | ||
``` | ||
### SourceNode | ||
@@ -408,2 +563,10 @@ | ||
```js | ||
var node = new SourceNode(1, 2, "a.cpp", [ | ||
new SourceNode(3, 4, "b.cpp", "extern int status;\n"), | ||
new SourceNode(5, 6, "c.cpp", "std::string* make_string(size_t n);\n"), | ||
new SourceNode(7, 8, "d.cpp", "int main(int argc, char** argv) {}\n"), | ||
]); | ||
``` | ||
#### SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath]) | ||
@@ -420,2 +583,8 @@ | ||
```js | ||
var consumer = new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map")); | ||
var node = SourceNode.fromStringWithSourceMap(fs.readFileSync("path/to/my-file.js"), | ||
consumer); | ||
``` | ||
#### SourceNode.prototype.add(chunk) | ||
@@ -428,2 +597,8 @@ | ||
```js | ||
node.add(" + "); | ||
node.add(otherNode); | ||
node.add([leftHandOperandNode, " + ", rightHandOperandNode]); | ||
``` | ||
#### SourceNode.prototype.prepend(chunk) | ||
@@ -436,2 +611,6 @@ | ||
```js | ||
node.prepend("/** Build Id: f783haef86324gf **/\n\n"); | ||
``` | ||
#### SourceNode.prototype.setSourceContent(sourceFile, sourceContent) | ||
@@ -446,2 +625,7 @@ | ||
```js | ||
node.setSourceContent("module-one.scm", | ||
fs.readFileSync("path/to/module-one.scm")) | ||
``` | ||
#### SourceNode.prototype.walk(fn) | ||
@@ -455,2 +639,19 @@ | ||
```js | ||
var node = new SourceNode(1, 2, "a.js", [ | ||
new SourceNode(3, 4, "b.js", "uno"), | ||
"dos", | ||
[ | ||
"tres", | ||
new SourceNode(5, 6, "c.js", "quatro") | ||
] | ||
]); | ||
node.walk(function (code, loc) { console.log("WALK:", code, loc); }) | ||
// WALK: uno { source: 'b.js', line: 3, column: 4, name: null } | ||
// WALK: dos { source: 'a.js', line: 1, column: 2, name: null } | ||
// WALK: tres { source: 'a.js', line: 1, column: 2, name: null } | ||
// WALK: quatro { source: 'c.js', line: 5, column: 6, name: null } | ||
``` | ||
#### SourceNode.prototype.walkSourceContents(fn) | ||
@@ -463,2 +664,17 @@ | ||
```js | ||
var a = new SourceNode(1, 2, "a.js", "generated from a"); | ||
a.setSourceContent("a.js", "original a"); | ||
var b = new SourceNode(1, 2, "b.js", "generated from b"); | ||
b.setSourceContent("b.js", "original b"); | ||
var c = new SourceNode(1, 2, "c.js", "generated from c"); | ||
c.setSourceContent("c.js", "original c"); | ||
var node = new SourceNode(null, null, null, [a, b, c]); | ||
node.walkSourceContents(function (source, contents) { console.log("WALK:", source, ":", contents); }) | ||
// WALK: a.js : original a | ||
// WALK: b.js : original b | ||
// WALK: c.js : original c | ||
``` | ||
#### SourceNode.prototype.join(sep) | ||
@@ -471,6 +687,15 @@ | ||
```js | ||
var lhs = new SourceNode(1, 2, "a.rs", "my_copy"); | ||
var operand = new SourceNode(3, 4, "a.rs", "="); | ||
var rhs = new SourceNode(5, 6, "a.rs", "orig.clone()"); | ||
var node = new SourceNode(null, null, null, [ lhs, operand, rhs ]); | ||
var joinedNode = node.join(" "); | ||
``` | ||
#### SourceNode.prototype.replaceRight(pattern, replacement) | ||
Call `String.prototype.replace` on the very right-most source snippet. Useful | ||
for trimming whitespace from the end of a source node, etc. | ||
for trimming white space from the end of a source node, etc. | ||
@@ -481,2 +706,7 @@ * `pattern`: The pattern to replace. | ||
```js | ||
// Trim trailing white space. | ||
node.replaceRight(/\s*$/, ""); | ||
``` | ||
#### SourceNode.prototype.toString() | ||
@@ -487,2 +717,16 @@ | ||
```js | ||
var node = new SourceNode(1, 2, "a.js", [ | ||
new SourceNode(3, 4, "b.js", "uno"), | ||
"dos", | ||
[ | ||
"tres", | ||
new SourceNode(5, 6, "c.js", "quatro") | ||
] | ||
]); | ||
node.toString() | ||
// 'unodostresquatro' | ||
``` | ||
#### SourceNode.prototype.toStringWithSourceMap([startOfSourceMap]) | ||
@@ -496,40 +740,15 @@ | ||
## Tests | ||
```js | ||
var node = new SourceNode(1, 2, "a.js", [ | ||
new SourceNode(3, 4, "b.js", "uno"), | ||
"dos", | ||
[ | ||
"tres", | ||
new SourceNode(5, 6, "c.js", "quatro") | ||
] | ||
]); | ||
[![Build Status](https://travis-ci.org/mozilla/source-map.png?branch=master)](https://travis-ci.org/mozilla/source-map) | ||
Install NodeJS version 0.8.0 or greater, then run `node test/run-tests.js`. | ||
To add new tests, create a new file named `test/test-<your new test name>.js` | ||
and export your test functions with names that start with "test", for example | ||
```js | ||
exports["test doing the foo bar"] = function (assert, util) { | ||
... | ||
}; | ||
node.toStringWithSourceMap({ file: "my-output-file.js" }) | ||
// { code: 'unodostresquatro', | ||
// map: [object SourceMapGenerator] } | ||
``` | ||
The new test will be located automatically when you run the suite. | ||
The `util` argument is the test utility module located at `test/source-map/util`. | ||
The `assert` argument is a cut down version of node's assert module. You have | ||
access to the following assertion functions: | ||
* `doesNotThrow` | ||
* `equal` | ||
* `ok` | ||
* `strictEqual` | ||
* `throws` | ||
(The reason for the restricted set of test functions is because we need the | ||
tests to run inside Firefox's test suite as well and so the assert module is | ||
shimmed in that environment. See `build/assert-shim.js`.) | ||
[format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit | ||
[feature]: https://wiki.mozilla.org/DevTools/Features/SourceMap | ||
[Dryice]: https://github.com/mozilla/dryice |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
730
124885
2
13
2603
- Removedamdefine@>=0.0.4
- Removedamdefine@1.0.1(transitive)