Skip to content

Commit

Permalink
Fix formatter for parenthesized arg after space (#11972)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Jun 9, 2022
1 parent 60e3364 commit 7fabd1b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ describe Crystal::Formatter do
assert_format "foo (1)", "foo(1)"
assert_format "foo (1), 2"
assert_format "foo (1; 2)"
assert_format "foo ((1) ? 2 : 3)", "foo((1) ? 2 : 3)"
assert_format "foo((1..3))"
assert_format "def foo(\n\n#foo\nx,\n\n#bar\nz\n)\nend", "def foo(\n # foo\n x,\n\n # bar\n z\n)\nend"
assert_format "def foo(\nx, #foo\nz #bar\n)\nend", "def foo(\n x, # foo\n z # bar\n)\nend"
assert_format "a = 1;;; b = 2", "a = 1; b = 2"
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2664,10 +2664,12 @@ module Crystal
# we skip whitespace between the method name and the arg. The parenthesized
# arg is transformed into a call with parenthesis: `foo (a)` becomes `foo(a)`.
if node.args.size == 1 &&
@token.type.space? &&
!node.named_args && !node.block_arg && !node.block &&
(expressions = node.args[0].as?(Expressions)) &&
expressions.keyword.paren? && expressions.expressions.size == 1
skip_space
node.args[0] = expressions.expressions[0]
end

if @token.type.op_lparen?
Expand Down

0 comments on commit 7fabd1b

Please sign in to comment.