From 2acc3e5ea659b493019a7221a52b69d29e2cc32e Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 22 Jan 2017 19:04:00 -0800 Subject: [PATCH] Add parenthesis around assignment for arrow function body (#422) Fixes #412 --- src/fast-path.js | 6 ++++++ tests/arrows/__snapshots__/jsfmt.spec.js.snap | 4 ++++ tests/arrows/arrows.js | 2 ++ 3 files changed, 12 insertions(+) diff --git a/src/fast-path.js b/src/fast-path.js index ba07bbc41..a531a96fe 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -376,6 +376,12 @@ FPp.needsParens = function(assumeExpressionContext) { parent.object === node; case "AssignmentExpression": + if (parent.type === "ArrowFunctionExpression" && + parent.body === node && + node.left.type === "ObjectPattern") { + return true; + } + case "ConditionalExpression": switch (parent.type) { case "TaggedTemplateExpression": diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index 00375d891..3a2606f90 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -64,6 +64,8 @@ exports[`test arrows.js 1`] = ` return images.find(image => image.width >= maxPixelWidth) || images[images.length - 1]; } + +a = () => ({} = this); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function selectBestEffortImageForWidth( maxWidth: number, @@ -75,6 +77,8 @@ function selectBestEffortImageForWidth( return images.find(image => image.width >= maxPixelWidth) || images[images.length - 1]; } + +a = () => ({} = this); " `; diff --git a/tests/arrows/arrows.js b/tests/arrows/arrows.js index f25cc0fb3..93f6b3c46 100644 --- a/tests/arrows/arrows.js +++ b/tests/arrows/arrows.js @@ -8,3 +8,5 @@ function selectBestEffortImageForWidth( return images.find(image => image.width >= maxPixelWidth) || images[images.length - 1]; } + +a = () => ({} = this);