Skip to content

Commit 3667817

Browse files
authored
Fix printing a double UnaryExpression (#4551)
Fixes #4476
1 parent b6e6224 commit 3667817

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`convert-esmodule can convert + + 1`] = `
4+
"\\"use strict\\";
5+
c = (10, + +15);
6+
"
7+
`;
8+
39
exports[`convert-esmodule can convert class default exports 1`] = `
410
"\\"use strict\\";
511
Object.defineProperty(exports, \\"__esModule\\", {

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/generator.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@ import * as astring from 'astring';
22
import * as meriyah from 'meriyah';
33
import { Identifier } from 'meriyah/dist/estree';
44

5+
// Enables parenthesis regardless of precedence
6+
const NEEDS_PARENTHESES = 17;
7+
const EXPRESSIONS_PRECEDENCE = {
8+
// Definitions
9+
ArrayExpression: 20,
10+
TaggedTemplateExpression: 20,
11+
ThisExpression: 20,
12+
Identifier: 20,
13+
Literal: 18,
14+
TemplateLiteral: 20,
15+
Super: 20,
16+
SequenceExpression: 20,
17+
// Operations
18+
MemberExpression: 19,
19+
CallExpression: 19,
20+
NewExpression: 19,
21+
// Other definitions
22+
ArrowFunctionExpression: NEEDS_PARENTHESES,
23+
ClassExpression: NEEDS_PARENTHESES,
24+
FunctionExpression: NEEDS_PARENTHESES,
25+
ObjectExpression: NEEDS_PARENTHESES,
26+
// Other operations
27+
UpdateExpression: 16,
28+
UnaryExpression: 15,
29+
BinaryExpression: 14,
30+
LogicalExpression: 13,
31+
ConditionalExpression: 4,
32+
AssignmentExpression: 3,
33+
AwaitExpression: 2,
34+
YieldExpression: 2,
35+
RestElement: 1,
36+
};
37+
538
/**
639
* Add support for next syntax
740
*/
@@ -30,4 +63,32 @@ export const customGenerator = {
3063
this[node.source.type](node.source, state);
3164
state.write(')');
3265
},
66+
UnaryExpression(
67+
node: meriyah.ESTree.UnaryExpression,
68+
state: { write(s: string): void }
69+
) {
70+
if (node.prefix) {
71+
state.write(node.operator);
72+
if (
73+
node.operator.length > 1 ||
74+
node.argument.type === 'UnaryExpression'
75+
) {
76+
state.write(' ');
77+
}
78+
if (
79+
EXPRESSIONS_PRECEDENCE[node.argument.type] <
80+
EXPRESSIONS_PRECEDENCE.UnaryExpression
81+
) {
82+
state.write('(');
83+
this[node.argument.type](node.argument, state);
84+
state.write(')');
85+
} else {
86+
this[node.argument.type](node.argument, state);
87+
}
88+
} else {
89+
// FIXME: This case never occurs
90+
this[node.argument.type](node.argument, state);
91+
state.write(node.operator);
92+
}
93+
},
3394
};

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,12 @@ describe('convert-esmodule', () => {
423423

424424
expect(convertEsModule(code)).toMatchSnapshot();
425425
});
426+
427+
it('can convert + +', () => {
428+
const code = `
429+
c = (10.0, + +(15))
430+
`;
431+
432+
expect(convertEsModule(code)).toMatchSnapshot();
433+
});
426434
});

0 commit comments

Comments
 (0)