From d6e3815c1939818989dd0dff1788f8deaf1987fc Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 28 Feb 2017 09:31:04 -0800 Subject: [PATCH] Allow breaking for logical expressions in member chains (#827) I'm not really sure what a general rule is for those, but starting with LogicalExpressions should be good. Outside of identifiers, function calls and logical expressions, there aren't a lot of things you'd put there in real code anyway. Fixes #822 --- src/printer.js | 5 ++++- tests/method-chain/__snapshots__/jsfmt.spec.js.snap | 9 +++++++++ tests/method-chain/logical.js | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/method-chain/logical.js diff --git a/src/printer.js b/src/printer.js index 3a9e6e86d..d39bbc2c5 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2394,7 +2394,10 @@ function printMemberChain(path, options, print) { // If we only have a single `.`, we shouldn't do anything fancy and just // render everything concatenated together. - if (groups.length <= (shouldMerge ? 3 : 2) && !hasComment) { + if (groups.length <= (shouldMerge ? 3 : 2) && + !hasComment && + // (a || b).map() should be break before .map() instead of || + groups[0][0].node.type !== "LogicalExpression") { return group(oneLine); } diff --git a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap index 7d364b6fe..0ca8d7e0a 100644 --- a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap +++ b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap @@ -224,6 +224,15 @@ Object.keys( " `; +exports[`logical.js 1`] = ` +"(veryLongVeryLongVeryLong || e).map(tickets => + TicketRecord.createFromSomeLongString()); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +(veryLongVeryLongVeryLong || e) + .map(tickets => TicketRecord.createFromSomeLongString()); +" +`; + exports[`multiple-members.js 1`] = ` "if (testConfig.ENABLE_ONLINE_TESTS === \\"true\\") { describe(\\"POST /users/me/pet\\", function() { diff --git a/tests/method-chain/logical.js b/tests/method-chain/logical.js new file mode 100644 index 000000000..a7ab0442d --- /dev/null +++ b/tests/method-chain/logical.js @@ -0,0 +1,2 @@ +(veryLongVeryLongVeryLong || e).map(tickets => + TicketRecord.createFromSomeLongString());