-
Notifications
You must be signed in to change notification settings - Fork 413
/
block.ex
246 lines (204 loc) · 4.74 KB
/
block.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
defmodule Credo.Code.Block do
@moduledoc """
This module provides helper functions to analyse blocks, e.g. the block taken
by the `if` macro.
"""
@doc """
Returns the do: block of a given AST node.
"""
def all_blocks_for!(ast) do
[
do_block_for!(ast),
else_block_for!(ast),
rescue_block_for!(ast),
after_block_for!(ast)
]
end
@doc """
Returns true if the given `ast` has a do block.
"""
def do_block?(ast) do
case do_block_for(ast) do
{:ok, _block} ->
true
nil ->
false
end
end
@doc """
Returns the do: block of a given AST node.
"""
def do_block_for!(ast) do
case do_block_for(ast) do
{:ok, block} ->
block
nil ->
nil
end
end
@doc """
Returns a tuple {:ok, do_block} or nil for a given AST node.
"""
def do_block_for({_atom, _meta, arguments}) when is_list(arguments) do
do_block_for(arguments)
end
def do_block_for(do: block) do
{:ok, block}
end
def do_block_for(arguments) when is_list(arguments) do
Enum.find_value(arguments, &find_keyword(&1, :do))
end
def do_block_for(_) do
nil
end
@doc """
Returns true if the given `ast` has an else block.
"""
def else_block?(ast) do
case else_block_for(ast) do
{:ok, _block} ->
true
nil ->
false
end
end
@doc """
Returns the `else` block of a given AST node.
"""
def else_block_for!(ast) do
case else_block_for(ast) do
{:ok, block} ->
block
nil ->
nil
end
end
@doc """
Returns a tuple {:ok, else_block} or nil for a given AST node.
"""
def else_block_for({_atom, _meta, arguments}) when is_list(arguments) do
else_block_for(arguments)
end
def else_block_for(do: _do_block, else: else_block) do
{:ok, else_block}
end
def else_block_for(arguments) when is_list(arguments) do
Enum.find_value(arguments, &find_keyword(&1, :else))
end
def else_block_for(_) do
nil
end
@doc """
Returns true if the given `ast` has an rescue block.
"""
def rescue_block?(ast) do
case rescue_block_for(ast) do
{:ok, _block} ->
true
nil ->
false
end
end
@doc """
Returns the rescue: block of a given AST node.
"""
def rescue_block_for!(ast) do
case rescue_block_for(ast) do
{:ok, block} ->
block
nil ->
nil
end
end
@doc """
Returns a tuple {:ok, rescue_block} or nil for a given AST node.
"""
def rescue_block_for({_atom, _meta, arguments}) when is_list(arguments) do
rescue_block_for(arguments)
end
def rescue_block_for(do: _do_block, rescue: rescue_block) do
{:ok, rescue_block}
end
def rescue_block_for(arguments) when is_list(arguments) do
Enum.find_value(arguments, &find_keyword(&1, :rescue))
end
def rescue_block_for(_) do
nil
end
@doc """
Returns true if the given `ast` has an after block.
"""
def after_block?(ast) do
case after_block_for(ast) do
{:ok, _block} ->
true
nil ->
false
end
end
@doc """
Returns the after: block of a given AST node.
"""
def after_block_for!(ast) do
case after_block_for(ast) do
{:ok, block} ->
block
nil ->
nil
end
end
@doc """
Returns a tuple {:ok, after_block} or nil for a given AST node.
"""
def after_block_for({_atom, _meta, arguments}) when is_list(arguments) do
after_block_for(arguments)
end
def after_block_for(do: _do_block, after: after_block) do
{:ok, after_block}
end
def after_block_for(arguments) when is_list(arguments) do
Enum.find_value(arguments, &find_keyword(&1, :after))
end
def after_block_for(_) do
nil
end
defp find_keyword(list, keyword) when is_list(list) do
if Keyword.has_key?(list, keyword) do
{:ok, list[keyword]}
else
nil
end
end
defp find_keyword(_, _), do: nil
@doc """
Returns the children of the `do` block of the given AST node.
"""
def calls_in_do_block({_op, _meta, arguments}) do
arguments
|> do_block_for!
|> instructions_for
end
def calls_in_do_block(arg) do
arg
|> do_block_for!
|> instructions_for
end
@doc """
Returns the children of the `rescue` block of the given AST node.
"""
def calls_in_rescue_block({_op, _meta, arguments}) do
arguments
|> rescue_block_for!
|> instructions_for
end
def calls_in_rescue_block(arg) do
arg
|> rescue_block_for!
|> instructions_for
end
defp instructions_for({:__block__, _meta, calls}), do: calls
defp instructions_for(v)
when is_atom(v) or is_tuple(v) or is_binary(v) or is_float(v) or is_integer(v),
do: List.wrap(v)
defp instructions_for(v) when is_list(v), do: [v]
end