Skip to content

Commit 0a9ee79

Browse files
sabiwarajosevalim
authored andcommitted
Formatter keeps quotes in atom keys (#13108)
1 parent 58b8b93 commit 0a9ee79

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,15 @@ defmodule Code.Formatter do
520520
{string(~S{"..//":}), state}
521521

522522
{:__block__, _, [atom]} when is_atom(atom) ->
523-
key =
523+
iodata =
524524
if Macro.classify_atom(atom) in [:identifier, :unquoted] do
525-
IO.iodata_to_binary([Atom.to_string(atom), ?:])
525+
[Atom.to_string(atom), ?:]
526526
else
527-
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
527+
[?", atom |> Atom.to_string() |> String.replace("\"", "\\\""), ?", ?:]
528528
end
529529

530-
{string(key) |> color(:atom, state.inspect_opts), state}
530+
{iodata |> IO.iodata_to_binary() |> string() |> color(:atom, state.inspect_opts),
531+
state}
531532

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ defmodule Code.Formatter.ContainersTest do
203203
assert_same ~S(["\w": 1, "\\w": 2])
204204
assert_same ~S(["Elixir.Foo": 1, "Elixir.Bar": 2])
205205
assert_format ~S(["Foo": 1, "Bar": 2]), ~S([Foo: 1, Bar: 2])
206+
assert_same ~S(["with \"scare quotes\"": 1])
206207
end
207208

208209
test "with operators keyword lists" do

0 commit comments

Comments
 (0)