Skip to content

Commit 73340a4

Browse files
author
José Valim
committed
Consider zero-arity on Macro.to_binary, closes #682
1 parent 94cc204 commit 73340a4

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/elixir/lib/macro.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ defmodule Macro do
215215

216216
# left -> right
217217
def to_binary({ :->, _, _ } = arrow) do
218-
"(" <> arrow_to_binary(arrow) <> ")"
218+
"(" <> arrow_to_binary(arrow, true) <> ")"
219219
end
220220

221221
# Binary ops
@@ -287,8 +287,8 @@ defmodule Macro do
287287

288288
defp block_to_binary({ :->, _, exprs }) do
289289
Enum.map_join(exprs, "\n", fn({ left, right }) ->
290-
left = Enum.map_join(left, ", ", to_binary(&1))
291-
left <> " ->\n " <> adjust_new_lines block_to_binary(right), "\n "
290+
left = comma_join_or_empty_paren(left, false)
291+
left <> "->\n " <> adjust_new_lines block_to_binary(right), "\n "
292292
end)
293293
end
294294

@@ -304,13 +304,20 @@ defmodule Macro do
304304

305305
defp op_to_binary(expr), do: to_binary(expr)
306306

307-
defp arrow_to_binary({ :->, _, pairs }) do
307+
defp arrow_to_binary({ :->, _, pairs }, paren // false) do
308308
Enum.map_join(pairs, "; ", fn({ left, right }) ->
309-
left = Enum.map_join(left, ", ", to_binary(&1))
310-
left <> " -> " <> to_binary(right)
309+
left = comma_join_or_empty_paren(left, paren)
310+
left <> "-> " <> to_binary(right)
311311
end)
312312
end
313313

314+
defp comma_join_or_empty_paren([], true), do: "() "
315+
defp comma_join_or_empty_paren([], false), do: ""
316+
317+
defp comma_join_or_empty_paren(left, _) do
318+
Enum.map_join(left, ", ", to_binary(&1)) <> " "
319+
end
320+
314321
defp adjust_new_lines(block, replacement) do
315322
bc <<x>> inbits block do
316323
<< case x == ?\n do

lib/elixir/test/elixir/macro_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ defmodule MacroTest do
187187
end
188188

189189
test :fn_to_binary do
190+
assert Macro.to_binary(quote do: (() -> x)) == "(() -> x)"
191+
assert Macro.to_binary(quote do: (fn -> 1 + 2 end)) == "fn -> 1 + 2 end"
190192
assert Macro.to_binary(quote do: (fn(x) -> x + 1 end)) == "fn x -> x + 1 end"
191193

192194
assert Macro.to_binary(quote do: (fn(x) -> y = x + 1; y end)) <> "\n" == """

0 commit comments

Comments
 (0)