Skip to content

Commit

Permalink
Wrap object expressions in parentheses if they become children of an …
Browse files Browse the repository at this point in the history
…arrow function expression (#3540)
  • Loading branch information
lukastaegert committed May 7, 2020
1 parent c445d60 commit 1ca18a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ast/nodes/ObjectExpression.ts
Expand Up @@ -272,7 +272,10 @@ export default class ObjectExpression extends NodeBase implements DeoptimizableE
{ renderedParentType }: NodeRenderOptions = BLANK
) {
super.render(code, options);
if (renderedParentType === NodeType.ExpressionStatement) {
if (
renderedParentType === NodeType.ExpressionStatement ||
renderedParentType === NodeType.ArrowFunctionExpression
) {
code.appendRight(this.start, '(');
code.prependLeft(this.end, ')');
}
Expand Down
@@ -0,0 +1,9 @@
const assert = require('assert');

module.exports = {
description: 'correctly simplifies arrow expressions where the right hand side becomes an object',
exports(exports) {
assert.deepStrictEqual(exports.conditional(), { x: 42, y: 43 });
assert.deepStrictEqual(exports.logical(), { x: 42, y: 43 });
}
};
@@ -0,0 +1,2 @@
export const logical = () => true && { x: 42, y: 43, };
export const conditional = () => true ? { x: 42, y: 43, } : null;

0 comments on commit 1ca18a3

Please sign in to comment.