Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bug method calling with anonymous block arg #344

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/rufo/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ def visit_call_at_paren(node, args)
# Check if there's a block arg and if the call ends with hash key/values
if args_node[0] == :args_add_block
_, args, block_arg = args_node
want_trailing_comma = !block_arg
want_trailing_comma = block_arg_type(block_arg) == :no_arg
if args.is_a?(Array) && (last_arg = args.last) && last_arg.is_a?(Array) &&
last_arg[0].is_a?(Symbol) && last_arg[0] != :bare_assoc_hash
want_trailing_comma = false
Expand Down Expand Up @@ -1475,11 +1475,7 @@ def visit_call_args(node)
visit_comma_separated_list args
end

# block_arg will be...
# - named => node
# - anonymous => nil
# - no arg => false
if block_arg || block_arg.nil?
if block_arg_type(block_arg) != :no_arg
skip_space_or_newline

if comma?
Expand Down Expand Up @@ -4207,4 +4203,15 @@ def node_line(node, beginning: true)
node[2][0]
end
end

def block_arg_type(node)
case node
when nil
:anonymous
when false
:no_arg
else
node
end
end
end
17 changes: 17 additions & 0 deletions spec/lib/rufo/formatter_source_specs/3.1/method_definition.rb.spec
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ end
def foo(a: 1,
&)
end

#~# ORIGINAL issue_332

def foo(&)
hoge(
**a,
&
)
end

#~# EXPECTED
def foo(&)
hoge(
**a,
&
)
end