Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/elixir/src/elixir_quote.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ escape(Expr, Op, Unquote) ->
op=Op,
unquote=Unquote
},
case Unquote of
true -> do_quote(Expr, Q);
false -> do_escape(Expr, Q)
end
do_escape(Expr, Q)
catch
Kind:Reason:Stacktrace ->
Pruned = lists:dropwhile(fun
Expand All @@ -165,6 +162,22 @@ escape(Expr, Op, Unquote) ->
erlang:raise(Kind, Reason, Pruned)
end.

%% Quote - unquote

do_escape({quote, Meta, Args}, #elixir_quote{unquote=true} = Q) when is_list(Meta), is_list(Args) ->
do_quote({quote, Meta, Args}, Q#elixir_quote{unquote=false});

do_escape({unquote, Meta, [Expr]}, #elixir_quote{unquote=true} = Q) when is_list(Meta) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need unquote_splicing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so yes!
Will add it if we agree on the approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so actually it is not needed here, but when escaping lists.
Which currently works (c2aafb4), since it delegates to do_quote_tail.
But we should probably implement a do_escape_tail here too to completely separate these implementations.

do_quote({unquote, Meta, [Expr]}, Q);

do_escape({{{'.', Meta, [Left, unquote]}, _, [Expr]}, _, Args}, #elixir_quote{unquote=true} = Q) when is_list(Meta) ->
do_escape_call(Left, Meta, Expr, Args, Q);

do_escape({{'.', Meta, [Left, unquote]}, _, [Expr]}, #elixir_quote{unquote=true} = Q) when is_list(Meta) ->
do_escape_call(Left, Meta, Expr, nil, Q);

% Tuples

do_escape({Left, Meta, Right}, #elixir_quote{op=escape_and_prune} = Q) when is_list(Meta) ->
TM = [{K, V} || {K, V} <- Meta, (K == no_parens) orelse (K == line) orelse (K == delimiter)],
TL = do_escape(Left, Q),
Expand Down Expand Up @@ -250,6 +263,11 @@ do_escape(Fun, _) when is_function(Fun) ->
do_escape(Other, _) ->
bad_escape(Other).

do_escape_call(Left, Meta, Expr, Args, Q) ->
All = [Left, {unquote, Meta, [Expr]}, Args, Q#elixir_quote.context],
TAll = [do_escape(X, Q) || X <- All],
{{'.', Meta, [elixir_quote, dot]}, Meta, [meta(Meta, Q) | TAll]}.

escape_map_key_value(K, V, Map, Q) ->
MaybeRef = if
is_reference(V) -> V;
Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ defmodule MacroTest do

contents = quote(unquote: false, do: unquote(x))
assert Macro.escape(contents, unquote: true) == {:x, [], MacroTest}

contents = %{foo: quote(unquote: false, do: unquote(1))}
assert Macro.escape(contents, unquote: true) == {:%{}, [], [foo: 1]}
end

test "with generated" do
Expand All @@ -97,6 +100,8 @@ defmodule MacroTest do
test "with remote unquote" do
contents = quote(unquote: false, do: Kernel.unquote(:is_atom)(:ok))
assert eval_escaped(contents) == quote(do: Kernel.is_atom(:ok))

assert eval_escaped(%{foo: contents}) == %{foo: quote(do: Kernel.is_atom(:ok))}
end

test "with nested unquote" do
Expand Down