source: frontend/node_modules/source-map/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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