Skip to content

Commit 0d1ffb3

Browse files
auvredfiskersosukesuzuki
authored
Consistently use tabs in ternaries when useTabs is true (#15662)
Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: SUZUKI Sosuke <aosukeke@gmail.com>
1 parent 5f7aedc commit 0d1ffb3

File tree

3 files changed

+219
-162
lines changed

3 files changed

+219
-162
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#### Consistently use tabs in ternaries when `useTabs` is `true` (#15662 by @auvred)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
aaaaaaaaaaaaaaa
7+
? bbbbbbbbbbbbbbbbbb
8+
: ccccccccccccccc
9+
? ddddddddddddddd
10+
: eeeeeeeeeeeeeee
11+
? fffffffffffffff
12+
: gggggggggggggggg;
13+
14+
// Prettier stable
15+
aaaaaaaaaaaaaaa
16+
? bbbbbbbbbbbbbbbbbb
17+
: ccccccccccccccc
18+
? ddddddddddddddd
19+
: eeeeeeeeeeeeeee
20+
? fffffffffffffff
21+
: gggggggggggggggg;
22+
23+
// Prettier main
24+
aaaaaaaaaaaaaaa
25+
? bbbbbbbbbbbbbbbbbb
26+
: ccccccccccccccc
27+
? ddddddddddddddd
28+
: eeeeeeeeeeeeeee
29+
? fffffffffffffff
30+
: gggggggggggggggg;
31+
```

src/language-js/print/ternary-old.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,42 @@ function printTernaryOld(path, options, print) {
274274
: wrap(print(alternateNodePropertyName)),
275275
);
276276
} else {
277+
/*
278+
This does not mean to indent, but make the doc aligned with the first character after `? ` or `: `,
279+
so we use `2` instead of `options.tabWidth` here.
280+
281+
```js
282+
test
283+
? {
284+
consequent
285+
}
286+
: alternate
287+
```
288+
289+
instead of
290+
291+
```js
292+
test
293+
? {
294+
consequent
295+
}
296+
: alternate
297+
```
298+
*/
299+
const printBranch = (nodePropertyName) =>
300+
options.useTabs
301+
? indent(print(nodePropertyName))
302+
: align(2, print(nodePropertyName));
277303
// normal mode
278304
const part = [
279305
line,
280306
"? ",
281307
consequentNode.type === node.type ? ifBreak("", "(") : "",
282-
align(2, print(consequentNodePropertyName)),
308+
printBranch(consequentNodePropertyName),
283309
consequentNode.type === node.type ? ifBreak("", ")") : "",
284310
line,
285311
": ",
286-
align(2, print(alternateNodePropertyName)),
312+
printBranch(alternateNodePropertyName),
287313
];
288314
parts.push(
289315
parent.type !== node.type ||

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy