Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(12077) line breaking with unary operators in conditional #12087

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
34 changes: 34 additions & 0 deletions changelog_unreleased/javascript/12087.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#### Constant line breaks for Conditional operators (#12087 by @islandryu)

```js
//--print-width=25
// Input
const a = foo == null ? bar : baz;

const b = !fooooooooo ? bar : baz;

const x = {
prop1: foo == null ? bar : baz,
prop1: !fooooooooo ? bar : baz,
};

// Prettier stable
const a = foo == null ? bar : baz;

const b = !fooooooooo ? bar : baz;

const x = {
prop1: foo == null ? bar : baz,
prop1: !fooooooooo ? bar : baz,
};
islandryu marked this conversation as resolved.
Show resolved Hide resolved

// Prettier main
const a = foo == null ? bar : baz;

const b = !fooooooooo ? bar : baz;

const x = {
prop1: foo == null ? bar : baz,
prop1: !fooooooooo ? bar : baz,
};
islandryu marked this conversation as resolved.
Show resolved Hide resolved
```
5 changes: 4 additions & 1 deletion src/language-js/print/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ function shouldBreakAfterOperator(path, options, print, hasShortKey) {
return true;
case "ConditionalExpression": {
const { test } = rightNode;
return isBinaryish(test) && !shouldInlineLogicalExpression(test);
return (
(test.type === "UnaryExpression" || isBinaryish(test)) &&
!shouldInlineLogicalExpression(test)
);
}
case "ClassExpression":
return isNonEmptyArray(rightNode.decorators);
Expand Down