summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2018-03-14 10:10:40 +0100
committerJosé Valim <jose.valim@plataformatec.com.br>2018-03-14 10:10:44 +0100
commitd0bd8692ddaea99793c444c61e21d977fa3435cb (patch)
treea2f0fe6d8e9dde5d8bb6239b8619292defe3c46a
parent14147ed88330511f350f807b092148a034343228 (diff)
downloadelixir-d0bd8692ddaea99793c444c61e21d977fa3435cb.tar.gz
Do not escape quoted keywords on formatting, closes #7451
-rw-r--r--lib/elixir/lib/code/formatter.ex11
-rw-r--r--lib/elixir/test/elixir/code_formatter/containers_test.exs2
2 files changed, 12 insertions, 1 deletions
diff --git a/lib/elixir/lib/code/formatter.ex b/lib/elixir/lib/code/formatter.ex
index fe37663f9..c1ee99e23 100644
--- a/lib/elixir/lib/code/formatter.ex
+++ b/lib/elixir/lib/code/formatter.ex
@@ -555,7 +555,16 @@ defmodule Code.Formatter do
{left, state} =
case left_arg do
{:__block__, _, [atom]} when is_atom(atom) ->
- {atom |> Code.Identifier.inspect_as_key() |> string(), state}
+ key =
+ case Code.Identifier.classify(atom) do
+ type when type in [:callable_local, :callable_operator, :not_callable] ->
+ IO.iodata_to_binary([Atom.to_string(atom), ?:])
+
+ _ ->
+ IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
+ end
+
+ {string(key), state}
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} ->
interpolation_to_algebra(entries, @double_quote, state, "\"", "\":")
diff --git a/lib/elixir/test/elixir/code_formatter/containers_test.exs b/lib/elixir/test/elixir/code_formatter/containers_test.exs
index 89989d78f..0c3b5fc27 100644
--- a/lib/elixir/test/elixir/code_formatter/containers_test.exs
+++ b/lib/elixir/test/elixir/code_formatter/containers_test.exs
@@ -200,6 +200,8 @@ defmodule Code.Formatter.ContainersTest do
test "with quoted keyword lists" do
assert_same ~S(["with spaces": 1])
assert_same ~S(["one #{two} three": 1])
+ 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])
end