From 5c589457a60f063a10fc2e47cb1dbbbe0f45b02a Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Thu, 29 Mar 2018 12:44:13 -0700 Subject: [PATCH] Expose 'getMappedExpression' from debugger panel and tweak mapping (#5822) --- assets/panel/panel.js | 4 ++++ src/test/mochitest/browser_dbg-babel-preview.js | 16 ++++++++-------- src/workers/parser/mapOriginalExpression.js | 15 ++++++++++++--- .../parser/tests/mapOriginalExpression.spec.js | 10 +++++++++- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/assets/panel/panel.js b/assets/panel/panel.js index 1fe40dc9f9..f2f74d79da 100644 --- a/assets/panel/panel.js +++ b/assets/panel/panel.js @@ -116,6 +116,10 @@ DebuggerPanel.prototype = { return { frames, selected }; }, + getMappedExpression(expression) { + return this._actions.getMappedExpression(expression); + }, + isPaused() { return this._selectors.isPaused(this._getState()); }, diff --git a/src/test/mochitest/browser_dbg-babel-preview.js b/src/test/mochitest/browser_dbg-babel-preview.js index 585052906b..3fdaa74bbf 100644 --- a/src/test/mochitest/browser_dbg-babel-preview.js +++ b/src/test/mochitest/browser_dbg-babel-preview.js @@ -85,19 +85,19 @@ add_task(async function() { { line: 5, column: 7, - expression: "doThing;", + expression: "doThing", result: "doThing(arg)", }, { line: 5, column: 12, - expression: "x;", + expression: "x", result: "1", }, { line: 8, column: 16, - expression: "doThing;", + expression: "doThing", result: "doThing(arg)", }, ]); @@ -110,7 +110,7 @@ add_task(async function() { { line: 2, column: 9, - expression: "aVar;", + expression: "aVar", result: '"var3"', }, { @@ -128,7 +128,7 @@ add_task(async function() { { line: 10, column: 11, - expression: "aVar;", + expression: "aVar", result: '"var3"', }, { @@ -148,7 +148,7 @@ add_task(async function() { { line: 14, column: 13, - expression: "aVar;", + expression: "aVar", result: '"var3"', }, { @@ -193,7 +193,7 @@ add_task(async function() { { line: 26, column: 16, - expression: "aNamespace;", + expression: "aNamespace", fields: [ ['aNamed', 'a-named'], ['default', 'a-default'], @@ -226,7 +226,7 @@ add_task(async function() { { line: 35, column: 20, - expression: "aNamespace2;", + expression: "aNamespace2", fields: [ ['aNamed', 'a-named2'], ['default', 'a-default2'], diff --git a/src/workers/parser/mapOriginalExpression.js b/src/workers/parser/mapOriginalExpression.js index 737d0ea3dd..4b60b19c94 100644 --- a/src/workers/parser/mapOriginalExpression.js +++ b/src/workers/parser/mapOriginalExpression.js @@ -30,6 +30,8 @@ export default function mapOriginalExpression( [string]: string | null } ): string { + let didReplace = false; + const ast = parseScript(expression); t.traverse(ast, (node, ancestors) => { const parent = ancestors[ancestors.length - 1]; @@ -41,14 +43,21 @@ export default function mapOriginalExpression( if (t.isIdentifier(node) && t.isReferenced(node, parentNode)) { if (mappings.hasOwnProperty(node.name)) { const mapping = mappings[node.name]; - if (mapping) { + if (mapping && mapping !== node.name) { const mappingNode = getFirstExpression(parseScript(mapping)); - replaceNode(ancestors, mappingNode); + + didReplace = true; } } } }); - return generate(ast, { concise: true }).code; + if (!didReplace) { + // Avoid the extra code generation work and also avoid potentially + // reformatting the user's code unnecessarily. + return expression; + } + + return generate(ast).code; } diff --git a/src/workers/parser/tests/mapOriginalExpression.spec.js b/src/workers/parser/tests/mapOriginalExpression.spec.js index 9e9269b6b6..af47764794 100644 --- a/src/workers/parser/tests/mapOriginalExpression.spec.js +++ b/src/workers/parser/tests/mapOriginalExpression.spec.js @@ -23,6 +23,14 @@ describe("mapOriginalExpression", () => { a: "_mod.foo", b: "_mod.bar" }); - expect(generatedExpression).toEqual("{ _mod.foo; }"); + expect(generatedExpression).toEqual("{\n _mod.foo;\n}"); + }); + + it("skips codegen with no mappings", () => { + const generatedExpression = mapOriginalExpression("a + b", { + a: "a", + c: "_c" + }); + expect(generatedExpression).toEqual("a + b"); }); });