Skip to content

Commit f525f5b

Browse files
author
José Valim
committed
Add unquote: true support to Macro.escape
1 parent 5c30a68 commit f525f5b

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3284,7 +3284,7 @@ defmodule Kernel do
32843284
"""
32853285
defmacro defdelegate(funs, opts) do
32863286
quote do
3287-
funs = unquote(Macro.escape_quoted(funs))
3287+
funs = unquote(Macro.escape(funs, unquote: true))
32883288
opts = unquote(opts)
32893289

32903290
target = Keyword.get(opts, :to) ||

lib/elixir/lib/macro.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ defmodule Macro do
5454
Recursively escapes a value so it can be inserted
5555
into a syntax tree.
5656
57+
One may pass `unquote: true` to `Macro.escape/2`
58+
which leaves unquote statements unescaped, effectively
59+
unquoting the contents on escape.
60+
5761
## Examples
5862
5963
iex> Macro.escape(:foo)
@@ -62,14 +66,12 @@ defmodule Macro do
6266
iex> Macro.escape({ :a, :b, :c })
6367
{ :{}, [], [:a, :b, :c] }
6468
65-
"""
66-
def escape(expr) do
67-
:elixir_quote.escape(expr, false) |> elem(0)
68-
end
69+
iex> Macro.escape({ :unquote, [], [1] }, unquote: true)
70+
1
6971
70-
@doc false
71-
def escape_quoted(expr) do
72-
:elixir_quote.escape(expr, true) |> elem(0)
72+
"""
73+
def escape(expr, opts // []) do
74+
:elixir_quote.escape(expr, Keyword.get(opts, :unquote, false)) |> elem(0)
7375
end
7476

7577
@doc %B"""

lib/elixir/test/elixir/macro_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ defmodule MacroTest do
3838

3939
test :escape_with_unquote do
4040
contents = quote unquote: false, do: unquote(1)
41-
assert Macro.escape_quoted(contents) == 1
41+
assert Macro.escape(contents, unquote: true) == 1
4242

4343
contents = quote unquote: false, do: unquote(x)
44-
assert Macro.escape_quoted(contents) == { :x, [], MacroTest }
44+
assert Macro.escape(contents, unquote: true) == { :x, [], MacroTest }
4545
end
4646

4747
defp eval_escaped(contents) do
48-
{ eval, [] } = Code.eval_quoted(Macro.escape_quoted(contents))
48+
{ eval, [] } = Code.eval_quoted(Macro.escape(contents, unquote: true))
4949
eval
5050
end
5151

@@ -71,7 +71,7 @@ defmodule MacroTest do
7171

7272
test :escape_with_splicing do
7373
contents = quote unquote: false, do: [1, 2, 3, 4, 5]
74-
assert Macro.escape_quoted(contents) == [1, 2, 3, 4, 5]
74+
assert Macro.escape(contents, unquote: true) == [1, 2, 3, 4, 5]
7575

7676
contents = quote unquote: false, do: [1, 2, unquote_splicing([3, 4, 5])]
7777
assert eval_escaped(contents) == [1, 2, 3, 4, 5]

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ defmodule ExUnit.Callbacks do
182182
end
183183

184184
defp escape(contents) do
185-
Macro.escape_quoted(contents)
185+
Macro.escape(contents, unquote: true)
186186
end
187187

188188
defp compile_callbacks(env, kind) do

lib/ex_unit/lib/ex_unit/case.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ defmodule ExUnit.Case do
9292
end
9393

9494
def message, [unquote(Macro.escape var)], [], do:
95-
unquote(Macro.escape_quoted contents)
95+
unquote(Macro.escape contents, unquote: true)
9696
end
9797
end
9898
end

0 commit comments

Comments
 (0)