Skip to content

Commit 486b79f

Browse files
authored
Fix comment placement at the end of lambda bodies (#846)
* Fixtures * Fix * do/end fixtures
1 parent 5ab47fc commit 486b79f

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
->(local) {
2+
cached_connections = applications.map {|x| x}
3+
return cached_connections.size == applications.size
4+
# for now assume cache miss
5+
}
6+
7+
-> {
8+
y = x + 1
9+
y * 2
10+
# trailing comment
11+
}
12+
13+
->(local) do
14+
cached_connections = applications.map {|x| x}
15+
return cached_connections.size == applications.size
16+
# for now assume cache miss
17+
end
18+
19+
-> do
20+
y = x + 1
21+
y * 2
22+
# trailing comment
23+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-> (local) {
2+
cached_connections = applications.map { |x| x }
3+
return cached_connections.size == applications.size
4+
# for now assume cache miss
5+
}
6+
7+
-> {
8+
y = x + 1
9+
y * 2
10+
# trailing comment
11+
}
12+
13+
-> (local) do
14+
cached_connections = applications.map { |x| x }
15+
return cached_connections.size == applications.size
16+
# for now assume cache miss
17+
end
18+
19+
-> do
20+
y = x + 1
21+
y * 2
22+
# trailing comment
23+
end

librubyfmt/src/format_prism.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,25 +4284,18 @@ fn format_lambda_node<'src>(ps: &mut ParserState<'src>, lambda_node: prism::Lamb
42844284
ps.emit_space();
42854285
ps.inline_breakable_of(BreakableDelims::for_brace_block(), |ps| {
42864286
if let Some(body) = lambda_node.body() {
4287-
let has_multiple_statements = body
4288-
.as_statements_node()
4289-
.map(|statements_node| statements_node.body().len() > 1)
4290-
.unwrap_or(false);
4291-
if has_multiple_statements {
4292-
ps.emit_soft_newline();
4293-
ps.with_start_of_line(true, |ps| {
4294-
format_node(ps, body);
4295-
});
4296-
} else {
4297-
ps.with_start_of_line(false, |ps| {
4298-
if let Some(node) = body.as_statements_node().unwrap().body().first() {
4299-
ps.emit_soft_newline();
4287+
ps.with_start_of_line(false, |ps| {
4288+
let statements = body.as_statements_node().unwrap().body();
4289+
if !statements.is_empty() {
4290+
ps.emit_soft_newline();
4291+
for node in statements.iter() {
43004292
ps.emit_soft_indent();
43014293
format_node(ps, node);
43024294
ps.emit_soft_newline();
43034295
}
4304-
});
4305-
}
4296+
ps.shift_comments();
4297+
}
4298+
});
43064299
} else if ps.has_comment_in_offset_span(
43074300
lambda_node.opening_loc().start_offset(),
43084301
lambda_node.closing_loc().end_offset(),

0 commit comments

Comments
 (0)