Skip to content

Commit

Permalink
Consider zero-arity on Macro.to_binary, closes #682
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Nov 30, 2012
1 parent 94cc204 commit 73340a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ defmodule Macro do

# left -> right
def to_binary({ :->, _, _ } = arrow) do
"(" <> arrow_to_binary(arrow) <> ")"
"(" <> arrow_to_binary(arrow, true) <> ")"
end

# Binary ops
Expand Down Expand Up @@ -287,8 +287,8 @@ defmodule Macro do

defp block_to_binary({ :->, _, exprs }) do
Enum.map_join(exprs, "\n", fn({ left, right }) ->
left = Enum.map_join(left, ", ", to_binary(&1))
left <> " ->\n " <> adjust_new_lines block_to_binary(right), "\n "
left = comma_join_or_empty_paren(left, false)
left <> "->\n " <> adjust_new_lines block_to_binary(right), "\n "
end)
end

Expand All @@ -304,13 +304,20 @@ defmodule Macro do

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

defp arrow_to_binary({ :->, _, pairs }) do
defp arrow_to_binary({ :->, _, pairs }, paren // false) do
Enum.map_join(pairs, "; ", fn({ left, right }) ->
left = Enum.map_join(left, ", ", to_binary(&1))
left <> " -> " <> to_binary(right)
left = comma_join_or_empty_paren(left, paren)
left <> "-> " <> to_binary(right)
end)
end

defp comma_join_or_empty_paren([], true), do: "() "
defp comma_join_or_empty_paren([], false), do: ""

defp comma_join_or_empty_paren(left, _) do
Enum.map_join(left, ", ", to_binary(&1)) <> " "
end

defp adjust_new_lines(block, replacement) do
bc <<x>> inbits block do
<< case x == ?\n do
Expand Down
2 changes: 2 additions & 0 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ defmodule MacroTest do
end

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

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

0 comments on commit 73340a4

Please sign in to comment.