Skip to content

Commit 1cf8108

Browse files
committed
fix comment regression
1 parent 835af2f commit 1cf8108

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

src/generation/generate.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7918,6 +7918,7 @@ fn gen_statements<'a>(inner_range: SourceRange, stmts: Vec<Node<'a>>, context: &
79187918
let trailing_comment_range = context
79197919
.token_finder
79207920
.get_first_semi_colon_after(last_node)
7921+
.filter(|token| token.start() < inner_range.end())
79217922
.map(|token| token.range())
79227923
.unwrap_or_else(|| last_node.range());
79237924
items.extend(gen_trailing_comments_as_statements(&trailing_comment_range, context));
@@ -9388,6 +9389,7 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
93889389
let body_should_be_multi_line = get_body_should_be_multi_line(opts.body_node, &header_trailing_comments, context);
93899390
let should_use_new_line = get_should_use_new_line(opts.body_node, body_should_be_multi_line, &opts.single_body_position, context);
93909391
let open_brace_token = get_open_brace_token(opts.body_node, context);
9392+
let close_brace_token = get_close_brace_token(opts.body_node, context);
93919393
let use_braces = opts.use_braces;
93929394
let is_body_empty_stmt = matches!(opts.body_node, Node::EmptyStatement(_));
93939395
let mut inner_brace_space_condition = if_true(
@@ -9532,7 +9534,10 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
95329534
// generate the remaining trailing comments inside because some of them are generated already
95339535
// by parsing the header trailing comments
95349536
items.extend(gen_leading_comments(&body_node.range(), context));
9535-
let inner_range = body_node.get_inner_range(context);
9537+
let inner_range = match (open_brace_token, close_brace_token) {
9538+
(Some(open_brace_token), Some(close_brace_token)) => SourceRange::new(open_brace_token.end(), close_brace_token.start()),
9539+
_ => body_node.get_inner_range(context),
9540+
};
95369541
if body_node.body.is_empty() {
95379542
let trailing_comments_same_line = get_trailing_comments_on_same_line(&inner_range.start().range(), context);
95389543
items.extend(gen_comments_same_line(trailing_comments_same_line, context));
@@ -9732,6 +9737,14 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
97329737
None
97339738
}
97349739
}
9740+
9741+
fn get_close_brace_token<'a>(body_node: Node<'a>, context: &mut Context<'a>) -> Option<&'a Token> {
9742+
if let Node::BlockStatement(block_stmt) = body_node {
9743+
context.token_finder.get_last_close_brace_token_within(block_stmt)
9744+
} else {
9745+
None
9746+
}
9747+
}
97359748
}
97369749

97379750
struct GenJsxWithOpeningAndClosingOptions<'a> {

src/generation/tokens.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ impl<'a> TokenFinder<'a> {
6767
self.get_first_token_within(node, |token| token.kind() == Kind::LCurly)
6868
}
6969

70+
pub fn get_last_close_brace_token_within(&self, node: &impl SourceRanged) -> Option<&'a Token> {
71+
self.get_last_token_within_if(node, |token| token.kind() == Kind::RCurly)
72+
}
73+
7074
pub fn get_last_token_within_if_comma(&self, node: &impl SourceRanged) -> Option<&'a Token> {
7175
self.get_last_token_within_if(node, |token| token.kind() == Kind::Comma)
7276
}

tests/specs/statements/ifStatement/IfStatement_Comments.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,37 @@ if (true) { /*0*/
2020
if (true) {
2121
/*0*/
2222
}
23+
24+
== should keep line comments before the close brace ==
25+
if (true) {
26+
call();
27+
// process already exited
28+
}
29+
30+
[expect]
31+
if (true) {
32+
call();
33+
// process already exited
34+
}
35+
36+
== should keep line comments before the close brace before else if ==
37+
if (runtime.mode === "server") {
38+
return {};
39+
// If the mode is static, set if the cwd is specified, and optionally spa if specified and valid
40+
} else if (
41+
runtime.mode === "static"
42+
|| runtime.cwd === "string"
43+
) {
44+
return {};
45+
}
46+
47+
[expect]
48+
if (runtime.mode === "server") {
49+
return {};
50+
// If the mode is static, set if the cwd is specified, and optionally spa if specified and valid
51+
} else if (
52+
runtime.mode === "static"
53+
|| runtime.cwd === "string"
54+
) {
55+
return {};
56+
}

tests/specs/statements/tryStatement/TryStatement_All.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,21 @@ try {
7171
} catch {
7272
} finally {
7373
}
74+
75+
== should keep line comments before close braces ==
76+
try {
77+
await process.status;
78+
// process already exited
79+
} catch {
80+
console.log("Failed to wait");
81+
// process group already exited
82+
}
83+
84+
[expect]
85+
try {
86+
await process.status;
87+
// process already exited
88+
} catch {
89+
console.log("Failed to wait");
90+
// process group already exited
91+
}

0 commit comments

Comments
 (0)