Skip to content

Commit

Permalink
fix: fix bug method calling with anonymous block arg
Browse files Browse the repository at this point in the history
ref: #332
  • Loading branch information
kzkn committed Feb 11, 2025
1 parent 895e638 commit 198ba68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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

0 comments on commit 198ba68

Please sign in to comment.