@@ -49,8 +49,8 @@ defmodule Macro do
49
49
end
50
50
51
51
@ doc """
52
- Breaks a pipeline expression into a list.
53
-
52
+ Breaks a pipeline expression into a list.
53
+
54
54
Raises if the pipeline is ill-formed.
55
55
"""
56
56
@ spec unpipe ( Macro . t ) :: [ Macro . t ]
@@ -152,8 +152,8 @@ defmodule Macro do
152
152
end
153
153
154
154
@ doc % S """
155
- Unescape the given chars.
156
-
155
+ Unescape the given chars.
156
+
157
157
This is the unescaping behavior
158
158
used by default in Elixir single- and double-quoted strings.
159
159
Check `unescape_string/2` for information on how to customize
@@ -298,7 +298,14 @@ defmodule Macro do
298
298
299
299
# Tuple containers
300
300
def to_string ( { :{} , _ , args } = ast , fun ) do
301
- fun . ( ast , "{" <> Enum . map_join ( args , ", " , & to_string ( & 1 , fun ) ) <> "}" )
301
+ if match? ( [ _ ] , args ) do
302
+ tuple = "{" <> Enum . map_join ( args , ", " , & to_string ( & 1 , fun ) ) <> "}"
303
+ else
304
+ args = args_to_string ( args , fun )
305
+ tuple = "{" <> args <> "}"
306
+ end
307
+
308
+ fun . ( ast , tuple )
302
309
end
303
310
304
311
# Fn keyword
@@ -321,6 +328,17 @@ defmodule Macro do
321
328
fun . ( ast , "(" <> arrow_to_string ( ast , fun , true ) <> ")" )
322
329
end
323
330
331
+ # left when right
332
+ def to_string ( { :when , _ , [ left , right ] } = ast , fun ) do
333
+ if right != [ ] and Keyword . keyword? ( right ) do
334
+ right = kw_list_to_string ( right , fun )
335
+ else
336
+ right = fun . ( ast , op_to_string ( right , fun , :when , :right ) )
337
+ end
338
+
339
+ fun . ( ast , op_to_string ( left , fun , :when , :left ) <> " when " <> right )
340
+ end
341
+
324
342
# Binary ops
325
343
def to_string ( { op , _ , [ left , right ] } = ast , fun ) when op in @ binary_ops do
326
344
fun . ( ast , op_to_string ( left , fun , op , :left ) <> " #{ op } " <> op_to_string ( right , fun , op , :right ) )
@@ -362,11 +380,15 @@ defmodule Macro do
362
380
363
381
# Lists
364
382
def to_string ( list , fun ) when is_list ( list ) do
365
- if Keyword . keyword? ( list ) do
366
- fun . ( list , "[" <> kw_list_to_string ( list , fun ) <> "]" )
367
- else
368
- fun . ( list , "[" <> Enum . map_join ( list , ", " , & to_string ( & 1 , fun ) ) <> "]" )
369
- end
383
+ fun . ( list , cond do
384
+ Keyword . keyword? ( list ) ->
385
+ "[" <> kw_list_to_string ( list , fun ) <> "]"
386
+ not match? ( [ _ ] , list ) ->
387
+ args = args_to_string ( list , fun )
388
+ "[" <> args <> "]"
389
+ true ->
390
+ "[" <> Enum . map_join ( list , ", " , & to_string ( & 1 , fun ) ) <> "]"
391
+ end )
370
392
end
371
393
372
394
# All other structures
@@ -389,18 +411,20 @@ defmodule Macro do
389
411
defp call_to_string ( other , fun ) , do: to_string ( other , fun )
390
412
391
413
defp call_to_string_with_args ( target , args , fun ) do
392
- { list , last } = :elixir_utils . split_last ( args )
393
414
target = call_to_string ( target , fun )
415
+ args = args_to_string ( args , fun )
416
+ target <> "(" <> args <> ")"
417
+ end
394
418
395
- case last != [ ] and Keyword . keyword? ( last ) do
396
- true ->
397
- args = Enum . map_join ( list , ", " , & to_string ( & 1 , fun ) )
398
- if list != [ ] , do: args = args <> ", "
399
- args = args <> kw_list_to_string ( last , fun )
400
- target <> "(" <> args <> ") "
401
- false ->
402
- args = Enum . map_join ( args , ", " , & to_string ( & 1 , fun ) )
403
- target <> "(" <> args <> ")"
419
+ defp args_to_string ( args , fun ) do
420
+ { list , last } = :elixir_utils . split_last ( args )
421
+
422
+ if last != [ ] and Keyword . keyword? ( last ) do
423
+ args = Enum . map_join ( list , ", " , & to_string ( & 1 , fun ) )
424
+ if list != [ ] , do: args = args <> ", "
425
+ args <> kw_list_to_string ( last , fun )
426
+ else
427
+ Enum . map_join ( args , ", " , & to_string ( & 1 , fun ) )
404
428
end
405
429
end
406
430
@@ -483,8 +507,8 @@ defmodule Macro do
483
507
end
484
508
485
509
@ doc """
486
- Receives an AST node and expands it once.
487
-
510
+ Receives an AST node and expands it once.
511
+
488
512
The following contents are expanded:
489
513
490
514
* Macros (local or remote);
@@ -665,8 +689,8 @@ defmodule Macro do
665
689
666
690
@ doc """
667
691
Receives an AST node and expands it until it no longer represents
668
- a macro.
669
-
692
+ a macro.
693
+
670
694
Check `expand_once/2` for more information on how
671
695
expansion works.
672
696
"""
@@ -715,7 +739,7 @@ defmodule Macro do
715
739
@ doc """
716
740
Recursively traverses the quoted expression checking if all sub-terms are
717
741
safe.
718
-
742
+
719
743
Terms are considered safe if they represent data structures and don't actually
720
744
evaluate code. Returns `:ok` unless a given term is unsafe,
721
745
which is returned as `{ :unsafe, term }`.
0 commit comments