PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/babel-core/node_modules/source-map-support/node_modules/source-map/README.md

https://gitlab.com/jeaster12/fabby
Markdown | 434 lines | 281 code | 153 blank | 0 comment | 0 complexity | e44c249b9245027f90c3e16d29d21e36 MD5 | raw file
  1. # Source Map
  2. This is a library to generate and consume the source map format
  3. [described here][format].
  4. This library is written in the Asynchronous Module Definition format, and works
  5. in the following environments:
  6. * Modern Browsers supporting ECMAScript 5 (either after the build, or with an
  7. AMD loader such as RequireJS)
  8. * Inside Firefox (as a JSM file, after the build)
  9. * With NodeJS versions 0.8.X and higher
  10. ## Node
  11. $ npm install source-map
  12. ## Building from Source (for everywhere else)
  13. Install Node and then run
  14. $ git clone https://fitzgen@github.com/mozilla/source-map.git
  15. $ cd source-map
  16. $ npm link .
  17. Next, run
  18. $ node Makefile.dryice.js
  19. This should spew a bunch of stuff to stdout, and create the following files:
  20. * `dist/source-map.js` - The unminified browser version.
  21. * `dist/source-map.min.js` - The minified browser version.
  22. * `dist/SourceMap.jsm` - The JavaScript Module for inclusion in Firefox source.
  23. ## Examples
  24. ### Consuming a source map
  25. var rawSourceMap = {
  26. version: 3,
  27. file: 'min.js',
  28. names: ['bar', 'baz', 'n'],
  29. sources: ['one.js', 'two.js'],
  30. sourceRoot: 'http://example.com/www/js/',
  31. mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
  32. };
  33. var smc = new SourceMapConsumer(rawSourceMap);
  34. console.log(smc.sources);
  35. // [ 'http://example.com/www/js/one.js',
  36. // 'http://example.com/www/js/two.js' ]
  37. console.log(smc.originalPositionFor({
  38. line: 2,
  39. column: 28
  40. }));
  41. // { source: 'http://example.com/www/js/two.js',
  42. // line: 2,
  43. // column: 10,
  44. // name: 'n' }
  45. console.log(smc.generatedPositionFor({
  46. source: 'http://example.com/www/js/two.js',
  47. line: 2,
  48. column: 10
  49. }));
  50. // { line: 2, column: 28 }
  51. smc.eachMapping(function (m) {
  52. // ...
  53. });
  54. ### Generating a source map
  55. In depth guide:
  56. [**Compiling to JavaScript, and Debugging with Source Maps**](https://hacks.mozilla.org/2013/05/compiling-to-javascript-and-debugging-with-source-maps/)
  57. #### With SourceNode (high level API)
  58. function compile(ast) {
  59. switch (ast.type) {
  60. case 'BinaryExpression':
  61. return new SourceNode(
  62. ast.location.line,
  63. ast.location.column,
  64. ast.location.source,
  65. [compile(ast.left), " + ", compile(ast.right)]
  66. );
  67. case 'Literal':
  68. return new SourceNode(
  69. ast.location.line,
  70. ast.location.column,
  71. ast.location.source,
  72. String(ast.value)
  73. );
  74. // ...
  75. default:
  76. throw new Error("Bad AST");
  77. }
  78. }
  79. var ast = parse("40 + 2", "add.js");
  80. console.log(compile(ast).toStringWithSourceMap({
  81. file: 'add.js'
  82. }));
  83. // { code: '40 + 2',
  84. // map: [object SourceMapGenerator] }
  85. #### With SourceMapGenerator (low level API)
  86. var map = new SourceMapGenerator({
  87. file: "source-mapped.js"
  88. });
  89. map.addMapping({
  90. generated: {
  91. line: 10,
  92. column: 35
  93. },
  94. source: "foo.js",
  95. original: {
  96. line: 33,
  97. column: 2
  98. },
  99. name: "christopher"
  100. });
  101. console.log(map.toString());
  102. // '{"version":3,"file":"source-mapped.js","sources":["foo.js"],"names":["christopher"],"mappings":";;;;;;;;;mCAgCEA"}'
  103. ## API
  104. Get a reference to the module:
  105. // NodeJS
  106. var sourceMap = require('source-map');
  107. // Browser builds
  108. var sourceMap = window.sourceMap;
  109. // Inside Firefox
  110. let sourceMap = {};
  111. Components.utils.import('resource:///modules/devtools/SourceMap.jsm', sourceMap);
  112. ### SourceMapConsumer
  113. A SourceMapConsumer instance represents a parsed source map which we can query
  114. for information about the original file positions by giving it a file position
  115. in the generated source.
  116. #### new SourceMapConsumer(rawSourceMap)
  117. The only parameter is the raw source map (either as a string which can be
  118. `JSON.parse`'d, or an object). According to the spec, source maps have the
  119. following attributes:
  120. * `version`: Which version of the source map spec this map is following.
  121. * `sources`: An array of URLs to the original source files.
  122. * `names`: An array of identifiers which can be referrenced by individual
  123. mappings.
  124. * `sourceRoot`: Optional. The URL root from which all sources are relative.
  125. * `sourcesContent`: Optional. An array of contents of the original source files.
  126. * `mappings`: A string of base64 VLQs which contain the actual mappings.
  127. * `file`: The generated filename this source map is associated with.
  128. #### SourceMapConsumer.prototype.originalPositionFor(generatedPosition)
  129. Returns the original source, line, and column information for the generated
  130. source's line and column positions provided. The only argument is an object with
  131. the following properties:
  132. * `line`: The line number in the generated source.
  133. * `column`: The column number in the generated source.
  134. and an object is returned with the following properties:
  135. * `source`: The original source file, or null if this information is not
  136. available.
  137. * `line`: The line number in the original source, or null if this information is
  138. not available.
  139. * `column`: The column number in the original source, or null or null if this
  140. information is not available.
  141. * `name`: The original identifier, or null if this information is not available.
  142. #### SourceMapConsumer.prototype.generatedPositionFor(originalPosition)
  143. Returns the generated line and column information for the original source,
  144. line, and column positions provided. The only argument is an object with
  145. the following properties:
  146. * `source`: The filename of the original source.
  147. * `line`: The line number in the original source.
  148. * `column`: The column number in the original source.
  149. and an object is returned with the following properties:
  150. * `line`: The line number in the generated source, or null.
  151. * `column`: The column number in the generated source, or null.
  152. #### SourceMapConsumer.prototype.sourceContentFor(source)
  153. Returns the original source content for the source provided. The only
  154. argument is the URL of the original source file.
  155. #### SourceMapConsumer.prototype.eachMapping(callback, context, order)
  156. Iterate over each mapping between an original source/line/column and a
  157. generated line/column in this source map.
  158. * `callback`: The function that is called with each mapping. Mappings have the
  159. form `{ source, generatedLine, generatedColumn, originalLine, originalColumn,
  160. name }`
  161. * `context`: Optional. If specified, this object will be the value of `this`
  162. every time that `callback` is called.
  163. * `order`: Either `SourceMapConsumer.GENERATED_ORDER` or
  164. `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to iterate over
  165. the mappings sorted by the generated file's line/column order or the
  166. original's source/line/column order, respectively. Defaults to
  167. `SourceMapConsumer.GENERATED_ORDER`.
  168. ### SourceMapGenerator
  169. An instance of the SourceMapGenerator represents a source map which is being
  170. built incrementally.
  171. #### new SourceMapGenerator(startOfSourceMap)
  172. To create a new one, you must pass an object with the following properties:
  173. * `file`: The filename of the generated source that this source map is
  174. associated with.
  175. * `sourceRoot`: An optional root for all relative URLs in this source map.
  176. #### SourceMapGenerator.fromSourceMap(sourceMapConsumer)
  177. Creates a new SourceMapGenerator based on a SourceMapConsumer
  178. * `sourceMapConsumer` The SourceMap.
  179. #### SourceMapGenerator.prototype.addMapping(mapping)
  180. Add a single mapping from original source line and column to the generated
  181. source's line and column for this source map being created. The mapping object
  182. should have the following properties:
  183. * `generated`: An object with the generated line and column positions.
  184. * `original`: An object with the original line and column positions.
  185. * `source`: The original source file (relative to the sourceRoot).
  186. * `name`: An optional original token name for this mapping.
  187. #### SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)
  188. Set the source content for an original source file.
  189. * `sourceFile` the URL of the original source file.
  190. * `sourceContent` the content of the source file.
  191. #### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile])
  192. Applies a SourceMap for a source file to the SourceMap.
  193. Each mapping to the supplied source file is rewritten using the
  194. supplied SourceMap. Note: The resolution for the resulting mappings
  195. is the minimium of this map and the supplied map.
  196. * `sourceMapConsumer`: The SourceMap to be applied.
  197. * `sourceFile`: Optional. The filename of the source file.
  198. If omitted, sourceMapConsumer.file will be used.
  199. #### SourceMapGenerator.prototype.toString()
  200. Renders the source map being generated to a string.
  201. ### SourceNode
  202. SourceNodes provide a way to abstract over interpolating and/or concatenating
  203. snippets of generated JavaScript source code, while maintaining the line and
  204. column information associated between those snippets and the original source
  205. code. This is useful as the final intermediate representation a compiler might
  206. use before outputting the generated JS and source map.
  207. #### new SourceNode(line, column, source[, chunk[, name]])
  208. * `line`: The original line number associated with this source node, or null if
  209. it isn't associated with an original line.
  210. * `column`: The original column number associated with this source node, or null
  211. if it isn't associated with an original column.
  212. * `source`: The original source's filename.
  213. * `chunk`: Optional. Is immediately passed to `SourceNode.prototype.add`, see
  214. below.
  215. * `name`: Optional. The original identifier.
  216. #### SourceNode.fromStringWithSourceMap(code, sourceMapConsumer)
  217. Creates a SourceNode from generated code and a SourceMapConsumer.
  218. * `code`: The generated code
  219. * `sourceMapConsumer` The SourceMap for the generated code
  220. #### SourceNode.prototype.add(chunk)
  221. Add a chunk of generated JS to this source node.
  222. * `chunk`: A string snippet of generated JS code, another instance of
  223. `SourceNode`, or an array where each member is one of those things.
  224. #### SourceNode.prototype.prepend(chunk)
  225. Prepend a chunk of generated JS to this source node.
  226. * `chunk`: A string snippet of generated JS code, another instance of
  227. `SourceNode`, or an array where each member is one of those things.
  228. #### SourceNode.prototype.setSourceContent(sourceFile, sourceContent)
  229. Set the source content for a source file. This will be added to the
  230. `SourceMap` in the `sourcesContent` field.
  231. * `sourceFile`: The filename of the source file
  232. * `sourceContent`: The content of the source file
  233. #### SourceNode.prototype.walk(fn)
  234. Walk over the tree of JS snippets in this node and its children. The walking
  235. function is called once for each snippet of JS and is passed that snippet and
  236. the its original associated source's line/column location.
  237. * `fn`: The traversal function.
  238. #### SourceNode.prototype.walkSourceContents(fn)
  239. Walk over the tree of SourceNodes. The walking function is called for each
  240. source file content and is passed the filename and source content.
  241. * `fn`: The traversal function.
  242. #### SourceNode.prototype.join(sep)
  243. Like `Array.prototype.join` except for SourceNodes. Inserts the separator
  244. between each of this source node's children.
  245. * `sep`: The separator.
  246. #### SourceNode.prototype.replaceRight(pattern, replacement)
  247. Call `String.prototype.replace` on the very right-most source snippet. Useful
  248. for trimming whitespace from the end of a source node, etc.
  249. * `pattern`: The pattern to replace.
  250. * `replacement`: The thing to replace the pattern with.
  251. #### SourceNode.prototype.toString()
  252. Return the string representation of this source node. Walks over the tree and
  253. concatenates all the various snippets together to one string.
  254. ### SourceNode.prototype.toStringWithSourceMap(startOfSourceMap)
  255. Returns the string representation of this tree of source nodes, plus a
  256. SourceMapGenerator which contains all the mappings between the generated and
  257. original sources.
  258. The arguments are the same as those to `new SourceMapGenerator`.
  259. ## Tests
  260. [![Build Status](https://travis-ci.org/mozilla/source-map.png?branch=master)](https://travis-ci.org/mozilla/source-map)
  261. Install NodeJS version 0.8.0 or greater, then run `node test/run-tests.js`.
  262. To add new tests, create a new file named `test/test-<your new test name>.js`
  263. and export your test functions with names that start with "test", for example
  264. exports["test doing the foo bar"] = function (assert, util) {
  265. ...
  266. };
  267. The new test will be located automatically when you run the suite.
  268. The `util` argument is the test utility module located at `test/source-map/util`.
  269. The `assert` argument is a cut down version of node's assert module. You have
  270. access to the following assertion functions:
  271. * `doesNotThrow`
  272. * `equal`
  273. * `ok`
  274. * `strictEqual`
  275. * `throws`
  276. (The reason for the restricted set of test functions is because we need the
  277. tests to run inside Firefox's test suite as well and so the assert module is
  278. shimmed in that environment. See `build/assert-shim.js`.)
  279. [format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
  280. [feature]: https://wiki.mozilla.org/DevTools/Features/SourceMap
  281. [Dryice]: https://github.com/mozilla/dryice