Skip to content

Commit 869afba

Browse files
committed
Format |> var.fun() calls as well
1 parent 9ca0b56 commit 869afba

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/elixir/lib/code.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,11 @@ defmodule Code do
697697
Defaults to the value of the `:migrate` option. This option changes the AST.
698698
699699
* `:migrate_call_parens_on_pipe` (since v1.19.0) - when `true`,
700-
formats local calls on the right-hand side of the pipe operator to always
701-
include parentheses, for example `foo |> bar` becomes `foo |> bar()`.
702-
Parentheses are always added for qualified calls like `foo |> Bar.bar`
703-
disregarding of this option.
700+
formats calls on the right-hand side of the pipe operator to always include
701+
parentheses, for example `foo |> bar` becomes `foo |> bar()` and
702+
`foo |> mod.fun` becomes `foo |> mod.fun()`.
703+
Parentheses are always added for qualified calls like `foo |> Bar.bar` even
704+
when this option is `false`.
704705
Defaults to the value of the `:migrate` option. This option changes the AST.
705706
706707
* `:migrate_charlists_as_sigils` (since v1.18.0) - when `true`,

lib/elixir/lib/code/formatter.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,20 +843,38 @@ defmodule Code.Formatter do
843843
{wrap_in_parens(doc), state}
844844
end
845845

846+
# |> var
847+
# |> var()
846848
defp binary_operand_to_algebra(
847-
{var, meta, atom},
849+
{var, meta, var_context},
848850
context,
849851
%{migrate_call_parens_on_pipe: true} = state,
850852
:|>,
851853
_parent_info,
852854
:right,
853855
_nesting
854856
)
855-
when is_atom(var) and is_atom(atom) do
857+
when is_atom(var) and is_atom(var_context) do
856858
operand = {var, meta, []}
857859
quoted_to_algebra(operand, context, state)
858860
end
859861

862+
# |> var.fun
863+
# |> var.fun()
864+
defp binary_operand_to_algebra(
865+
{{:., _, [_, fun]} = call, meta, []},
866+
context,
867+
%{migrate_call_parens_on_pipe: true} = state,
868+
:|>,
869+
_parent_info,
870+
:right,
871+
_nesting
872+
)
873+
when is_atom(fun) do
874+
meta = Keyword.put_new_lazy(meta, :closing, fn -> [line: meta[:line]] end)
875+
quoted_to_algebra({call, meta, []}, context, state)
876+
end
877+
860878
defp binary_operand_to_algebra(operand, context, state, parent_op, parent_info, side, nesting) do
861879
{parent_assoc, parent_prec} = parent_info
862880

lib/elixir/test/elixir/code_formatter/migration_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ defmodule Code.Formatter.MigrationTest do
3333
test "enables :migrate_call_parens_on_pipe" do
3434
assert_format "x |> y", "x |> y()", @opts
3535
assert_format "x |> y |> z", "x |> y() |> z()", @opts
36+
assert_format "x |> y.z", "x |> y.z()", @opts
37+
assert_format "x |> y.z.t", "x |> y.z.t()", @opts
3638
end
3739

3840
test "does nothing within defmacro" do
@@ -42,6 +44,7 @@ defmodule Code.Formatter.MigrationTest do
4244
test "does nothing without the migrate_unless option" do
4345
assert_same "x |> y"
4446
assert_same "x |> y |> z"
47+
assert_same "x |> y.z"
4548
end
4649
end
4750

0 commit comments

Comments
 (0)