Skip to content

Commit b1a1dd0

Browse files
sabiwarajosevalim
authored andcommitted
Fix formatter for :* in bitstring modifiers (#12923)
1 parent e4e2f1a commit b1a1dd0

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,9 @@ defmodule Code.Formatter do
14191419

14201420
defp bitstring_segment_to_algebra({{:"::", _, [segment, spec]}, i}, state, last) do
14211421
{doc, state} = quoted_to_algebra(segment, :parens_arg, state)
1422-
{spec, state} = bitstring_spec_to_algebra(spec, state, state.normalize_bitstring_modifiers)
1422+
1423+
{spec, state} =
1424+
bitstring_spec_to_algebra(spec, state, state.normalize_bitstring_modifiers, :"::")
14231425

14241426
spec = wrap_in_parens_if_inspected_atom(spec)
14251427
spec = if i == last, do: bitstring_wrap_parens(spec, i, last), else: spec
@@ -1438,15 +1440,17 @@ defmodule Code.Formatter do
14381440
{bitstring_wrap_parens(doc, i, last), state}
14391441
end
14401442

1441-
defp bitstring_spec_to_algebra({op, _, [left, right]}, state, normalize_modifiers)
1443+
defp bitstring_spec_to_algebra({op, _, [left, right]}, state, normalize_modifiers, paren_op)
14421444
when op in [:-, :*] do
14431445
normalize_modifiers = normalize_modifiers && op != :*
1444-
{left, state} = bitstring_spec_to_algebra(left, state, normalize_modifiers)
1446+
{left, state} = bitstring_spec_to_algebra(left, state, normalize_modifiers, op)
14451447
{right, state} = bitstring_spec_element_to_algebra(right, state, normalize_modifiers)
1446-
{concat(concat(left, Atom.to_string(op)), right), state}
1448+
doc = concat(concat(left, Atom.to_string(op)), right)
1449+
doc = if paren_op == :*, do: wrap_in_parens(doc), else: doc
1450+
{doc, state}
14471451
end
14481452

1449-
defp bitstring_spec_to_algebra(spec, state, normalize_modifiers) do
1453+
defp bitstring_spec_to_algebra(spec, state, normalize_modifiers, _paren_op) do
14501454
bitstring_spec_element_to_algebra(spec, state, normalize_modifiers)
14511455
end
14521456

lib/elixir/test/elixir/code_formatter/containers_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ defmodule Code.Formatter.ContainersTest do
275275
assert_format "<< 1 :: 2 - unit(3) >>", "<<1::2-unit(3)>>"
276276
assert_format "<< 1 :: 2 * 3 - unit(4) >>", "<<1::2*3-unit(4)>>"
277277
assert_format "<< 1 :: 2 - unit(3) - 4 / 5 >>", "<<1::2-unit(3)-(4 / 5)>>"
278+
assert_format "<<0 :: ( x - 1 ) * 5>>", "<<0::(x-1)*5>>"
279+
assert_format "<<0 :: 2 * 3 * 4>>", "<<0::(2*3)*4>>"
278280
end
279281

280282
test "in comprehensions" do

0 commit comments

Comments
 (0)