Skip to content

Fix crash on an endless method with empty parentheses (#361) - #362

Open
youdie006 wants to merge 1 commit into
ruby-formatter:masterfrom
youdie006:fix/361-endless-empty-params
Open

Fix crash on an endless method with empty parentheses (#361)#362
youdie006 wants to merge 1 commit into
ruby-formatter:masterfrom
youdie006:fix/361-endless-empty-params

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #361.

Problem

Formatting a shorthand (endless) method definition with empty parentheses crashes:

def foo() = 1
Rufo::Bug: Expected token on_int, not on_sp

def foo(x) = 1, def foo = 1, and non-endless def 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-parens elsif), does not skip_space after consuming the closing parenthesis. So the endless-method check format_endless_method if current_token_kind == :on_op sees the space (:on_sp) before = instead of the = operator, skips format_endless_method, and visit body then hits the unconsumed = 1, raising Rufo::Bug.

Fix

Add skip_space after write "()", mirroring the sibling branches.

Test

Added two cases to spec/lib/rufo/formatter_source_specs/endless_methods.rb.spec (def foo() = 1 idempotent, and def foo() = 1 normalized to def foo() = 1). Red-green verified with rspec: both raise Rufo::Bug before the fix and format correctly after; the full formatter spec (977 examples) passes and rubocop is clean.


Disclosure: prepared with AI assistance (Claude); I reviewed it and verified the red-green spec and the full formatter suite.

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
youdie006 force-pushed the fix/361-endless-empty-params branch from 6aa3ebc to 881769c Compare August 13, 2026 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shorthand method definition with parenthesis without an argument will result in You've found a bug!

1 participant