1
1
-module (elixir_aliases ).
2
- -export ([nesting_alias /2 , last /1 , concat /1 , safe_concat /1 , lookup / 2 ,
2
+ -export ([nesting_alias /2 , last /1 , concat /1 , safe_concat /1 ,
3
3
format_error /1 , ensure_loaded /3 , expand /2 ]).
4
4
-include (" elixir.hrl" ).
5
5
-compile ({parse_transform , elixir_transform }).
6
6
7
- % % Expand an alias returning an atom if possible.
8
- % % Otherwise returns the list of aliases args.
9
-
10
- expand ({ '__aliases__' , _Line , [H ] }, Aliases ) when H /= 'Elixir' ->
11
- expand_one (H , Aliases );
12
-
13
- expand ({ '__aliases__' , _Line , [H |T ] }, Aliases ) when is_atom (H ) ->
14
- concat (case H of
15
- 'Elixir' -> [H |T ];
16
- _ -> [expand_one (H , Aliases )|T ]
17
- end );
18
-
19
- expand ({ '__aliases__' , _Line , List }, _Aliases ) ->
7
+ % % Expand an alias. It returns an atom (mearning that there
8
+ % % was an expansion) or a list of atoms.
9
+
10
+ expand ({ '__aliases__' , _Meta , [H ] }, Aliases ) when H /= 'Elixir' ->
11
+ case expand_one (H , Aliases ) of
12
+ false -> [H ];
13
+ Atom -> Atom
14
+ end ;
15
+
16
+ expand ({ '__aliases__' , _Meta , [H |T ] }, Aliases ) when is_atom (H ) ->
17
+ case H of
18
+ 'Elixir' ->
19
+ concat (T );
20
+ _ ->
21
+ case expand_one (H , Aliases ) of
22
+ false -> [H |T ];
23
+ Atom -> concat ([Atom |T ])
24
+ end
25
+ end ;
26
+
27
+ expand ({ '__aliases__' , _Meta , List }, _Aliases ) ->
20
28
List .
21
29
22
30
expand_one (H , Aliases ) ->
23
- lookup (list_to_atom (" Elixir-" ++ atom_to_list (H )), Aliases ).
31
+ Lookup = list_to_atom (" Elixir-" ++ atom_to_list (H )),
32
+ case lookup (Lookup , Aliases ) of
33
+ Lookup -> false ;
34
+ Else -> Else
35
+ end .
24
36
25
37
% % Ensure a module is loaded before its usage.
26
38
@@ -39,7 +51,7 @@ ensure_loaded(Line, Ref, S) ->
39
51
elixir_errors :form_error (Line , S # elixir_scope .file , ? MODULE , { Kind , Ref })
40
52
end .
41
53
42
- % % Receives an atom and returns the last alias.
54
+ % % Receives an atom and returns the last bit as an alias.
43
55
44
56
last (Atom ) ->
45
57
Last = last (lists :reverse (atom_to_list (Atom )), []),
@@ -56,8 +68,13 @@ last([], Acc) -> Acc.
56
68
% %
57
69
% % Examples:
58
70
% %
59
- % % nesting_alias('Elixir.Foo.Bar', 'Elixir.Foo.Bar.Baz')
60
- % % { '__aliases__', [], ['Elixir', 'Foo', 'Bar', 'Baz'] }
71
+ % % nesting_alias('Elixir.Foo.Bar', 'Elixir.Foo.Bar.Baz.Bat')
72
+ % % { '__aliases__', [], ['Elixir', 'Foo', 'Bar', 'Baz'] }
73
+ % %
74
+ % % When passed to alias, the example above will generate an
75
+ % % alias like:
76
+ % %
77
+ % % 'Elixir.Baz' => 'Elixir.Foo.Bar.Baz'
61
78
% %
62
79
nesting_alias (nil , _Full ) -> false ;
63
80
@@ -69,18 +86,18 @@ nesting_alias(Prefix, Full) ->
69
86
do_nesting ([X |PreTail ], [X |Tail ], Acc ) ->
70
87
do_nesting (PreTail , Tail , [X |Acc ]);
71
88
do_nesting ([], [H |_ ], Acc ) ->
72
- { '__aliases__' , [], ['Elixir' |[binary_to_atom ( X , utf8 ) || X <- lists :reverse ([H |Acc ])]] };
89
+ { '__aliases__' , [], ['Elixir' |[list_to_atom ( X ) || X <- lists :reverse ([H |Acc ])]] };
73
90
do_nesting (_ , _ , _Acc ) ->
74
91
false .
75
92
76
93
list_nesting (Atom ) ->
77
- case binary : split ( atom_to_binary (Atom , utf8 ), << $- >>, [ global ] ) of
78
- [<< " Elixir" >> |T ] -> T ;
94
+ case string : tokens ( atom_to_list (Atom ), " - " ) of
95
+ [" Elixir" |T ] -> T ;
79
96
_ -> []
80
97
end .
81
98
82
- % % Receives a list of atoms representing modules
83
- % % and concatenate them.
99
+ % % Receives a list of atoms, binaries or lists
100
+ % % representing modules and concatenates them.
84
101
85
102
concat (Args ) -> list_to_atom (raw_concat (Args )).
86
103
safe_concat (Args ) -> list_to_existing_atom (raw_concat (Args )).
@@ -104,7 +121,7 @@ dot_to_dash(List) ->
104
121
_ -> X
105
122
end || X <- List ].
106
123
107
- % % Lookup an alias in the current scope
124
+ % % Lookup an alias in the current scope.
108
125
109
126
lookup (Else , Dict ) ->
110
127
case orddict :find (Else , Dict ) of
0 commit comments