Skip to content

Commit 6aa3ebc

Browse files
committed
Fix crash on an endless method with empty parentheses
Formatting "def foo() = 1" raised Rufo::Bug. The empty-parameters branch of visit_def_from_name wrote "()" but, unlike the other two branches, did not skip the space after the closing parenthesis. The endless-method check (current_token_kind == :on_op) therefore saw the space instead of "=", skipped format_endless_method, and visiting the body then hit the unconsumed " = 1". Skip the space after "()" like the sibling branches do. Fixes #361
1 parent 5186ef6 commit 6aa3ebc

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/rufo/formatter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,9 @@ def visit_def_from_name(name, params, body)
20152015
check :on_rparen
20162016
next_token
20172017
write "()"
2018+
# Consume the space after `)` like the other branches do, so an endless
2019+
# method body (`def foo() = 1`) is detected below instead of crashing.
2020+
skip_space
20182021
else
20192022
write "("
20202023

spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ def foo = puts( "a")
1515

1616
#~# EXPECTED
1717
def foo = puts("a")
18+
19+
#~# ORIGINAL format_endless_method_with_empty_params
20+
def foo() = 1
21+
22+
#~# EXPECTED
23+
def foo() = 1
24+
25+
#~# ORIGINAL format_endless_method_with_empty_params_and_spacing
26+
def foo() = 1
27+
28+
#~# EXPECTED
29+
def foo() = 1

0 commit comments

Comments
 (0)