Skip to content

Commit 233fc09

Browse files
doorganjosevalim
authored andcommitted
Fix formatting of lists in module attribues (#11462)
1 parent 31e24b3 commit 233fc09

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/elixir/lib/code/normalizer.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,15 @@ defmodule Code.Normalizer do
200200
# Module attributes
201201
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
202202
value =
203-
if is_list(value) do
204-
normalize_kw_args(value, state, false)
205-
else
206-
do_normalize(value, state)
203+
cond do
204+
keyword?(value) ->
205+
normalize_kw_args(value, state, true)
206+
207+
is_list(value) ->
208+
normalize_literal(value, meta, state)
209+
210+
true ->
211+
do_normalize(value, state)
207212
end
208213

209214
{:@, meta, [{name, name_meta, [value]}]}

lib/elixir/test/elixir/code_normalizer/formatted_ast_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ defmodule Code.Normalizer.FormatterASTTest do
164164
end
165165
end
166166

167+
describe "lists" do
168+
test "on module attribute" do
169+
assert_same ~S"@foo [1]"
170+
end
171+
end
172+
167173
describe "charlists" do
168174
test "without escapes" do
169175
assert_same ~S['']

lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,26 @@ defmodule Code.Normalizer.QuotedASTTest do
542542
assert quoted_to_string(quote(do: foo(catch: a))) == "foo(catch: a)"
543543
assert quoted_to_string(quote(do: foo |> [bar: :baz])) == "foo |> [bar: :baz]"
544544
end
545+
546+
test "list in module attribute" do
547+
assert quoted_to_string(
548+
quote do
549+
@foo [1]
550+
end
551+
) == "@foo [1]"
552+
553+
assert quoted_to_string(
554+
quote do
555+
@foo [foo: :bar]
556+
end
557+
) == "@foo foo: :bar"
558+
559+
assert quoted_to_string(
560+
quote do
561+
@foo [1, foo: :bar]
562+
end
563+
) == "@foo [1, foo: :bar]"
564+
end
545565
end
546566

547567
describe "quoted_to_algebra/2 escapes" do

0 commit comments

Comments
 (0)