Skip to content

Commit e8550bc

Browse files
author
José Valim
committed
Always wrap binary operators in groups
1 parent 9df1f4c commit e8550bc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,11 @@ defmodule Code.Formatter do
647647
doc =
648648
cond do
649649
op in @no_space_binary_operators ->
650-
concat(concat(left, op_string), right)
650+
group(concat(concat(left, op_string), right))
651651

652652
op in @no_newline_binary_operators ->
653653
op_string = " " <> op_string <> " "
654-
concat(concat(left, op_string), right)
654+
group(concat(concat(left, op_string), right))
655655

656656
op in @left_new_line_before_binary_operators ->
657657
op_string = op_string <> " "
@@ -688,7 +688,7 @@ defmodule Code.Formatter do
688688

689689
with_next_break_fits(next_break_fits?, right, fn right ->
690690
op_string = " " <> op_string
691-
concat(left, group(nest(glue(op_string, group(right)), nesting, :break)))
691+
group(concat(left, group(nest(glue(op_string, group(right)), nesting, :break))))
692692
end)
693693
end
694694

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ defmodule Code.Formatter.IntegrationTest do
231231
"""
232232
end
233233

234+
test "comprehensions with when" do
235+
assert_same """
236+
for {key, value} when is_atom(key) <- Map.to_list(map),
237+
key = Atom.to_string(key),
238+
String.starts_with?(key, hint) do
239+
%{kind: :map_key, name: key, value_is_map: is_map(value)}
240+
end
241+
"""
242+
end
243+
234244
test "next break fits followed by inline tuple" do
235245
assert_same """
236246
assert ExUnit.Filters.eval([line: "1"], [:line], %{line: 3, describe_line: 2}, tests) ==

0 commit comments

Comments
 (0)