diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c264a1..a97c0c2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Fixed +- Fix crash when formatting an endless method with empty parentheses (`def foo() = 1`). ([#362](https://github.com/ruby-formatter/rufo/pull/362)) ### Changed diff --git a/lib/rufo/formatter.rb b/lib/rufo/formatter.rb index d37d2d5b..04c0d581 100644 --- a/lib/rufo/formatter.rb +++ b/lib/rufo/formatter.rb @@ -2015,6 +2015,9 @@ def visit_def_from_name(name, params, body) check :on_rparen next_token write "()" + # Consume the space after `)` like the other branches do, so an endless + # method body (`def foo() = 1`) is detected below instead of crashing. + skip_space else write "(" diff --git a/spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec b/spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec index e914f3b1..64ff09eb 100644 --- a/spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec @@ -15,3 +15,15 @@ def foo = puts( "a") #~# EXPECTED def foo = puts("a") + +#~# ORIGINAL format_endless_method_with_empty_params +def foo() = 1 + +#~# EXPECTED +def foo() = 1 + +#~# ORIGINAL format_endless_method_with_empty_params_and_spacing +def foo() = 1 + +#~# EXPECTED +def foo() = 1