Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_js_formatter): no comment reordeing in default clause
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Mar 4, 2023
1 parent e516c31 commit ec3722a
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl CommentStyle for JsCommentStyle {
.or_else(handle_call_expression_comment)
.or_else(handle_continue_break_comment)
.or_else(handle_mapped_type_comment)
.or_else(handle_switch_default_case_comment)
//.or_else(handle_switch_default_case_comment)
.or_else(handle_import_export_specifier_comment),
CommentTextPosition::OwnLine => handle_member_expression_comment(comment)
.or_else(handle_function_declaration_comment)
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/js/auxiliary/case_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FormatNodeRule<JsCaseClause> for FormatJsCaseClause {
);

if consequent.is_empty() {
// Skip inserting an indent block is the consequent is empty to print
// Skip inserting an indent block if the consequent is empty to print
// the trailing comments for the case clause inline if there is no
// block to push them into
write!(f, [hard_line_break()])
Expand Down
22 changes: 11 additions & 11 deletions crates/rome_js_formatter/src/js/auxiliary/default_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
consequent,
} = node.as_fields();

let first_child_is_block_stmt = matches!(
write!(f, [default_token.format(), colon_token.format()])?;

let is_first_child_block_stmt = matches!(
consequent.iter().next(),
Some(AnyJsStatement::JsBlockStatement(_))
);

write!(f, [default_token.format(), colon_token.format()])?;

if f.comments().has_dangling_comments(node.syntax()) {
write!(f, [space(), format_dangling_comments(node.syntax())])?;
}
//if f.comments().has_dangling_comments(node.syntax()) {
// write!(f, [space(), format_dangling_comments(node.syntax())])?;
//}

if consequent.is_empty() {
write!(f, [hard_line_break()])
} else if first_child_is_block_stmt {
} else if is_first_child_block_stmt {
write!(f, [space(), consequent.format()])
} else {
// no line break needed after because it is added by the indent in the switch statement
Expand All @@ -42,8 +42,8 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
}
}

fn fmt_dangling_comments(&self, _: &JsDefaultClause, _: &mut JsFormatter) -> FormatResult<()> {
// Handled inside of `fmt_fields`
Ok(())
}
//fn fmt_dangling_comments(&self, _: &JsDefaultClause, _: &mut JsFormatter) -> FormatResult<()> {
// // Handled inside of `fmt_fields`
// Ok(())
//}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
info: js/switch/comments.js
---

# Input

```js
switch (true) {
case true:
// Good luck getting here

case false:
}

switch (true) {
case true:

// Good luck getting here
case false:
}

switch(x) {
case x: {
}

// other

case y: {
}
}

switch(x) {
default: // comment
break;
}

switch(x) {
default: // comment
{break;}
}

switch(x) {
default: {// comment
break;}
}

switch(x) {
default: /* comment */
break;
}

switch(x) {
default: /* comment */
{break;}
}

switch(x) {
default: {/* comment */
break;}
}

switch(x) {
default: /* comment */ {
break;}
}

```


# Prettier differences

```diff
--- Prettier
+++ Rome
@@ -23,13 +23,14 @@
}

switch (x) {
- default: // comment
+ default:
+ // comment
break;
}

switch (x) {
- default: {
- // comment
+ default: // comment
+ {
break;
}
}
@@ -42,12 +43,14 @@
}

switch (x) {
- default: /* comment */
+ default:
+ /* comment */
break;
}

switch (x) {
- default: /* comment */ {
+ default: /* comment */
+ {
break;
}
}
```

# Output

```js
switch (true) {
case true:
// Good luck getting here

case false:
}

switch (true) {
case true:

// Good luck getting here
case false:
}

switch (x) {
case x: {
}

// other

case y: {
}
}

switch (x) {
default:
// comment
break;
}

switch (x) {
default: // comment
{
break;
}
}

switch (x) {
default: {
// comment
break;
}
}

switch (x) {
default:
/* comment */
break;
}

switch (x) {
default: /* comment */
{
break;
}
}

switch (x) {
default: {
/* comment */
break;
}
}

switch (x) {
default: /* comment */ {
break;
}
}
```


0 comments on commit ec3722a

Please sign in to comment.