@@ -34,6 +34,7 @@ This is a library to generate and consume the source map format
3434 - [ SourceMapConsumer] ( #sourcemapconsumer )
3535 - [ SourceMapConsumer.initialize(options)] ( #sourcemapconsumerinitializeoptions )
3636 - [ new SourceMapConsumer(rawSourceMap)] ( #new-sourcemapconsumerrawsourcemap )
37+ - [ SourceMapConsumer.with] ( #sourcemapconsumerwith )
3738 - [ SourceMapConsumer.prototype.destroy()] ( #sourcemapconsumerprototypedestroy )
3839 - [ SourceMapConsumer.prototype.computeColumnSpans()] ( #sourcemapconsumerprototypecomputecolumnspans )
3940 - [ SourceMapConsumer.prototype.originalPositionFor(generatedPosition)] ( #sourcemapconsumerprototypeoriginalpositionforgeneratedposition )
@@ -78,34 +79,34 @@ const rawSourceMap = {
7879 mappings: ' CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
7980};
8081
81- const smc = await new SourceMapConsumer (rawSourceMap);
82-
83- console .log (smc .sources );
84- // [ 'http://example.com/www/js/one.js',
85- // 'http://example.com/www/js/two.js' ]
86-
87- console .log (smc .originalPositionFor ({
88- line: 2 ,
89- column: 28
90- }));
91- // { source: 'http://example.com/www/js/two.js',
92- // line: 2,
93- // column: 10,
94- // name: 'n' }
95-
96- console .log (smc .generatedPositionFor ({
97- source: ' http://example.com/www/js/two.js' ,
98- line: 2 ,
99- column: 10
100- }));
101- // { line: 2, column: 28 }
102-
103- smc .eachMapping (function (m ) {
104- // ...
82+ const whatever = await SourceMapConsumer .with (rawSourceMap, null , consumer => {
83+
84+ console .log (consumer .sources );
85+ // [ 'http://example.com/www/js/one.js',
86+ // 'http://example.com/www/js/two.js' ]
87+
88+ console .log (consumer .originalPositionFor ({
89+ line: 2 ,
90+ column: 28
91+ }));
92+ // { source: 'http://example.com/www/js/two.js',
93+ // line: 2,
94+ // column: 10,
95+ // name: 'n' }
96+
97+ console .log (consumer .generatedPositionFor ({
98+ source: ' http://example.com/www/js/two.js' ,
99+ line: 2 ,
100+ column: 10
101+ }));
102+ // { line: 2, column: 28 }
103+
104+ consumer .eachMapping (function (m ) {
105+ // ...
106+ });
107+
108+ return computeWhatever ();
105109});
106-
107- // Free the SourceMapConsumer's manually managed wasm data.
108- smc .destroy ();
109110```
110111
111112### Generating a source map
@@ -240,6 +241,40 @@ doStuffWith(consumer);
240241consumer .destroy ();
241242```
242243
244+ Alternatively, you can use ` SourceMapConsumer.with ` to avoid needing to remember
245+ to call ` destroy ` .
246+
247+ #### SourceMapConsumer.with
248+
249+ Construct a new ` SourceMapConsumer ` from ` rawSourceMap ` and ` sourceMapUrl `
250+ (see the ` SourceMapConsumer ` constructor for details. Then, invoke the `async
251+ function f(SourceMapConsumer) -> T` with the newly constructed consumer, wait
252+ for ` f ` to complete, call ` destroy ` on the consumer, and return ` f ` 's return
253+ value.
254+
255+ You must not use the consumer after ` f ` completes!
256+
257+ By using ` with ` , you do not have to remember to manually call ` destroy ` on
258+ the consumer, since it will be called automatically once ` f ` completes.
259+
260+ ``` js
261+ const xSquared = await SourceMapConsumer .with (
262+ myRawSourceMap,
263+ null ,
264+ async function (consumer ) {
265+ // Use `consumer` inside here and don't worry about remembering
266+ // to call `destroy`.
267+
268+ const x = await whatever (consumer);
269+ return x * x;
270+ }
271+ );
272+
273+ // You may not use that `consumer` anymore out here; it has
274+ // been destroyed. But you can use `xSquared`.
275+ console .log (xSquared);
276+ ```
277+
243278#### SourceMapConsumer.prototype.destroy()
244279
245280Free this source map consumer's associated wasm data that is manually-managed.
@@ -248,6 +283,9 @@ Free this source map consumer's associated wasm data that is manually-managed.
248283consumer .destroy ();
249284```
250285
286+ Alternatively, you can use ` SourceMapConsumer.with ` to avoid needing to remember
287+ to call ` destroy ` .
288+
251289#### SourceMapConsumer.prototype.computeColumnSpans()
252290
253291Compute the last column for each generated mapping. The last column is
0 commit comments