Skip to content

Commit e58e267

Browse files
authored
Formatter keeps quotes in atom keys (#13108)
1 parent 856da79 commit e58e267

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
@@ -521,14 +521,15 @@ defmodule Code.Formatter do
521521
{string(~S{"..//":}), state}
522522

523523
{:__block__, _, [atom]} when is_atom(atom) ->
524-
key =
524+
iodata =
525525
if Macro.classify_atom(atom) in [:identifier, :unquoted] do
526-
IO.iodata_to_binary([Atom.to_string(atom), ?:])
526+
[Atom.to_string(atom), ?:]
527527
else
528-
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
528+
[?", atom |> Atom.to_string() |> String.replace("\"", "\\\""), ?", ?:]
529529
end
530530

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

533534
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} ->
534535
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)