From bba7dcf498854853b44ec6a78a4c53b263f4236d Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sun, 10 Sep 2017 02:18:40 -0300 Subject: [PATCH] Fix break on conditional expressions inside return Fixes #2777 Since we can't break after `return` and don't add `()` around ConditionalExpressions' tests, we end up with some weird indentation when breaking. --- src/printer.js | 3 +- .../__snapshots__/jsfmt.spec.js.snap | 40 +++++++++++++++++++ tests/binary-expressions/return.js | 15 +++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/binary-expressions/return.js diff --git a/src/printer.js b/src/printer.js index 420e87260..6b3334634 100644 --- a/src/printer.js +++ b/src/printer.js @@ -346,7 +346,8 @@ function genericPrintNoParens(path, options, print, args) { parentParent.type === "JSXAttribute") || (n === parent.body && parent.type === "ArrowFunctionExpression") || (n !== parent.body && parent.type === "ForStatement") || - parent.type === "ConditionalExpression"; + (parent.type === "ConditionalExpression" && + parentParent.type !== "ReturnStatement"); const shouldIdentIfInlining = parent.type === "AssignmentExpression" || diff --git a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap index fd0d05c85..5c4ba464f 100644 --- a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap @@ -352,6 +352,46 @@ x & (y >> z); `; +exports[`return.js 1`] = ` +function foo() { + return this.hasPlugin("dynamicImports") && this.lookahead().type === tt.parenLeft.right; +} + +function foo() { + return this.hasPlugin("dynamicImports") && this.lookahead().type === tt.parenLeft.right + ? true + : false; +} + +function foo() { + return this.calculate().compute().first.numberOfThings > this.calculate().compute().last.numberOfThings + ? true + : false; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function foo() { + return ( + this.hasPlugin("dynamicImports") && + this.lookahead().type === tt.parenLeft.right + ); +} + +function foo() { + return this.hasPlugin("dynamicImports") && + this.lookahead().type === tt.parenLeft.right + ? true + : false; +} + +function foo() { + return this.calculate().compute().first.numberOfThings > + this.calculate().compute().last.numberOfThings + ? true + : false; +} + +`; + exports[`short-right.js 1`] = ` this._cumulativeHeights && Math.abs( diff --git a/tests/binary-expressions/return.js b/tests/binary-expressions/return.js new file mode 100644 index 000000000..44ba5b067 --- /dev/null +++ b/tests/binary-expressions/return.js @@ -0,0 +1,15 @@ +function foo() { + return this.hasPlugin("dynamicImports") && this.lookahead().type === tt.parenLeft.right; +} + +function foo() { + return this.hasPlugin("dynamicImports") && this.lookahead().type === tt.parenLeft.right + ? true + : false; +} + +function foo() { + return this.calculate().compute().first.numberOfThings > this.calculate().compute().last.numberOfThings + ? true + : false; +}