From 8bbbc3a6f3282d5f953c715a655d4db3f6c39735 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Fri, 7 Jul 2017 21:02:43 +1000 Subject: [PATCH] Remove parens for type assertions in binary expressions, fixes #2416 (#2419) --- src/fast-path.js | 2 +- tests/typescript_cast/__snapshots__/jsfmt.spec.js.snap | 10 ++++++++++ tests/typescript_cast/parenthesis.ts | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/fast-path.js b/src/fast-path.js index 7d6b82a1..255f2cc1 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -305,7 +305,7 @@ FastPath.prototype.needsParens = function(options) { case "BinaryExpression": case "LogicalExpression": { - if (!node.operator) { + if (!node.operator && node.type !== "TSTypeAssertionExpression") { return true; } diff --git a/tests/typescript_cast/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_cast/__snapshots__/jsfmt.spec.js.snap index d2810f32..d7636493 100644 --- a/tests/typescript_cast/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_cast/__snapshots__/jsfmt.spec.js.snap @@ -3,8 +3,18 @@ exports[`parenthesis.ts 1`] = ` (a ? b : c); (() => {}); + +a || {}; +a && []; +true || a; +a + b; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (a ? b : c); (() => {}); +a || {}; +a && []; +true || a; +a + b; + `; diff --git a/tests/typescript_cast/parenthesis.ts b/tests/typescript_cast/parenthesis.ts index fd356da4..38808d55 100644 --- a/tests/typescript_cast/parenthesis.ts +++ b/tests/typescript_cast/parenthesis.ts @@ -1,2 +1,7 @@ (a ? b : c); (() => {}); + +a || {}; +a && []; +true || a; +a + b;