source: trip-planner-front/node_modules/source-map-js/README.md@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 25.3 KB
Line 
1# Source Map JS
2
3<!-- [![Build Status](https://travis-ci.org/mozilla/source-map.png?branch=master)](https://travis-ci.org/mozilla/source-map) -->
4
5[![NPM](https://nodei.co/npm/source-map-js.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/source-map-js)
6
7## Difference between original [source-map](https://github.com/mozilla/source-map)
8
9> TL,DR: it's fork of original source-map@0.6, but with perfomance optimizations.
10
11This journey starts from [source-map@0.7.0](https://github.com/mozilla/source-map/blob/master/CHANGELOG.md#070). Some part of it was rewritten to Rust and WASM and API became async.
12
13It's still a major block for many libraries like PostCSS or Webpack for example because they need to migrate the whole API to the async way. This is the reason why 0.6.1 has 2x more downloads than 0.7.3 while it's faster several times.
14
15![Downloads count](media/downloads.png)
16
17More important that WASM version has some optimizations in JS code too. This is why [community asked to create branch for 0.6 version](https://github.com/mozilla/source-map/issues/324) and port these optimizations but, sadly, the answer was «no». A bit later I discovered [the issue](https://github.com/mozilla/source-map/issues/370) created by [Ben Rothman (@benthemonkey)](https://github.com/benthemonkey) with no response at all.
18
19[Roman Dvornov (@lahmatiy)](https://github.com/lahmatiy) wrote a [serveral posts](https://t.me/gorshochekvarit/76) (russian, only, sorry) about source-map library in his own Telegram channel. He mentioned the article [«Maybe you don't need Rust and WASM to speed up your JS»](https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to-speed-up-your-js.html) written by [Vyacheslav Egorov (@mraleph)](https://github.com/mraleph). This article contains optimizations and hacks that lead to almost the same performance compare to WASM implementation.
20
21I decided to fork the original source-map and port these optimizations from the article and several others PR from the original source-map.
22
23---------
24
25This is a library to generate and consume the source map format
26[described here][format].
27
28[format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
29
30## Use with Node
31
32 $ npm install source-map-js
33
34<!-- ## Use on the Web
35
36 <script src="https://raw.githubusercontent.com/mozilla/source-map/master/dist/source-map.min.js" defer></script> -->
37
38--------------------------------------------------------------------------------
39
40<!-- `npm run toc` to regenerate the Table of Contents -->
41
42<!-- START doctoc generated TOC please keep comment here to allow auto update -->
43<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
44## Table of Contents
45
46- [Examples](#examples)
47 - [Consuming a source map](#consuming-a-source-map)
48 - [Generating a source map](#generating-a-source-map)
49 - [With SourceNode (high level API)](#with-sourcenode-high-level-api)
50 - [With SourceMapGenerator (low level API)](#with-sourcemapgenerator-low-level-api)
51- [API](#api)
52 - [SourceMapConsumer](#sourcemapconsumer)
53 - [new SourceMapConsumer(rawSourceMap)](#new-sourcemapconsumerrawsourcemap)
54 - [SourceMapConsumer.prototype.computeColumnSpans()](#sourcemapconsumerprototypecomputecolumnspans)
55 - [SourceMapConsumer.prototype.originalPositionFor(generatedPosition)](#sourcemapconsumerprototypeoriginalpositionforgeneratedposition)
56 - [SourceMapConsumer.prototype.generatedPositionFor(originalPosition)](#sourcemapconsumerprototypegeneratedpositionfororiginalposition)
57 - [SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)](#sourcemapconsumerprototypeallgeneratedpositionsfororiginalposition)
58 - [SourceMapConsumer.prototype.hasContentsOfAllSources()](#sourcemapconsumerprototypehascontentsofallsources)
59 - [SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])](#sourcemapconsumerprototypesourcecontentforsource-returnnullonmissing)
60 - [SourceMapConsumer.prototype.eachMapping(callback, context, order)](#sourcemapconsumerprototypeeachmappingcallback-context-order)
61 - [SourceMapGenerator](#sourcemapgenerator)
62 - [new SourceMapGenerator([startOfSourceMap])](#new-sourcemapgeneratorstartofsourcemap)
63 - [SourceMapGenerator.fromSourceMap(sourceMapConsumer)](#sourcemapgeneratorfromsourcemapsourcemapconsumer)
64 - [SourceMapGenerator.prototype.addMapping(mapping)](#sourcemapgeneratorprototypeaddmappingmapping)
65 - [SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)](#sourcemapgeneratorprototypesetsourcecontentsourcefile-sourcecontent)
66 - [SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])](#sourcemapgeneratorprototypeapplysourcemapsourcemapconsumer-sourcefile-sourcemappath)
67 - [SourceMapGenerator.prototype.toString()](#sourcemapgeneratorprototypetostring)
68 - [SourceNode](#sourcenode)
69 - [new SourceNode([line, column, source[, chunk[, name]]])](#new-sourcenodeline-column-source-chunk-name)
70 - [SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath])](#sourcenodefromstringwithsourcemapcode-sourcemapconsumer-relativepath)
71 - [SourceNode.prototype.add(chunk)](#sourcenodeprototypeaddchunk)
72 - [SourceNode.prototype.prepend(chunk)](#sourcenodeprototypeprependchunk)
73 - [SourceNode.prototype.setSourceContent(sourceFile, sourceContent)](#sourcenodeprototypesetsourcecontentsourcefile-sourcecontent)
74 - [SourceNode.prototype.walk(fn)](#sourcenodeprototypewalkfn)
75 - [SourceNode.prototype.walkSourceContents(fn)](#sourcenodeprototypewalksourcecontentsfn)
76 - [SourceNode.prototype.join(sep)](#sourcenodeprototypejoinsep)
77 - [SourceNode.prototype.replaceRight(pattern, replacement)](#sourcenodeprototypereplacerightpattern-replacement)
78 - [SourceNode.prototype.toString()](#sourcenodeprototypetostring)
79 - [SourceNode.prototype.toStringWithSourceMap([startOfSourceMap])](#sourcenodeprototypetostringwithsourcemapstartofsourcemap)
80
81<!-- END doctoc generated TOC please keep comment here to allow auto update -->
82
83## Examples
84
85### Consuming a source map
86
87```js
88var rawSourceMap = {
89 version: 3,
90 file: 'min.js',
91 names: ['bar', 'baz', 'n'],
92 sources: ['one.js', 'two.js'],
93 sourceRoot: 'http://example.com/www/js/',
94 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
95};
96
97var smc = new SourceMapConsumer(rawSourceMap);
98
99console.log(smc.sources);
100// [ 'http://example.com/www/js/one.js',
101// 'http://example.com/www/js/two.js' ]
102
103console.log(smc.originalPositionFor({
104 line: 2,
105 column: 28
106}));
107// { source: 'http://example.com/www/js/two.js',
108// line: 2,
109// column: 10,
110// name: 'n' }
111
112console.log(smc.generatedPositionFor({
113 source: 'http://example.com/www/js/two.js',
114 line: 2,
115 column: 10
116}));
117// { line: 2, column: 28 }
118
119smc.eachMapping(function (m) {
120 // ...
121});
122```
123
124### Generating a source map
125
126In depth guide:
127[**Compiling to JavaScript, and Debugging with Source Maps**](https://hacks.mozilla.org/2013/05/compiling-to-javascript-and-debugging-with-source-maps/)
128
129#### With SourceNode (high level API)
130
131```js
132function compile(ast) {
133 switch (ast.type) {
134 case 'BinaryExpression':
135 return new SourceNode(
136 ast.location.line,
137 ast.location.column,
138 ast.location.source,
139 [compile(ast.left), " + ", compile(ast.right)]
140 );
141 case 'Literal':
142 return new SourceNode(
143 ast.location.line,
144 ast.location.column,
145 ast.location.source,
146 String(ast.value)
147 );
148 // ...
149 default:
150 throw new Error("Bad AST");
151 }
152}
153
154var ast = parse("40 + 2", "add.js");
155console.log(compile(ast).toStringWithSourceMap({
156 file: 'add.js'
157}));
158// { code: '40 + 2',
159// map: [object SourceMapGenerator] }
160```
161
162#### With SourceMapGenerator (low level API)
163
164```js
165var map = new SourceMapGenerator({
166 file: "source-mapped.js"
167});
168
169map.addMapping({
170 generated: {
171 line: 10,
172 column: 35
173 },
174 source: "foo.js",
175 original: {
176 line: 33,
177 column: 2
178 },
179 name: "christopher"
180});
181
182console.log(map.toString());
183// '{"version":3,"file":"source-mapped.js","sources":["foo.js"],"names":["christopher"],"mappings":";;;;;;;;;mCAgCEA"}'
184```
185
186## API
187
188Get a reference to the module:
189
190```js
191// Node.js
192var sourceMap = require('source-map');
193
194// Browser builds
195var sourceMap = window.sourceMap;
196
197// Inside Firefox
198const sourceMap = require("devtools/toolkit/sourcemap/source-map.js");
199```
200
201### SourceMapConsumer
202
203A SourceMapConsumer instance represents a parsed source map which we can query
204for information about the original file positions by giving it a file position
205in the generated source.
206
207#### new SourceMapConsumer(rawSourceMap)
208
209The only parameter is the raw source map (either as a string which can be
210`JSON.parse`'d, or an object). According to the spec, source maps have the
211following attributes:
212
213* `version`: Which version of the source map spec this map is following.
214
215* `sources`: An array of URLs to the original source files.
216
217* `names`: An array of identifiers which can be referenced by individual
218 mappings.
219
220* `sourceRoot`: Optional. The URL root from which all sources are relative.
221
222* `sourcesContent`: Optional. An array of contents of the original source files.
223
224* `mappings`: A string of base64 VLQs which contain the actual mappings.
225
226* `file`: Optional. The generated filename this source map is associated with.
227
228```js
229var consumer = new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
230```
231
232#### SourceMapConsumer.prototype.computeColumnSpans()
233
234Compute the last column for each generated mapping. The last column is
235inclusive.
236
237```js
238// Before:
239consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" })
240// [ { line: 2,
241// column: 1 },
242// { line: 2,
243// column: 10 },
244// { line: 2,
245// column: 20 } ]
246
247consumer.computeColumnSpans();
248
249// After:
250consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" })
251// [ { line: 2,
252// column: 1,
253// lastColumn: 9 },
254// { line: 2,
255// column: 10,
256// lastColumn: 19 },
257// { line: 2,
258// column: 20,
259// lastColumn: Infinity } ]
260
261```
262
263#### SourceMapConsumer.prototype.originalPositionFor(generatedPosition)
264
265Returns the original source, line, and column information for the generated
266source's line and column positions provided. The only argument is an object with
267the following properties:
268
269* `line`: The line number in the generated source. Line numbers in
270 this library are 1-based (note that the underlying source map
271 specification uses 0-based line numbers -- this library handles the
272 translation).
273
274* `column`: The column number in the generated source. Column numbers
275 in this library are 0-based.
276
277* `bias`: Either `SourceMapConsumer.GREATEST_LOWER_BOUND` or
278 `SourceMapConsumer.LEAST_UPPER_BOUND`. Specifies whether to return the closest
279 element that is smaller than or greater than the one we are searching for,
280 respectively, if the exact element cannot be found. Defaults to
281 `SourceMapConsumer.GREATEST_LOWER_BOUND`.
282
283and an object is returned with the following properties:
284
285* `source`: The original source file, or null if this information is not
286 available.
287
288* `line`: The line number in the original source, or null if this information is
289 not available. The line number is 1-based.
290
291* `column`: The column number in the original source, or null if this
292 information is not available. The column number is 0-based.
293
294* `name`: The original identifier, or null if this information is not available.
295
296```js
297consumer.originalPositionFor({ line: 2, column: 10 })
298// { source: 'foo.coffee',
299// line: 2,
300// column: 2,
301// name: null }
302
303consumer.originalPositionFor({ line: 99999999999999999, column: 999999999999999 })
304// { source: null,
305// line: null,
306// column: null,
307// name: null }
308```
309
310#### SourceMapConsumer.prototype.generatedPositionFor(originalPosition)
311
312Returns the generated line and column information for the original source,
313line, and column positions provided. The only argument is an object with
314the following properties:
315
316* `source`: The filename of the original source.
317
318* `line`: The line number in the original source. The line number is
319 1-based.
320
321* `column`: The column number in the original source. The column
322 number is 0-based.
323
324and an object is returned with the following properties:
325
326* `line`: The line number in the generated source, or null. The line
327 number is 1-based.
328
329* `column`: The column number in the generated source, or null. The
330 column number is 0-based.
331
332```js
333consumer.generatedPositionFor({ source: "example.js", line: 2, column: 10 })
334// { line: 1,
335// column: 56 }
336```
337
338#### SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)
339
340Returns all generated line and column information for the original source, line,
341and column provided. If no column is provided, returns all mappings
342corresponding to a either the line we are searching for or the next closest line
343that has any mappings. Otherwise, returns all mappings corresponding to the
344given line and either the column we are searching for or the next closest column
345that has any offsets.
346
347The only argument is an object with the following properties:
348
349* `source`: The filename of the original source.
350
351* `line`: The line number in the original source. The line number is
352 1-based.
353
354* `column`: Optional. The column number in the original source. The
355 column number is 0-based.
356
357and an array of objects is returned, each with the following properties:
358
359* `line`: The line number in the generated source, or null. The line
360 number is 1-based.
361
362* `column`: The column number in the generated source, or null. The
363 column number is 0-based.
364
365```js
366consumer.allGeneratedpositionsfor({ line: 2, source: "foo.coffee" })
367// [ { line: 2,
368// column: 1 },
369// { line: 2,
370// column: 10 },
371// { line: 2,
372// column: 20 } ]
373```
374
375#### SourceMapConsumer.prototype.hasContentsOfAllSources()
376
377Return true if we have the embedded source content for every source listed in
378the source map, false otherwise.
379
380In other words, if this method returns `true`, then
381`consumer.sourceContentFor(s)` will succeed for every source `s` in
382`consumer.sources`.
383
384```js
385// ...
386if (consumer.hasContentsOfAllSources()) {
387 consumerReadyCallback(consumer);
388} else {
389 fetchSources(consumer, consumerReadyCallback);
390}
391// ...
392```
393
394#### SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])
395
396Returns the original source content for the source provided. The only
397argument is the URL of the original source file.
398
399If the source content for the given source is not found, then an error is
400thrown. Optionally, pass `true` as the second param to have `null` returned
401instead.
402
403```js
404consumer.sources
405// [ "my-cool-lib.clj" ]
406
407consumer.sourceContentFor("my-cool-lib.clj")
408// "..."
409
410consumer.sourceContentFor("this is not in the source map");
411// Error: "this is not in the source map" is not in the source map
412
413consumer.sourceContentFor("this is not in the source map", true);
414// null
415```
416
417#### SourceMapConsumer.prototype.eachMapping(callback, context, order)
418
419Iterate over each mapping between an original source/line/column and a
420generated line/column in this source map.
421
422* `callback`: The function that is called with each mapping. Mappings have the
423 form `{ source, generatedLine, generatedColumn, originalLine, originalColumn,
424 name }`
425
426* `context`: Optional. If specified, this object will be the value of `this`
427 every time that `callback` is called.
428
429* `order`: Either `SourceMapConsumer.GENERATED_ORDER` or
430 `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to iterate over
431 the mappings sorted by the generated file's line/column order or the
432 original's source/line/column order, respectively. Defaults to
433 `SourceMapConsumer.GENERATED_ORDER`.
434
435```js
436consumer.eachMapping(function (m) { console.log(m); })
437// ...
438// { source: 'illmatic.js',
439// generatedLine: 1,
440// generatedColumn: 0,
441// originalLine: 1,
442// originalColumn: 0,
443// name: null }
444// { source: 'illmatic.js',
445// generatedLine: 2,
446// generatedColumn: 0,
447// originalLine: 2,
448// originalColumn: 0,
449// name: null }
450// ...
451```
452### SourceMapGenerator
453
454An instance of the SourceMapGenerator represents a source map which is being
455built incrementally.
456
457#### new SourceMapGenerator([startOfSourceMap])
458
459You may pass an object with the following properties:
460
461* `file`: The filename of the generated source that this source map is
462 associated with.
463
464* `sourceRoot`: A root for all relative URLs in this source map.
465
466* `skipValidation`: Optional. When `true`, disables validation of mappings as
467 they are added. This can improve performance but should be used with
468 discretion, as a last resort. Even then, one should avoid using this flag when
469 running tests, if possible.
470
471```js
472var generator = new sourceMap.SourceMapGenerator({
473 file: "my-generated-javascript-file.js",
474 sourceRoot: "http://example.com/app/js/"
475});
476```
477
478#### SourceMapGenerator.fromSourceMap(sourceMapConsumer)
479
480Creates a new `SourceMapGenerator` from an existing `SourceMapConsumer` instance.
481
482* `sourceMapConsumer` The SourceMap.
483
484```js
485var generator = sourceMap.SourceMapGenerator.fromSourceMap(consumer);
486```
487
488#### SourceMapGenerator.prototype.addMapping(mapping)
489
490Add a single mapping from original source line and column to the generated
491source's line and column for this source map being created. The mapping object
492should have the following properties:
493
494* `generated`: An object with the generated line and column positions.
495
496* `original`: An object with the original line and column positions.
497
498* `source`: The original source file (relative to the sourceRoot).
499
500* `name`: An optional original token name for this mapping.
501
502```js
503generator.addMapping({
504 source: "module-one.scm",
505 original: { line: 128, column: 0 },
506 generated: { line: 3, column: 456 }
507})
508```
509
510#### SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)
511
512Set the source content for an original source file.
513
514* `sourceFile` the URL of the original source file.
515
516* `sourceContent` the content of the source file.
517
518```js
519generator.setSourceContent("module-one.scm",
520 fs.readFileSync("path/to/module-one.scm"))
521```
522
523#### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])
524
525Applies a SourceMap for a source file to the SourceMap.
526Each mapping to the supplied source file is rewritten using the
527supplied SourceMap. Note: The resolution for the resulting mappings
528is the minimum of this map and the supplied map.
529
530* `sourceMapConsumer`: The SourceMap to be applied.
531
532* `sourceFile`: Optional. The filename of the source file.
533 If omitted, sourceMapConsumer.file will be used, if it exists.
534 Otherwise an error will be thrown.
535
536* `sourceMapPath`: Optional. The dirname of the path to the SourceMap
537 to be applied. If relative, it is relative to the SourceMap.
538
539 This parameter is needed when the two SourceMaps aren't in the same
540 directory, and the SourceMap to be applied contains relative source
541 paths. If so, those relative source paths need to be rewritten
542 relative to the SourceMap.
543
544 If omitted, it is assumed that both SourceMaps are in the same directory,
545 thus not needing any rewriting. (Supplying `'.'` has the same effect.)
546
547#### SourceMapGenerator.prototype.toString()
548
549Renders the source map being generated to a string.
550
551```js
552generator.toString()
553// '{"version":3,"sources":["module-one.scm"],"names":[],"mappings":"...snip...","file":"my-generated-javascript-file.js","sourceRoot":"http://example.com/app/js/"}'
554```
555
556### SourceNode
557
558SourceNodes provide a way to abstract over interpolating and/or concatenating
559snippets of generated JavaScript source code, while maintaining the line and
560column information associated between those snippets and the original source
561code. This is useful as the final intermediate representation a compiler might
562use before outputting the generated JS and source map.
563
564#### new SourceNode([line, column, source[, chunk[, name]]])
565
566* `line`: The original line number associated with this source node, or null if
567 it isn't associated with an original line. The line number is 1-based.
568
569* `column`: The original column number associated with this source node, or null
570 if it isn't associated with an original column. The column number
571 is 0-based.
572
573* `source`: The original source's filename; null if no filename is provided.
574
575* `chunk`: Optional. Is immediately passed to `SourceNode.prototype.add`, see
576 below.
577
578* `name`: Optional. The original identifier.
579
580```js
581var node = new SourceNode(1, 2, "a.cpp", [
582 new SourceNode(3, 4, "b.cpp", "extern int status;\n"),
583 new SourceNode(5, 6, "c.cpp", "std::string* make_string(size_t n);\n"),
584 new SourceNode(7, 8, "d.cpp", "int main(int argc, char** argv) {}\n"),
585]);
586```
587
588#### SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath])
589
590Creates a SourceNode from generated code and a SourceMapConsumer.
591
592* `code`: The generated code
593
594* `sourceMapConsumer` The SourceMap for the generated code
595
596* `relativePath` The optional path that relative sources in `sourceMapConsumer`
597 should be relative to.
598
599```js
600var consumer = new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
601var node = SourceNode.fromStringWithSourceMap(fs.readFileSync("path/to/my-file.js"),
602 consumer);
603```
604
605#### SourceNode.prototype.add(chunk)
606
607Add a chunk of generated JS to this source node.
608
609* `chunk`: A string snippet of generated JS code, another instance of
610 `SourceNode`, or an array where each member is one of those things.
611
612```js
613node.add(" + ");
614node.add(otherNode);
615node.add([leftHandOperandNode, " + ", rightHandOperandNode]);
616```
617
618#### SourceNode.prototype.prepend(chunk)
619
620Prepend a chunk of generated JS to this source node.
621
622* `chunk`: A string snippet of generated JS code, another instance of
623 `SourceNode`, or an array where each member is one of those things.
624
625```js
626node.prepend("/** Build Id: f783haef86324gf **/\n\n");
627```
628
629#### SourceNode.prototype.setSourceContent(sourceFile, sourceContent)
630
631Set the source content for a source file. This will be added to the
632`SourceMap` in the `sourcesContent` field.
633
634* `sourceFile`: The filename of the source file
635
636* `sourceContent`: The content of the source file
637
638```js
639node.setSourceContent("module-one.scm",
640 fs.readFileSync("path/to/module-one.scm"))
641```
642
643#### SourceNode.prototype.walk(fn)
644
645Walk over the tree of JS snippets in this node and its children. The walking
646function is called once for each snippet of JS and is passed that snippet and
647the its original associated source's line/column location.
648
649* `fn`: The traversal function.
650
651```js
652var node = new SourceNode(1, 2, "a.js", [
653 new SourceNode(3, 4, "b.js", "uno"),
654 "dos",
655 [
656 "tres",
657 new SourceNode(5, 6, "c.js", "quatro")
658 ]
659]);
660
661node.walk(function (code, loc) { console.log("WALK:", code, loc); })
662// WALK: uno { source: 'b.js', line: 3, column: 4, name: null }
663// WALK: dos { source: 'a.js', line: 1, column: 2, name: null }
664// WALK: tres { source: 'a.js', line: 1, column: 2, name: null }
665// WALK: quatro { source: 'c.js', line: 5, column: 6, name: null }
666```
667
668#### SourceNode.prototype.walkSourceContents(fn)
669
670Walk over the tree of SourceNodes. The walking function is called for each
671source file content and is passed the filename and source content.
672
673* `fn`: The traversal function.
674
675```js
676var a = new SourceNode(1, 2, "a.js", "generated from a");
677a.setSourceContent("a.js", "original a");
678var b = new SourceNode(1, 2, "b.js", "generated from b");
679b.setSourceContent("b.js", "original b");
680var c = new SourceNode(1, 2, "c.js", "generated from c");
681c.setSourceContent("c.js", "original c");
682
683var node = new SourceNode(null, null, null, [a, b, c]);
684node.walkSourceContents(function (source, contents) { console.log("WALK:", source, ":", contents); })
685// WALK: a.js : original a
686// WALK: b.js : original b
687// WALK: c.js : original c
688```
689
690#### SourceNode.prototype.join(sep)
691
692Like `Array.prototype.join` except for SourceNodes. Inserts the separator
693between each of this source node's children.
694
695* `sep`: The separator.
696
697```js
698var lhs = new SourceNode(1, 2, "a.rs", "my_copy");
699var operand = new SourceNode(3, 4, "a.rs", "=");
700var rhs = new SourceNode(5, 6, "a.rs", "orig.clone()");
701
702var node = new SourceNode(null, null, null, [ lhs, operand, rhs ]);
703var joinedNode = node.join(" ");
704```
705
706#### SourceNode.prototype.replaceRight(pattern, replacement)
707
708Call `String.prototype.replace` on the very right-most source snippet. Useful
709for trimming white space from the end of a source node, etc.
710
711* `pattern`: The pattern to replace.
712
713* `replacement`: The thing to replace the pattern with.
714
715```js
716// Trim trailing white space.
717node.replaceRight(/\s*$/, "");
718```
719
720#### SourceNode.prototype.toString()
721
722Return the string representation of this source node. Walks over the tree and
723concatenates all the various snippets together to one string.
724
725```js
726var node = new SourceNode(1, 2, "a.js", [
727 new SourceNode(3, 4, "b.js", "uno"),
728 "dos",
729 [
730 "tres",
731 new SourceNode(5, 6, "c.js", "quatro")
732 ]
733]);
734
735node.toString()
736// 'unodostresquatro'
737```
738
739#### SourceNode.prototype.toStringWithSourceMap([startOfSourceMap])
740
741Returns the string representation of this tree of source nodes, plus a
742SourceMapGenerator which contains all the mappings between the generated and
743original sources.
744
745The arguments are the same as those to `new SourceMapGenerator`.
746
747```js
748var node = new SourceNode(1, 2, "a.js", [
749 new SourceNode(3, 4, "b.js", "uno"),
750 "dos",
751 [
752 "tres",
753 new SourceNode(5, 6, "c.js", "quatro")
754 ]
755]);
756
757node.toStringWithSourceMap({ file: "my-output-file.js" })
758// { code: 'unodostresquatro',
759// map: [object SourceMapGenerator] }
760```
Note: See TracBrowser for help on using the repository browser.