From 93f852c75295e56af56a0ceaeaf5f13f972b66ed Mon Sep 17 00:00:00 2001 From: Sahil mobaidin <33333226+sahilmob@users.noreply.github.com> Date: Thu, 6 Jan 2022 21:10:14 +0200 Subject: [PATCH] wrap arrow function body in parentheses when it is an object destructuring assignment (#70) --- src/print/handlers.js | 5 ++++- .../expected.js | 1 + .../expected.js.map | 11 +++++++++++ .../arrow-function-assignment-object-pattern/input.js | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/print/handlers.js create mode 100644 test/samples/arrow-function-assignment-object-pattern/expected.js create mode 100644 test/samples/arrow-function-assignment-object-pattern/expected.js.map create mode 100644 test/samples/arrow-function-assignment-object-pattern/input.js diff --git a/src/print/handlers.js b/src/print/handlers.js old mode 100644 new mode 100755 index f2e3d1a..de4d01e --- a/src/print/handlers.js +++ b/src/print/handlers.js @@ -882,7 +882,10 @@ const handlers = { chunks.push(c(' => ')); - if (node.body.type === 'ObjectExpression') { + if ( + node.body.type === 'ObjectExpression' || + (node.body.type === 'AssignmentExpression' && node.body.left.type === 'ObjectPattern') + ) { chunks.push( c('('), ...handle(node.body, state), diff --git a/test/samples/arrow-function-assignment-object-pattern/expected.js b/test/samples/arrow-function-assignment-object-pattern/expected.js new file mode 100644 index 0000000..baa90dd --- /dev/null +++ b/test/samples/arrow-function-assignment-object-pattern/expected.js @@ -0,0 +1 @@ +let a = () => ({ x } = { x: 42 }); \ No newline at end of file diff --git a/test/samples/arrow-function-assignment-object-pattern/expected.js.map b/test/samples/arrow-function-assignment-object-pattern/expected.js.map new file mode 100644 index 0000000..36eac76 --- /dev/null +++ b/test/samples/arrow-function-assignment-object-pattern/expected.js.map @@ -0,0 +1,11 @@ +{ + "version": 3, + "names": [], + "sources": [ + "input.js" + ], + "sourcesContent": [ + null + ], + "mappings": "" +} \ No newline at end of file diff --git a/test/samples/arrow-function-assignment-object-pattern/input.js b/test/samples/arrow-function-assignment-object-pattern/input.js new file mode 100644 index 0000000..ca0e50e --- /dev/null +++ b/test/samples/arrow-function-assignment-object-pattern/input.js @@ -0,0 +1 @@ +export default ({ b }) => b`let a = () => ({ x } = { x: 42 });`;