Skip to content

Commit f9e74f9

Browse files
author
José Valim
committed
Add Macro.escape_quoted instead of Macro.escape with options
1 parent 3a98369 commit f9e74f9

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
* enhancements
22
* [ExUnit] Use ANSI escape codes in CLI output
33
* [ExUnit] Include suite run time on CLI results
4+
* [File] Add `File.ls` and `File.ls!`
45
* [IEx] Support `pwd` and `cd` helpers
56
* [Kernel] Better error reporting for invalid bitstring generators
67
* [Kernel] Improve meta-programming by allowing `unquote` on `def/2`, `defp/2`, `defmacro/2` and `defmacrop/2`
7-
* [Macro] `Macro.escape` now supports `escape_unquote` as an option
8+
* [Macro] Add `Macro.escape_quoted` to escape quoted expressions
89
* [Mix] Support `--cover` on mix test and `test_coverage` on Mixfiles
910

1011
* bug fix
1112
* [Binary] inspect no longer escapes standalone hash `#`
13+
* [IEx] The h helper can now retrieve docs for special forms
1214
* [Kernel] Record optimizations were not being triggered in functions inside the record module
1315
* [Kernel] Aliases defined inside macros should be carried over
1416
* [Path] Fix a bug on `Path.expand` when expanding paths starting with `~`

lib/elixir/lib/kernel.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ defmodule Kernel do
31443144
"""
31453145
defmacro defdelegate(funs, opts) do
31463146
quote do
3147-
funs = unquote(Macro.escape(funs, escape_unquote: false))
3147+
funs = unquote(Macro.escape_quoted(funs))
31483148
opts = unquote(opts)
31493149

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

lib/elixir/lib/macro.ex

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ defmodule Macro do
5252
end
5353

5454
@doc """
55-
Recursively escapes the given value so it can be inserted
56-
into a syntax tree. Structures that are valid syntax nodes
57-
(like atoms, integers, binaries) are represented by themselves.
55+
Recursively escapes a value so it can be inserted
56+
into a syntax tree. This must be used when you have
57+
a value that does not represent an AST and you want
58+
to introduce it inside a quoted expression.
5859
59-
## Options
60-
61-
* `escape_unquote` - when false, does not escape unquoted calls
60+
If you have a AST and you want to introduce it inside
61+
a quoted expression in a escaped form, use Macro.escape_quoted
62+
instead.
6263
6364
## Examples
6465
@@ -69,8 +70,8 @@ defmodule Macro do
6970
#=> { :{}, [], [:a, :b, :c] }
7071
7172
"""
72-
def escape(expr, opts // []) do
73-
do_escape(expr, not Keyword.get(opts, :escape_unquote, true))
73+
def escape(expr) do
74+
do_escape(expr, false)
7475
end
7576

7677
defp do_escape({ { { :., meta, [left, :unquote] }, _, [expr] }, _, args }, true) do
@@ -138,6 +139,21 @@ defmodule Macro do
138139
defp do_splice_join(left, []), do: left
139140
defp do_splice_join(left, right), do: { :++, [], [left, right] }
140141

142+
@doc """
143+
Recursively escapes a AST so it can be inserted into
144+
another tree unmodified. If the given AST has any
145+
call to unquote, they are properly evaluated.
146+
147+
## Examples
148+
149+
Macro.escape_quoted({ :+, [], [1,2] })
150+
#=> {:"{}",[],[:+,[],[1,2]]}
151+
152+
"""
153+
def escape_quoted(expr) do
154+
do_escape(expr, true)
155+
end
156+
141157
@doc %B"""
142158
Unescape the given chars. This is the unescaping behavior
143159
used by default in Elixir single- and double-quoted strings.

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(contents, escape_unquote: false) == 1
41+
assert Macro.escape_quoted(contents) == 1
4242

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

4747
defp eval_escaped(contents) do
48-
{ eval, [] } = Code.eval_quoted(Macro.escape(contents, escape_unquote: false))
48+
{ eval, [] } = Code.eval_quoted(Macro.escape_quoted(contents))
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(contents, escape_unquote: false) == [1,2,3,4,5]
74+
assert Macro.escape_quoted(contents) == [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
@@ -126,7 +126,7 @@ defmodule ExUnit.Callbacks do
126126
end
127127

128128
defp escape(contents) do
129-
Macro.escape(contents, escape_unquoted: false)
129+
Macro.escape_quoted(contents)
130130
end
131131

132132
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
@@ -88,7 +88,7 @@ defmodule ExUnit.Case do
8888
end
8989

9090
def message, [unquote(Macro.escape var)], [], do:
91-
unquote(Macro.escape contents, escape_unquote: false)
91+
unquote(Macro.escape_quoted contents)
9292
end
9393
end
9494
end

0 commit comments

Comments
 (0)