Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 5c58945

Browse files
loganfsmythjasonLaster
authored andcommitted
Expose 'getMappedExpression' from debugger panel and tweak mapping (#5822)
1 parent c95e086 commit 5c58945

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

assets/panel/panel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ DebuggerPanel.prototype = {
116116
return { frames, selected };
117117
},
118118

119+
getMappedExpression(expression) {
120+
return this._actions.getMappedExpression(expression);
121+
},
122+
119123
isPaused() {
120124
return this._selectors.isPaused(this._getState());
121125
},

src/test/mochitest/browser_dbg-babel-preview.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ add_task(async function() {
8585
{
8686
line: 5,
8787
column: 7,
88-
expression: "doThing;",
88+
expression: "doThing",
8989
result: "doThing(arg)",
9090
},
9191
{
9292
line: 5,
9393
column: 12,
94-
expression: "x;",
94+
expression: "x",
9595
result: "1",
9696
},
9797
{
9898
line: 8,
9999
column: 16,
100-
expression: "doThing;",
100+
expression: "doThing",
101101
result: "doThing(arg)",
102102
},
103103
]);
@@ -110,7 +110,7 @@ add_task(async function() {
110110
{
111111
line: 2,
112112
column: 9,
113-
expression: "aVar;",
113+
expression: "aVar",
114114
result: '"var3"',
115115
},
116116
{
@@ -128,7 +128,7 @@ add_task(async function() {
128128
{
129129
line: 10,
130130
column: 11,
131-
expression: "aVar;",
131+
expression: "aVar",
132132
result: '"var3"',
133133
},
134134
{
@@ -148,7 +148,7 @@ add_task(async function() {
148148
{
149149
line: 14,
150150
column: 13,
151-
expression: "aVar;",
151+
expression: "aVar",
152152
result: '"var3"',
153153
},
154154
{
@@ -193,7 +193,7 @@ add_task(async function() {
193193
{
194194
line: 26,
195195
column: 16,
196-
expression: "aNamespace;",
196+
expression: "aNamespace",
197197
fields: [
198198
['aNamed', 'a-named'],
199199
['default', 'a-default'],
@@ -226,7 +226,7 @@ add_task(async function() {
226226
{
227227
line: 35,
228228
column: 20,
229-
expression: "aNamespace2;",
229+
expression: "aNamespace2",
230230
fields: [
231231
['aNamed', 'a-named2'],
232232
['default', 'a-default2'],

src/workers/parser/mapOriginalExpression.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export default function mapOriginalExpression(
3030
[string]: string | null
3131
}
3232
): string {
33+
let didReplace = false;
34+
3335
const ast = parseScript(expression);
3436
t.traverse(ast, (node, ancestors) => {
3537
const parent = ancestors[ancestors.length - 1];
@@ -41,14 +43,21 @@ export default function mapOriginalExpression(
4143
if (t.isIdentifier(node) && t.isReferenced(node, parentNode)) {
4244
if (mappings.hasOwnProperty(node.name)) {
4345
const mapping = mappings[node.name];
44-
if (mapping) {
46+
if (mapping && mapping !== node.name) {
4547
const mappingNode = getFirstExpression(parseScript(mapping));
46-
4748
replaceNode(ancestors, mappingNode);
49+
50+
didReplace = true;
4851
}
4952
}
5053
}
5154
});
5255

53-
return generate(ast, { concise: true }).code;
56+
if (!didReplace) {
57+
// Avoid the extra code generation work and also avoid potentially
58+
// reformatting the user's code unnecessarily.
59+
return expression;
60+
}
61+
62+
return generate(ast).code;
5463
}

src/workers/parser/tests/mapOriginalExpression.spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe("mapOriginalExpression", () => {
2323
a: "_mod.foo",
2424
b: "_mod.bar"
2525
});
26-
expect(generatedExpression).toEqual("{ _mod.foo; }");
26+
expect(generatedExpression).toEqual("{\n _mod.foo;\n}");
27+
});
28+
29+
it("skips codegen with no mappings", () => {
30+
const generatedExpression = mapOriginalExpression("a + b", {
31+
a: "a",
32+
c: "_c"
33+
});
34+
expect(generatedExpression).toEqual("a + b");
2735
});
2836
});

0 commit comments

Comments
 (0)