Skip to content

Commit

Permalink
Consistently use tabs in ternaries when useTabs is true (#15662)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
Co-authored-by: SUZUKI Sosuke <aosukeke@gmail.com>
  • Loading branch information
3 people authored Dec 10, 2023
1 parent 5f7aedc commit 0d1ffb3
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 162 deletions.
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),
];
parts.push(
parent.type !== node.type ||
Expand Down
Loading

0 comments on commit 0d1ffb3

Please sign in to comment.