Skip to content

Commit 26f3584

Browse files
committed
Yet another refactoring proposal
1 parent 30ee6fd commit 26f3584

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,7 @@ do_escape(BitString, _) when is_bitstring(BitString) ->
164164
end;
165165

166166
do_escape(Map, Q) when is_map(Map) ->
167-
TT =
168-
[case do_quote_map_value(V, Q) of
169-
{ok, QV} ->
170-
{do_quote(K, Q), QV};
171-
{error, Ref} ->
172-
argument_error(<<('Elixir.Kernel':inspect(Map, []))/binary, " contains a reference (",
173-
('Elixir.Kernel':inspect(Ref, []))/binary, ") and therefore it cannot be escaped ",
174-
"(it must be defined within a function instead). ", (bad_escape_hint())/binary>>)
175-
end || {K, V} <- lists:sort(maps:to_list(Map))],
167+
TT = [escape_map_key_value(K, V, Map, Q) || {K, V} <- lists:sort(maps:to_list(Map))],
176168
{'%{}', [], TT};
177169

178170
do_escape([], _) ->
@@ -206,16 +198,26 @@ do_escape(Fun, _) when is_function(Fun) ->
206198
do_escape(Other, _) ->
207199
bad_escape(Other).
208200

209-
do_quote_map_value(Ref, _Q) when is_reference(Ref) -> {error, Ref};
210-
do_quote_map_value(Tuple, Q) when is_tuple(Tuple) -> do_quote_map_value_tuple(Tuple, Q, 1);
211-
do_quote_map_value(Value, Q) -> {ok, do_quote(Value, Q)}.
201+
escape_map_key_value(K, V, Map, Q) ->
202+
MaybeRef = if
203+
is_reference(V) -> V;
204+
is_tuple(V) -> find_tuple_ref(V, 1);
205+
true -> nil
206+
end,
207+
if
208+
is_reference(MaybeRef) ->
209+
argument_error(<<('Elixir.Kernel':inspect(Map, []))/binary, " contains a reference (",
210+
('Elixir.Kernel':inspect(MaybeRef, []))/binary, ") and therefore it cannot be escaped ",
211+
"(it must be defined within a function instead). ", (bad_escape_hint())/binary>>);
212+
true ->
213+
{do_quote(K, Q), do_quote(V, Q)}
214+
end.
212215

213-
do_quote_map_value_tuple(Tuple, Q, Index) when Index > tuple_size(Tuple) ->
214-
{ok, do_quote(Tuple, Q)};
215-
do_quote_map_value_tuple(Tuple, Q, Index) ->
216+
find_tuple_ref(Tuple, Index) when Index > tuple_size(Tuple) -> nil;
217+
find_tuple_ref(Tuple, Index) ->
216218
case element(Index, Tuple) of
217-
Ref when is_reference(Ref) -> {error, Ref};
218-
_ -> do_quote_map_value_tuple(Tuple, Q, Index + 1)
219+
Ref when is_reference(Ref) -> Ref;
220+
_ -> find_tuple_ref(Tuple, Index + 1)
219221
end.
220222

221223
bad_escape(Arg) ->

0 commit comments

Comments
 (0)