From b93aadb81107188ee02bf5a11304b32ab0cb330a Mon Sep 17 00:00:00 2001 From: David Sheldrick Date: Sat, 21 Apr 2018 20:03:31 +0100 Subject: [PATCH 1/3] add failing test for nested consumer usage --- test/test-nested-consumer-usage.js | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test/test-nested-consumer-usage.js diff --git a/test/test-nested-consumer-usage.js b/test/test-nested-consumer-usage.js new file mode 100644 index 00000000..e2496494 --- /dev/null +++ b/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') +}; From 121d6271c4105133a3f8044f057c735613705698 Mon Sep 17 00:00:00 2001 From: David Sheldrick Date: Sat, 21 Apr 2018 19:32:51 +0100 Subject: [PATCH 2/3] keep callbacks on stack to allow nested consumer usage --- lib/wasm.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/wasm.js b/lib/wasm.js index 58dc64bf..b713615e 100644 --- a/lib/wasm.js +++ b/lib/wasm.js @@ -20,7 +20,7 @@ module.exports = function wasm() { return cachedWasm; } - let currentCallback = null; + const callbackStack = []; cachedWasm = readWasm().then(buffer => { return WebAssembly.instantiate(buffer, { @@ -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"); }, @@ -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() } } }; From b87dfc8185f1aba721a797cee3585cc3a05a2c25 Mon Sep 17 00:00:00 2001 From: David Sheldrick Date: Sat, 21 Apr 2018 19:36:05 +0100 Subject: [PATCH 3/3] update dist --- dist/source-map.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/source-map.js b/dist/source-map.js index 4d485444..afaba05e 100644 --- a/dist/source-map.js +++ b/dist/source-map.js @@ -2919,7 +2919,7 @@ module.exports = function wasm() { return cachedWasm; } - let currentCallback = null; + const callbackStack = []; cachedWasm = readWasm().then(buffer => { return WebAssembly.instantiate(buffer, { @@ -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"); }, @@ -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() } } };