Skip to content

Commit 6694ddb

Browse files
author
José Valim
committed
Properly add parens around when with keywords as argument
1 parent 85a4d55 commit 6694ddb

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,21 @@ defmodule Code.Formatter do
716716
{concat(op_string, doc), @empty, newlines, state}
717717
end
718718

719-
operand_to_algebra_with_comments(
720-
operands,
721-
meta,
722-
min_line,
723-
max_line,
724-
state,
725-
operand_to_algebra
726-
)
719+
{doc, state} =
720+
operand_to_algebra_with_comments(
721+
operands,
722+
meta,
723+
min_line,
724+
max_line,
725+
state,
726+
operand_to_algebra
727+
)
728+
729+
if keyword?(right_arg) and context in [:parens_arg, :no_parens_arg] do
730+
{wrap_in_parens(doc), state}
731+
else
732+
{doc, state}
733+
end
727734
end
728735

729736
defp binary_op_to_algebra(op, _, meta, left_arg, right_arg, context, state, _nesting)

lib/elixir/test/elixir/code_formatter/integration_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ defmodule Code.Formatter.IntegrationTest do
447447
"""
448448
end
449449

450+
test "when with keywords inside call" do
451+
assert_same """
452+
quote((bar(foo(1)) when bat: foo(1)), [])
453+
"""
454+
455+
assert_same """
456+
quote(do: (bar(foo(1)) when bat: foo(1)), line: 1)
457+
"""
458+
459+
assert_same """
460+
typespec(quote(do: (bar(foo(1)) when bat: foo(1))), [foo: 1], [])
461+
"""
462+
end
463+
450464
test "capture with operators" do
451465
assert_same """
452466
"this works" |> (&String.upcase/1) |> (&String.downcase/1)

0 commit comments

Comments
 (0)