From 881769c91085caa2507e993a570e023e34c33942 Mon Sep 17 00:00:00 2001 From: youdie006 Date: Thu, 13 Aug 2026 10:00:36 +0900 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + lib/rufo/formatter.rb | 3 +++ .../formatter_source_specs/endless_methods.rb.spec | 12 ++++++++++++ 3 files changed, 16 insertions(+) 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