Skip to content

Commit fcb7b69

Browse files
sabiwarajosevalim
authored andcommitted
Fix formatter: size*unit shortcut in bitstring (#12201)
1 parent 32c2a2d commit fcb7b69

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ defmodule Code.Formatter do
14111411

14121412
defp bitstring_segment_to_algebra({{:"::", _, [segment, spec]}, i}, state, last) do
14131413
{doc, state} = quoted_to_algebra(segment, :parens_arg, state)
1414-
{spec, state} = bitstring_spec_to_algebra(spec, state)
1414+
{spec, state} = bitstring_spec_to_algebra(spec, state, state.normalize_bitstring_modifiers)
14151415

14161416
spec = wrap_in_parens_if_inspected_atom(spec)
14171417
spec = if i == last, do: bitstring_wrap_parens(spec, i, last), else: spec
@@ -1430,26 +1430,29 @@ defmodule Code.Formatter do
14301430
{bitstring_wrap_parens(doc, i, last), state}
14311431
end
14321432

1433-
defp bitstring_spec_to_algebra({op, _, [left, right]}, state) when op in [:-, :*] do
1434-
{left, state} = bitstring_spec_to_algebra(left, state)
1435-
{right, state} = bitstring_spec_element_to_algebra(right, state)
1433+
defp bitstring_spec_to_algebra({op, _, [left, right]}, state, normalize_modifiers)
1434+
when op in [:-, :*] do
1435+
normalize_modifiers = normalize_modifiers && op != :*
1436+
{left, state} = bitstring_spec_to_algebra(left, state, normalize_modifiers)
1437+
{right, state} = bitstring_spec_element_to_algebra(right, state, normalize_modifiers)
14361438
{concat(concat(left, Atom.to_string(op)), right), state}
14371439
end
14381440

1439-
defp bitstring_spec_to_algebra(spec, state) do
1440-
bitstring_spec_element_to_algebra(spec, state)
1441+
defp bitstring_spec_to_algebra(spec, state, normalize_modifiers) do
1442+
bitstring_spec_element_to_algebra(spec, state, normalize_modifiers)
14411443
end
14421444

14431445
defp bitstring_spec_element_to_algebra(
14441446
{atom, meta, empty_args},
1445-
state = %{normalize_bitstring_modifiers: true}
1447+
state,
1448+
_normalize_modifiers = true
14461449
)
14471450
when is_atom(atom) and empty_args in [nil, []] do
14481451
empty_args = bitstring_spec_normalize_empty_args(atom)
14491452
quoted_to_algebra_with_parens_if_operator({atom, meta, empty_args}, :parens_arg, state)
14501453
end
14511454

1452-
defp bitstring_spec_element_to_algebra(spec_element, state) do
1455+
defp bitstring_spec_element_to_algebra(spec_element, state, _normalize_modifiers) do
14531456
quoted_to_algebra_with_parens_if_operator(spec_element, :parens_arg, state)
14541457
end
14551458

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ defmodule Code.Formatter.ContainersTest do
299299

300300
assert_format "<<x::binary()-(13 * 6)-custom>>", "<<x::binary-(13 * 6)-custom()>>"
301301
assert_same "<<x::binary-(13 * 6)-custom()>>"
302+
assert_same "<<0::size*unit, bytes::binary>>"
303+
assert_format "<<0::size*unit, bytes::custom>>", "<<0::size*unit, bytes::custom()>>"
302304

303305
assert_format "<<0, 1::2-integer() <- x>>", "<<0, 1::2-integer <- x>>"
304306
assert_same "<<0, 1::2-integer <- x>>"

0 commit comments

Comments
 (0)