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

Consistently use tabs in ternaries when useTabs is true #15662

Merged
merged 14 commits into from
Dec 10, 2023
31 changes: 31 additions & 0 deletions changelog_unreleased/javascript/15662.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#### Consistently use tabs in ternaries when `useTabs` is `true` (#15662 by @auvred)

<!-- prettier-ignore -->
```jsx
// Input
aaaaaaaaaaaaaaa
? bbbbbbbbbbbbbbbbbb
: ccccccccccccccc
? ddddddddddddddd
: eeeeeeeeeeeeeee
? fffffffffffffff
: gggggggggggggggg;

// Prettier stable
aaaaaaaaaaaaaaa
? bbbbbbbbbbbbbbbbbb
: ccccccccccccccc
? ddddddddddddddd
: eeeeeeeeeeeeeee
? fffffffffffffff
: gggggggggggggggg;

// Prettier main
aaaaaaaaaaaaaaa
? bbbbbbbbbbbbbbbbbb
: ccccccccccccccc
? ddddddddddddddd
: eeeeeeeeeeeeeee
? fffffffffffffff
: gggggggggggggggg;
```
30 changes: 28 additions & 2 deletions src/language-js/print/ternary-old.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,42 @@ function printTernaryOld(path, options, print) {
: wrap(print(alternateNodePropertyName)),
);
} else {
/*
This does not mean to indent, but make the doc aligned with the first character after `? ` or `: `,
so we use `2` instead of `options.tabWidth` here.

```js
test
? {
consequent
}
: alternate
```

instead of

```js
test
? {
consequent
}
: alternate
```
*/
const printBranch = (nodePropertyName) =>
options.useTabs
? indent(print(nodePropertyName))
: align(2, print(nodePropertyName));
// normal mode
const part = [
line,
"? ",
consequentNode.type === node.type ? ifBreak("", "(") : "",
align(2, print(consequentNodePropertyName)),
printBranch(consequentNodePropertyName),
consequentNode.type === node.type ? ifBreak("", ")") : "",
line,
": ",
align(2, print(alternateNodePropertyName)),
printBranch(alternateNodePropertyName),
auvred marked this conversation as resolved.
Show resolved Hide resolved
];
parts.push(
parent.type !== node.type ||
Expand Down
Loading
Loading