Skip to content

Commit 198ba68

Browse files
committed
fix: fix bug method calling with anonymous block arg
ref: #332
1 parent 895e638 commit 198ba68

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/rufo/formatter.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def visit_call_at_paren(node, args)
10831083
# Check if there's a block arg and if the call ends with hash key/values
10841084
if args_node[0] == :args_add_block
10851085
_, args, block_arg = args_node
1086-
want_trailing_comma = !block_arg
1086+
want_trailing_comma = block_arg_type(block_arg) == :no_arg
10871087
if args.is_a?(Array) && (last_arg = args.last) && last_arg.is_a?(Array) &&
10881088
last_arg[0].is_a?(Symbol) && last_arg[0] != :bare_assoc_hash
10891089
want_trailing_comma = false
@@ -1475,11 +1475,7 @@ def visit_call_args(node)
14751475
visit_comma_separated_list args
14761476
end
14771477

1478-
# block_arg will be...
1479-
# - named => node
1480-
# - anonymous => nil
1481-
# - no arg => false
1482-
if block_arg || block_arg.nil?
1478+
if block_arg_type(block_arg) != :no_arg
14831479
skip_space_or_newline
14841480

14851481
if comma?
@@ -4207,4 +4203,15 @@ def node_line(node, beginning: true)
42074203
node[2][0]
42084204
end
42094205
end
4206+
4207+
def block_arg_type(node)
4208+
case node
4209+
when nil
4210+
:anonymous
4211+
when false
4212+
:no_arg
4213+
else
4214+
node
4215+
end
4216+
end
42104217
end

spec/lib/rufo/formatter_source_specs/3.1/method_definition.rb.spec

+17
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,20 @@ end
6161
def foo(a: 1,
6262
&)
6363
end
64+
65+
#~# ORIGINAL issue_332
66+
67+
def foo(&)
68+
hoge(
69+
**a,
70+
&
71+
)
72+
end
73+
74+
#~# EXPECTED
75+
def foo(&)
76+
hoge(
77+
**a,
78+
&
79+
)
80+
end

0 commit comments

Comments
 (0)