Skip to content

Commit e0f1ba2

Browse files
author
José Valim
committed
Do not escape quoted keywords on formatting, closes #7451
Signed-off-by: José Valim <[email protected]>
1 parent 45c7f82 commit e0f1ba2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,16 @@ defmodule Code.Formatter do
554554
{left, state} =
555555
case left_arg do
556556
{:__block__, _, [atom]} when is_atom(atom) ->
557-
{atom |> Code.Identifier.inspect_as_key() |> string(), state}
557+
key =
558+
case Code.Identifier.classify(atom) do
559+
type when type in [:callable_local, :callable_operator, :not_callable] ->
560+
IO.iodata_to_binary([Atom.to_string(atom), ?:])
561+
562+
_ ->
563+
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
564+
end
565+
566+
{string(key), state}
558567

559568
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} ->
560569
interpolation_to_algebra(entries, @double_quote, state, "\"", "\":")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ defmodule Code.Formatter.ContainersTest do
200200
test "with quoted keyword lists" do
201201
assert_same ~S(["with spaces": 1])
202202
assert_same ~S(["one #{two} three": 1])
203+
assert_same ~S(["\w": 1, "\\w": 2])
204+
assert_same ~S(["Elixir.Foo": 1, "Elixir.Bar": 2])
203205
assert_format ~S(["Foo": 1, "Bar": 2]), ~S([Foo: 1, Bar: 2])
204206
end
205207

0 commit comments

Comments
 (0)