Skip to content

Commit d2c9824

Browse files
author
José Valim
committed
Respect :no_parens meta in nullary remote calls in Macro.to_string
Closes #9668.
1 parent 04c9c24 commit d2c9824

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lib/elixir/lib/macro.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,16 @@ defmodule Macro do
894894
end
895895

896896
# All other calls
897+
def to_string({target, meta, []} = ast, fun) do
898+
target = call_to_string(target, fun)
899+
900+
if meta[:no_parens] do
901+
fun.(ast, target)
902+
else
903+
fun.(ast, target <> "()")
904+
end
905+
end
906+
897907
def to_string({target, _, args} = ast, fun) when is_list(args) do
898908
with :error <- unary_call(ast, fun),
899909
:error <- binary_call(ast, fun),

lib/elixir/src/elixir_quote.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,11 @@ do_quote(Other, _, _) ->
282282

283283
%% do_escape
284284

285-
do_escape({Left, _Meta, Right}, Q, E = prune_metadata) ->
285+
do_escape({Left, Meta, Right}, Q, E = prune_metadata) ->
286+
TM = [{K, V} || {K, V} <- Meta, (K == no_parens) orelse (K == line)],
286287
TL = do_quote(Left, Q, E),
287288
TR = do_quote(Right, Q, E),
288-
{'{}', [], [TL, [], TR]};
289+
{'{}', [], [TL, TM, TR]};
289290

290291
do_escape(Tuple, Q, E) when is_tuple(Tuple) ->
291292
TT = do_quote(tuple_to_list(Tuple), Q, E),

lib/elixir/test/elixir/macro_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,24 @@ defmodule MacroTest do
288288
assert Macro.to_string(quoted) == "(foo do\n :ok\nend).bar([1, 2, 3])"
289289
end
290290

291+
test "nullary remote call" do
292+
assert Macro.to_string(quote do: foo.bar) == "foo.bar"
293+
assert Macro.to_string(quote do: foo.bar()) == "foo.bar()"
294+
end
295+
291296
test "atom remote call" do
292297
assert Macro.to_string(quote(do: :foo.bar(1, 2, 3))) == ":foo.bar(1, 2, 3)"
293298
end
294299

295300
test "remote and fun call" do
296-
assert Macro.to_string(quote(do: foo.bar.(1, 2, 3))) == "foo.bar().(1, 2, 3)"
297-
assert Macro.to_string(quote(do: foo.bar.([1, 2, 3]))) == "foo.bar().([1, 2, 3])"
301+
assert Macro.to_string(quote(do: foo.bar().(1, 2, 3))) == "foo.bar().(1, 2, 3)"
302+
assert Macro.to_string(quote(do: foo.bar().([1, 2, 3]))) == "foo.bar().([1, 2, 3])"
298303
end
299304

300305
test "unusual remote atom fun call" do
301306
assert Macro.to_string(quote(do: Foo."42"())) == ~s/Foo."42"()/
302307
assert Macro.to_string(quote(do: Foo."Bar"())) == ~s/Foo."Bar"()/
303-
assert Macro.to_string(quote(do: Foo."bar baz"()."")) == ~s/Foo."bar baz"().""()/
308+
assert Macro.to_string(quote(do: Foo."bar baz"().""())) == ~s/Foo."bar baz"().""()/
304309
assert Macro.to_string(quote(do: Foo."%{}"())) == ~s/Foo."%{}"()/
305310
assert Macro.to_string(quote(do: Foo."..."())) == ~s/Foo."..."()/
306311
end

0 commit comments

Comments
 (0)