Skip to content

Commit

Permalink
challenge(formatter): indent nested conditional expressions (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 21, 2023
1 parent e90ca6f commit 9cc8f96
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 2,952 deletions.
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

0 comments on commit 9cc8f96

Please sign in to comment.