Skip to content

Commit 418c277

Browse files
cybroxJosé Valim
authored andcommitted
Fix formatter removing nested parens in call on call (#9211)
1 parent 580bd76 commit 418c277

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ defmodule Code.Formatter do
10751075
defp local_to_algebra(fun, meta, args, context, state) when is_atom(fun) do
10761076
skip_parens =
10771077
cond do
1078-
not Keyword.get(meta, :no_parens, false) -> :required
1078+
not Keyword.get(meta, :no_parens, false) -> :skip_if_only_do_end
10791079
local_without_parens?(fun, args, state) -> :skip_unless_many_args
10801080
true -> :skip_if_do_end
10811081
end
@@ -1093,9 +1093,10 @@ defmodule Code.Formatter do
10931093
{doc, state}
10941094
end
10951095

1096-
# parens may one of:
1096+
# parens may be one of:
10971097
#
10981098
# * :skip_unless_many_args - skips parens unless we are the argument context
1099+
# * :skip_if_only_do_end - skip parens if we are do-end and the only arg
10991100
# * :skip_if_do_end - skip parens if we are do-end
11001101
# * :required - never skip parens
11011102
#
@@ -1111,11 +1112,16 @@ defmodule Code.Formatter do
11111112

11121113
if blocks = do_end_blocks(last, state) do
11131114
{call_doc, state} =
1114-
if rest == [] do
1115-
{" do", state}
1116-
else
1117-
no_parens? = parens != :required
1118-
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
1115+
case rest do
1116+
[] when parens == :required ->
1117+
{"() do", state}
1118+
1119+
[] ->
1120+
{" do", state}
1121+
1122+
_ ->
1123+
no_parens? = parens not in [:required, :skip_if_only_do_end]
1124+
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
11191125
end
11201126

11211127
{blocks_doc, state} = do_end_blocks_to_algebra(blocks, state)

lib/elixir/test/elixir/code_formatter/calls_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ defmodule Code.Formatter.CallsTest do
449449
assert_same "unquote(call)()"
450450
assert_same "unquote(call)(one, two)"
451451

452+
assert_same """
453+
unquote(call)() do
454+
:ok
455+
end
456+
"""
457+
452458
assert_same """
453459
unquote(call)(one, two) do
454460
:ok
@@ -668,6 +674,11 @@ defmodule Code.Formatter.CallsTest do
668674
assert_same "foo.bar(call)()"
669675
assert_same "foo.bar(call)(one, two)"
670676

677+
assert_same """
678+
foo.bar(call)() do
679+
end
680+
"""
681+
671682
assert_same """
672683
foo.bar(call)(one, two) do
673684
:ok

0 commit comments

Comments
 (0)