Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nested consumer usage #330

Merged
merged 3 commits into from Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/source-map.js
Expand Up @@ -2919,7 +2919,7 @@ module.exports = function wasm() {
return cachedWasm;
}

let currentCallback = null;
const callbackStack = [];

cachedWasm = readWasm().then(buffer => {
return WebAssembly.instantiate(buffer, {
Expand Down Expand Up @@ -2960,7 +2960,7 @@ module.exports = function wasm() {
}
}

currentCallback(mapping);
callbackStack[callbackStack.length - 1](mapping);
},

start_all_generated_locations_for: function () { console.time("all_generated_locations_for"); },
Expand Down Expand Up @@ -2989,11 +2989,11 @@ module.exports = function wasm() {
return {
exports: wasm.instance.exports,
withMappingCallback: (mappingCallback, f) => {
currentCallback = mappingCallback;
callbackStack.push(mappingCallback)
try {
f();
} finally {
currentCallback = null;
callbackStack.pop()
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions lib/wasm.js
Expand Up @@ -20,7 +20,7 @@ module.exports = function wasm() {
return cachedWasm;
}

let currentCallback = null;
const callbackStack = [];

cachedWasm = readWasm().then(buffer => {
return WebAssembly.instantiate(buffer, {
Expand Down Expand Up @@ -61,7 +61,7 @@ module.exports = function wasm() {
}
}

currentCallback(mapping);
callbackStack[callbackStack.length - 1](mapping);
},

start_all_generated_locations_for: function () { console.time("all_generated_locations_for"); },
Expand Down Expand Up @@ -90,11 +90,11 @@ module.exports = function wasm() {
return {
exports: wasm.instance.exports,
withMappingCallback: (mappingCallback, f) => {
currentCallback = mappingCallback;
callbackStack.push(mappingCallback)
try {
f();
} finally {
currentCallback = null;
callbackStack.pop()
}
}
};
Expand Down
80 changes: 80 additions & 0 deletions test/test-nested-consumer-usage.js
@@ -0,0 +1,80 @@
const {SourceMapConsumer, SourceMapGenerator} = require('../')

const tsMap = {
version: 3,
file: "blah.js",
sourceRoot: "",
sources: ["blah.tsx"],
names: [],
mappings:
";;AAKA;IACE,MAAM,CAAC,EAAC,MAAM,EAAE,SAAS,EAAC,CAAC;AAC7B,CAAC;AAFD,yBAEC",
sourcesContent: [
"\ntype Cheese = {\n readonly cheese: string\n}\n\nexport default function Cheese(): Cheese {\n return {cheese: 'stilton'};\n}\n"
]
};

const babelMap = {
version: 3,
sources: ["blah.tsx"],
names: [
"Object",
"defineProperty",
"exports",
"value",
"Cheese",
"cheese",
"default"
],
mappings:
"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",
sourcesContent: [
'"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\nfunction Cheese() {\n return { cheese: \'stilton\' };\n}\nexports.default = Cheese;\n//# sourceMappingURL=blah.js.map'
]
};


async function composeSourceMaps(
tsMap,
babelMap,
tsFileName,
) {
const tsConsumer = await new SourceMapConsumer(tsMap)
const babelConsumer = await new SourceMapConsumer(babelMap)
const map = new SourceMapGenerator()
babelConsumer.eachMapping(
({
source,
generatedLine,
generatedColumn,
originalLine,
originalColumn,
name,
}) => {
if (originalLine) {
const original = tsConsumer.originalPositionFor({
line: originalLine,
column: originalColumn,
})
if (original.line) {
map.addMapping({
generated: {
line: generatedLine,
column: generatedColumn,
},
original: {
line: original.line,
column: original.column,
},
source: tsFileName,
name: name,
})
}
}
}
)
return map.toJSON()
}

exports["test nested consumer usage"] = async function (assert) {
await composeSourceMaps(tsMap, babelMap, 'blah.tsx')
};