Skip to content

Commit

Permalink
Formatter: add (&) to param-less yielding defs before comment line (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Mar 2, 2023
1 parent 66372ec commit 13a3d84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,18 @@ describe Crystal::Formatter do
end
CRYSTAL

# #13091
assert_format <<-CRYSTAL,
def foo # bar
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&) # bar
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
def foo(x)
yield
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,13 @@ module Crystal

indent do
next_token

# this formats `def foo # ...` to `def foo(&) # ...` for yielding
# methods before consuming the comment line
if node.block_arity && node.args.empty? && !node.block_arg && !node.double_splat
write "(&)"
end

skip_space consume_newline: false
next_token_skip_space if @token.type.op_eq?
end
Expand Down Expand Up @@ -1517,7 +1524,6 @@ module Crystal
def format_def_args(args : Array, block_arg, splat_index, variadic, double_splat, yields)
# If there are no args, remove extra "()"
if args.empty? && !block_arg && !double_splat && !variadic
write "(&)" if yields
if @token.type.op_lparen?
next_token_skip_space_or_newline
check :OP_RPAREN
Expand Down

0 comments on commit 13a3d84

Please sign in to comment.