From d7a44bae1005cac86d3a57b0450d1e66a609b059 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 22 Jan 2017 20:32:17 -0800 Subject: [PATCH] Add parenthesis when class expressions are left of a ternary (#428) Fixes #426 --- src/fast-path.js | 4 ++++ tests/classes/__snapshots__/jsfmt.spec.js.snap | 7 +++++++ tests/classes/ternary.js | 1 + 3 files changed, 12 insertions(+) create mode 100644 tests/classes/ternary.js diff --git a/src/fast-path.js b/src/fast-path.js index 36e0cc1db..35bbd28de 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -452,6 +452,10 @@ FPp.needsParens = function(assumeExpressionContext) { } case "MemberExpression": return name === "object" && parent.object === node; + case "ConditionalExpression": + if (parent.test === node) { + return true; + } } return false; diff --git a/tests/classes/__snapshots__/jsfmt.spec.js.snap b/tests/classes/__snapshots__/jsfmt.spec.js.snap index c3208a09b..28a1e1e84 100644 --- a/tests/classes/__snapshots__/jsfmt.spec.js.snap +++ b/tests/classes/__snapshots__/jsfmt.spec.js.snap @@ -305,3 +305,10 @@ declare var o: { p: number }; (o: Class); // error, object type incompatible with class type " `; + +exports[`test ternary.js 1`] = ` +"if (1) (class {}) ? 1 : 2; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +if (1) (class {}) ? 1 : 2; +" +`; diff --git a/tests/classes/ternary.js b/tests/classes/ternary.js new file mode 100644 index 000000000..3d6727056 --- /dev/null +++ b/tests/classes/ternary.js @@ -0,0 +1 @@ +if (1) (class {}) ? 1 : 2;