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

challenge(formatter): indent nested conditional expressions #816

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 1 addition & 17 deletions crates/biome_js_formatter/src/utils/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,7 @@ impl FormatRule<AnyJsConditional> for FormatJsAnyConditionalRule {
space()
]
)?;

let is_alternate_nested = alternate.syntax().kind() == syntax.kind();

// Don't "indent" a nested alternate by two spaces so that the consequent and alternates nicely align
// ```
// // Prefer **this** over // **this**
// aLongLongLongLong aLongLongLongLong
// ? bLongLongLongLong ? bLongLongLongLong
// : cLongLongLong : cLongLongLong
// ? dLongLongLong ? dLongLong
// : eLongLongLong; : eLongLongLong
// ```
if is_alternate_nested {
write!(f, [alternate])
} else {
write!(f, [align(2, &alternate)])
}
write!(f, [align(2, &alternate)])
});

let format_tail_with_indent = format_with(|f: &mut JsFormatter| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ somethingThatsAReallyLongPropName
somethingThatsAReallyLongPropName
? somethingThatsAReallyLongPropName
: somethingThatsAReallyLongPropName
? somethingThatsAReallyLongPropName
: somethingThatsAReallyLongPropName;
? somethingThatsAReallyLongPropName
: somethingThatsAReallyLongPropName;
```


Original file line number Diff line number Diff line change
Expand Up @@ -137,37 +137,7 @@ c */? foo : bar : bar;
```diff
--- Prettier
+++ Biome
@@ -63,11 +63,11 @@
*/
foo
: test
- ? /* comment
+ ? /* comment
comment
comment */
- foo
- : bar;
+ foo
+ : bar;

test ? /* comment */ foo : bar;

@@ -88,29 +88,30 @@
comment
A newline will be added after this comment, unfortunately – but it can be removed manually, see next statement.
*/
- test
- ? foo
- : /* comment
+ test
+ ? foo
+ : /* comment
comment
comment
*/
- bar;
+ bar;

@@ -99,12 +99,13 @@
// It is at least possible to delete the extra newline that was
// unfortunately added before the second condition above:
test
Expand All @@ -179,19 +149,10 @@ c */? foo : bar : bar;
comment
*/
- : test
- ? foo
- : /* comment
+ test
+ ? foo
+ : /* comment
+ test
? foo
: /* comment
comment
comment
*/
- bar;
+ bar;

test ? foo : /* comment */ bar;

```

# Output
Expand Down Expand Up @@ -262,11 +223,11 @@ test
*/
foo
: test
? /* comment
? /* comment
comment
comment */
foo
: bar;
foo
: bar;

test ? /* comment */ foo : bar;

Expand All @@ -287,13 +248,13 @@ test
comment
A newline will be added after this comment, unfortunately – but it can be removed manually, see next statement.
*/
test
? foo
: /* comment
test
? foo
: /* comment
comment
comment
*/
bar;
bar;

// It is at least possible to delete the extra newline that was
// unfortunately added before the second condition above:
Expand All @@ -304,13 +265,13 @@ test
comment
comment
*/
test
? foo
: /* comment
test
? foo
: /* comment
comment
comment
*/
bar;
bar;

test ? foo : /* comment */ bar;

Expand Down