Skip to content

Commit 50e9333

Browse files
authored
fix: maintain indented trailing comments in non-last switch case (#412)
1 parent 2192c94 commit 50e9333

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/generation/generate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,13 +4833,14 @@ fn gen_switch_case<'a>(node: &'a SwitchCase, context: &mut Context<'a>) -> Print
48334833
_ => false,
48344834
};
48354835
let mut is_equal_indent = block_stmt_body.is_some();
4836-
let mut last_node = node_range;
4836+
let mut last_range = node_range;
4837+
let last_node_column = node_range.start_column_fast(context.program);
48374838

48384839
for comment in trailing_comments {
4839-
is_equal_indent = is_equal_indent || comment.start_column_fast(context.program) <= last_node.start_column_fast(context.program);
4840+
is_equal_indent = is_equal_indent || comment.start_column_fast(context.program) <= last_node_column;
48404841
let generated_comment = gen_comment_based_on_last_node(
48414842
comment,
4842-
&Some(last_node),
4843+
&Some(last_range),
48434844
GenCommentBasedOnLastNodeOptions { separate_with_newlines: true },
48444845
context,
48454846
);
@@ -4849,7 +4850,7 @@ fn gen_switch_case<'a>(node: &'a SwitchCase, context: &mut Context<'a>) -> Print
48494850
} else {
48504851
ir_helpers::with_indent(generated_comment)
48514852
});
4852-
last_node = comment.range();
4853+
last_range = comment.range();
48534854
}
48544855
}
48554856
items

tests/specs/issues/issue0409.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
== should format ==
2+
switch (x) {
3+
case SyntaxKind.A:
4+
if (condition) {
5+
return 1234;
6+
}
7+
// Something something something
8+
// Something else
9+
// falls-through
10+
11+
case SyntaxKind.B:
12+
return "cool"
13+
}
14+
15+
[expect]
16+
switch (x) {
17+
case SyntaxKind.A:
18+
if (condition) {
19+
return 1234;
20+
}
21+
// Something something something
22+
// Something else
23+
// falls-through
24+
25+
case SyntaxKind.B:
26+
return "cool";
27+
}

0 commit comments

Comments
 (0)