Skip to content

Formatter keeps quotes in atom keys #13108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/elixir/lib/code/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,15 @@ defmodule Code.Formatter do
{string(~S{"..//":}), state}

{:__block__, _, [atom]} when is_atom(atom) ->
key =
iodata =
if Macro.classify_atom(atom) in [:identifier, :unquoted] do
IO.iodata_to_binary([Atom.to_string(atom), ?:])
[Atom.to_string(atom), ?:]
else
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
[?", atom |> Atom.to_string() |> String.replace("\"", "\\\""), ?", ?:]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Macro.inspect_atom(:key, atom). This way we can even skip the Macro.classify_atom call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a nice one, totally forgot about inspect_atom!

But it seems to be over-escaping backslashes for our usage:
Screenshot 2023-11-15 at 20 55 05

Will try to figure out if we can remove the double escape.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also noticed that this code here is almost the same, there's probably a way to refactor it (maybe with Macro.inspect_atom/2)

Copy link
Member

@josevalim josevalim Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it is because strings in the formatter are already unescaped. My bad. Your PR is good to go!

end

{string(key) |> color(:atom, state.inspect_opts), state}
{iodata |> IO.iodata_to_binary() |> string() |> color(:atom, state.inspect_opts),
state}

{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} ->
interpolation_to_algebra(entries, @double_quote, state, "\"", "\":")
Expand Down
1 change: 1 addition & 0 deletions lib/elixir/test/elixir/code_formatter/containers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ defmodule Code.Formatter.ContainersTest do
assert_same ~S(["\w": 1, "\\w": 2])
assert_same ~S(["Elixir.Foo": 1, "Elixir.Bar": 2])
assert_format ~S(["Foo": 1, "Bar": 2]), ~S([Foo: 1, Bar: 2])
assert_same ~S(["with \"scare quotes\"": 1])
end

test "with operators keyword lists" do
Expand Down