From 86c65d1f81e330890ac4453a2c7c063b0ffb5024 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 16 Jan 2017 11:08:46 -0600 Subject: [PATCH] Fix missing parenthesis for typeof and arrow functions (#249) --- src/fast-path.js | 11 +++++++---- tests/arrows/__snapshots__/jsfmt.spec.js.snap | 3 +++ tests/arrows/arrow_function_expression.js | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/fast-path.js b/src/fast-path.js index af4392cb..dd4b2b55 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -371,11 +371,14 @@ FPp.needsParens = function(assumeExpressionContext) { return true; } - if (parent.type === "MemberExpression") { - return true; - } + switch (parent.type) { + case "MemberExpression": + case "UnaryExpression": + return true; - return isBinary(parent); + default: + return isBinary(parent); + } case "ClassExpression": return parent.type === "ExpressionStatement"; diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index 728c2470..bf224ddd 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -27,9 +27,12 @@ var ident = (x: T): T => x; exports[`test arrow_function_expression.js 1`] = ` "(a => {}).length +typeof (() => {}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (a => { }).length; +typeof (() => { +}); " `; diff --git a/tests/arrows/arrow_function_expression.js b/tests/arrows/arrow_function_expression.js index ecea2142..57a6e920 100644 --- a/tests/arrows/arrow_function_expression.js +++ b/tests/arrows/arrow_function_expression.js @@ -1 +1,2 @@ (a => {}).length +typeof (() => {});