Fix crash on an endless method with empty parentheses (#361) - #362
Open
youdie006 wants to merge 1 commit into
Open
Fix crash on an endless method with empty parentheses (#361)#362youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
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 ruby-formatter#361
youdie006
force-pushed
the
fix/361-endless-empty-params
branch
from
August 13, 2026 01:01
6aa3ebc to
881769c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #361.
Problem
Formatting a shorthand (endless) method definition with empty parentheses crashes:
def foo(x) = 1,def foo = 1, and non-endlessdef foo()all format fine — it is specific to the empty-parens-plus-endless combination.Cause
In
visit_def_from_name, the empty-parameters branch writes"()"but, unlike the two sibling branches (non-empty parens and the no-parenselsif), does notskip_spaceafter consuming the closing parenthesis. So the endless-method checkformat_endless_method if current_token_kind == :on_opsees the space (:on_sp) before=instead of the=operator, skipsformat_endless_method, andvisit bodythen hits the unconsumed= 1, raisingRufo::Bug.Fix
Add
skip_spaceafterwrite "()", mirroring the sibling branches.Test
Added two cases to
spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec(def foo() = 1idempotent, anddef foo() = 1normalized todef foo() = 1). Red-green verified withrspec: both raiseRufo::Bugbefore the fix and format correctly after; the full formatter spec (977 examples) passes andrubocopis clean.Disclosure: prepared with AI assistance (Claude); I reviewed it and verified the red-green spec and the full formatter suite.