Skip to content

Commit 1be9136

Browse files
thomasklemmclaude
andauthored
[fix] Preserve then delimiter for when clauses with endless ranges (#880)
Ruby treats `..` at end-of-line as a line continuation operator, so `when 0.9..` followed by a newline causes a SyntaxError. Emit `then` after the conditions when the last condition is an endless range. Fixes #878 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d818f52 commit 1be9136

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
case x
2+
when 0.9.. then "very strong"
3+
when 0.7.. then "strong"
4+
when 0.5.. then "moderate"
5+
end
6+
7+
case x
8+
when 1.. then "positive"
9+
end
10+
11+
case x
12+
when ..0 then "non-positive"
13+
end
14+
15+
case x
16+
when 0..10 then "small"
17+
end
18+
19+
case x
20+
when 0.9.., :special then "a"
21+
end
22+
23+
case x
24+
when 0.9.. then
25+
"very strong"
26+
end
27+
28+
case x
29+
when 1... then "exclusive endless"
30+
end
31+
32+
case x
33+
when :special, 0.9.. then "a"
34+
end
35+
36+
case x
37+
when (0.9..) then "parenthesized endless"
38+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
case x
2+
when 0.9.. then
3+
"very strong"
4+
when 0.7.. then
5+
"strong"
6+
when 0.5.. then
7+
"moderate"
8+
end
9+
10+
case x
11+
when 1.. then
12+
"positive"
13+
end
14+
15+
case x
16+
when ..0
17+
"non-positive"
18+
end
19+
20+
case x
21+
when 0..10
22+
"small"
23+
end
24+
25+
case x
26+
when 0.9.., :special
27+
"a"
28+
end
29+
30+
case x
31+
when 0.9.. then
32+
"very strong"
33+
end
34+
35+
case x
36+
when 1... then
37+
"exclusive endless"
38+
end
39+
40+
case x
41+
when :special, 0.9.. then
42+
"a"
43+
end
44+
45+
case x
46+
when (0.9..)
47+
"parenthesized endless"
48+
end

librubyfmt/src/format_prism.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,6 +4862,14 @@ fn format_when_node<'src>(ps: &mut ParserState<'src>, when_node: prism::WhenNode
48624862
});
48634863
});
48644864

4865+
// Ruby treats `..` at end-of-line as a line continuation operator.
4866+
// When the last condition is an endless range, we must emit `then`
4867+
// to prevent a SyntaxError.
4868+
if when_conditions_end_with_endless_range(&when_node) {
4869+
ps.emit_space();
4870+
ps.emit_keyword(b"then");
4871+
}
4872+
48654873
ps.new_block(|ps| {
48664874
ps.with_start_of_line(true, |ps| {
48674875
ps.emit_newline();
@@ -4872,6 +4880,14 @@ fn format_when_node<'src>(ps: &mut ParserState<'src>, when_node: prism::WhenNode
48724880
});
48734881
}
48744882

4883+
fn when_conditions_end_with_endless_range(when_node: &prism::WhenNode) -> bool {
4884+
when_node
4885+
.conditions()
4886+
.last()
4887+
.and_then(|c| c.as_range_node())
4888+
.is_some_and(|r| r.right().is_none())
4889+
}
4890+
48754891
fn format_while_node<'src>(ps: &mut ParserState<'src>, while_node: prism::WhileNode<'src>) {
48764892
format_conditional_node(ps, b"while", true, &Conditional::While(while_node));
48774893
}

0 commit comments

Comments
 (0)