@@ -164,15 +164,7 @@ do_escape(BitString, _) when is_bitstring(BitString) ->
164
164
end ;
165
165
166
166
do_escape (Map , Q ) when is_map (Map ) ->
167
- TT =
168
- [if
169
- is_reference (V ) ->
170
- argument_error (<<('Elixir.Kernel' :inspect (Map , []))/binary , " contains a reference (" ,
171
- ('Elixir.Kernel' :inspect (V , []))/binary , " ) and therefore it cannot be escaped " ,
172
- " (it must be defined within a function instead). " , (bad_escape_hint ())/binary >>);
173
- true ->
174
- {do_quote (K , Q ), do_quote (V , Q )}
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 ))],
176
168
{'%{}' , [], TT };
177
169
178
170
do_escape ([], _ ) ->
@@ -206,6 +198,28 @@ do_escape(Fun, _) when is_function(Fun) ->
206
198
do_escape (Other , _ ) ->
207
199
bad_escape (Other ).
208
200
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 .
215
+
216
+ find_tuple_ref (Tuple , Index ) when Index > tuple_size (Tuple ) -> nil ;
217
+ find_tuple_ref (Tuple , Index ) ->
218
+ case element (Index , Tuple ) of
219
+ Ref when is_reference (Ref ) -> Ref ;
220
+ _ -> find_tuple_ref (Tuple , Index + 1 )
221
+ end .
222
+
209
223
bad_escape (Arg ) ->
210
224
argument_error (<<" cannot escape " , ('Elixir.Kernel' :inspect (Arg , []))/binary , " . " ,
211
225
(bad_escape_hint ())/binary >>).
0 commit comments