Skip to content

Commit 8214089

Browse files
authored
Merge pull request #330 from ds300/fix/callback-stack
Allow nested consumer usage
2 parents c1e2ec8 + b87dfc8 commit 8214089

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

dist/source-map.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,7 @@ module.exports = function wasm() {
29192919
return cachedWasm;
29202920
}
29212921

2922-
let currentCallback = null;
2922+
const callbackStack = [];
29232923

29242924
cachedWasm = readWasm().then(buffer => {
29252925
return WebAssembly.instantiate(buffer, {
@@ -2960,7 +2960,7 @@ module.exports = function wasm() {
29602960
}
29612961
}
29622962

2963-
currentCallback(mapping);
2963+
callbackStack[callbackStack.length - 1](mapping);
29642964
},
29652965

29662966
start_all_generated_locations_for: function () { console.time("all_generated_locations_for"); },
@@ -2989,11 +2989,11 @@ module.exports = function wasm() {
29892989
return {
29902990
exports: wasm.instance.exports,
29912991
withMappingCallback: (mappingCallback, f) => {
2992-
currentCallback = mappingCallback;
2992+
callbackStack.push(mappingCallback)
29932993
try {
29942994
f();
29952995
} finally {
2996-
currentCallback = null;
2996+
callbackStack.pop()
29972997
}
29982998
}
29992999
};

lib/wasm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = function wasm() {
2020
return cachedWasm;
2121
}
2222

23-
let currentCallback = null;
23+
const callbackStack = [];
2424

2525
cachedWasm = readWasm().then(buffer => {
2626
return WebAssembly.instantiate(buffer, {
@@ -61,7 +61,7 @@ module.exports = function wasm() {
6161
}
6262
}
6363

64-
currentCallback(mapping);
64+
callbackStack[callbackStack.length - 1](mapping);
6565
},
6666

6767
start_all_generated_locations_for: function () { console.time("all_generated_locations_for"); },
@@ -90,11 +90,11 @@ module.exports = function wasm() {
9090
return {
9191
exports: wasm.instance.exports,
9292
withMappingCallback: (mappingCallback, f) => {
93-
currentCallback = mappingCallback;
93+
callbackStack.push(mappingCallback)
9494
try {
9595
f();
9696
} finally {
97-
currentCallback = null;
97+
callbackStack.pop()
9898
}
9999
}
100100
};

test/test-nested-consumer-usage.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const {SourceMapConsumer, SourceMapGenerator} = require('../')
2+
3+
const tsMap = {
4+
version: 3,
5+
file: "blah.js",
6+
sourceRoot: "",
7+
sources: ["blah.tsx"],
8+
names: [],
9+
mappings:
10+
";;AAKA;IACE,MAAM,CAAC,EAAC,MAAM,EAAE,SAAS,EAAC,CAAC;AAC7B,CAAC;AAFD,yBAEC",
11+
sourcesContent: [
12+
"\ntype Cheese = {\n readonly cheese: string\n}\n\nexport default function Cheese(): Cheese {\n return {cheese: 'stilton'};\n}\n"
13+
]
14+
};
15+
16+
const babelMap = {
17+
version: 3,
18+
sources: ["blah.tsx"],
19+
names: [
20+
"Object",
21+
"defineProperty",
22+
"exports",
23+
"value",
24+
"Cheese",
25+
"cheese",
26+
"default"
27+
],
28+
mappings:
29+
"AAAA;;AACAA,OAAOC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C,EAAEC,OAAO,IAAT,EAA7C;AACA,SAASC,MAAT,GAAkB;AACd,WAAO,EAAEC,QAAQ,SAAV,EAAP;AACH;AACDH,QAAQI,OAAR,GAAkBF,MAAlB",
30+
sourcesContent: [
31+
'"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\nfunction Cheese() {\n return { cheese: \'stilton\' };\n}\nexports.default = Cheese;\n//# sourceMappingURL=blah.js.map'
32+
]
33+
};
34+
35+
36+
async function composeSourceMaps(
37+
tsMap,
38+
babelMap,
39+
tsFileName,
40+
) {
41+
const tsConsumer = await new SourceMapConsumer(tsMap)
42+
const babelConsumer = await new SourceMapConsumer(babelMap)
43+
const map = new SourceMapGenerator()
44+
babelConsumer.eachMapping(
45+
({
46+
source,
47+
generatedLine,
48+
generatedColumn,
49+
originalLine,
50+
originalColumn,
51+
name,
52+
}) => {
53+
if (originalLine) {
54+
const original = tsConsumer.originalPositionFor({
55+
line: originalLine,
56+
column: originalColumn,
57+
})
58+
if (original.line) {
59+
map.addMapping({
60+
generated: {
61+
line: generatedLine,
62+
column: generatedColumn,
63+
},
64+
original: {
65+
line: original.line,
66+
column: original.column,
67+
},
68+
source: tsFileName,
69+
name: name,
70+
})
71+
}
72+
}
73+
}
74+
)
75+
return map.toJSON()
76+
}
77+
78+
exports["test nested consumer usage"] = async function (assert) {
79+
await composeSourceMaps(tsMap, babelMap, 'blah.tsx')
80+
};

0 commit comments

Comments
 (0)