Debian Codegraph @

node/source-map-js/README.md

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