-
Notifications
You must be signed in to change notification settings - Fork 414
/
scope.ex
140 lines (105 loc) · 3.42 KB
/
scope.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
defmodule Credo.Code.Scope do
@moduledoc """
This module provides helper functions to determine the scope name at a certain
point in the analysed code.
"""
@def_ops [:def, :defp, :defmacro]
@doc """
Returns the module part of a scope.
iex> Credo.Code.Scope.mod_name("Credo.Code")
"Credo.Code"
iex> Credo.Code.Scope.mod_name("Credo.Code.ast")
"Credo.Code"
"""
def mod_name(nil), do: nil
def mod_name(scope_name) do
names = String.split(scope_name, ".")
base_name = List.last(names)
if String.match?(base_name, ~r/^[a-z]/) do
names
|> Enum.slice(0..(length(names) - 2))
|> Enum.join(".")
else
scope_name
end
end
@doc """
Returns the scope for the given line as a tuple consisting of the call to
define the scope (`:defmodule`, `:def`, `:defp` or `:defmacro`) and the
name of the scope.
Examples:
{:defmodule, "Foo.Bar"}
{:def, "Foo.Bar.baz"}
"""
def name(_ast, line: 0), do: nil
def name(ast, line: line) do
ast
|> scope_info_list()
|> name_from_scope_info_list(line)
end
@doc false
def name_from_scope_info_list(scope_info_list, line) do
result =
Enum.find(scope_info_list, fn
{line_no, _op, _arguments} when line_no <= line -> true
_ -> false
end)
case result do
{_line_no, op, arguments} ->
name = Credo.Code.Name.full(arguments)
{op, name}
_ ->
{nil, ""}
end
end
@doc false
def scope_info_list(ast) do
{_, scope_info_list} = Macro.prewalk(ast, [], &traverse_modules(&1, &2, nil, nil))
Enum.reverse(scope_info_list)
end
defp traverse_modules({:defmodule, meta, arguments} = ast, acc, current_scope, _current_op)
when is_list(arguments) do
new_scope_part = Credo.Code.Module.name(ast)
scope_name =
[current_scope, new_scope_part]
|> Enum.reject(&is_nil/1)
|> Credo.Code.Name.full()
defmodule_scope_info = {meta[:line], :defmodule, scope_name}
{_, def_scope_infos} =
Macro.prewalk(arguments, [], &traverse_defs(&1, &2, scope_name, :defmodule))
new_acc = (acc ++ [defmodule_scope_info]) ++ def_scope_infos
{nil, new_acc}
end
defp traverse_modules({_op, meta, _arguments} = ast, acc, current_scope, current_op) do
scope_info = {meta[:line], current_op, current_scope}
{ast, acc ++ [scope_info]}
end
defp traverse_modules(ast, acc, _current_scope, _current_op) do
{ast, acc}
end
defp traverse_defs({:defmodule, _meta, arguments} = ast, acc, current_scope, _current_op)
when is_list(arguments) do
{_, scopes} = Macro.prewalk(ast, [], &traverse_modules(&1, &2, current_scope, :defmodule))
{nil, acc ++ scopes}
end
for op <- @def_ops do
defp traverse_defs({unquote(op), meta, arguments} = ast, acc, current_scope, _current_op)
when is_list(arguments) do
new_scope_part = Credo.Code.Module.def_name(ast)
scope_name =
[current_scope, new_scope_part]
|> Enum.reject(&is_nil/1)
|> Credo.Code.Name.full()
scope_info = {meta[:line], unquote(op), scope_name}
new_acc = acc ++ [scope_info]
{nil, new_acc}
end
end
defp traverse_defs({_op, meta, _arguments} = ast, acc, current_scope, current_op) do
scope_info = {meta[:line], current_op, current_scope}
{ast, acc ++ [scope_info]}
end
defp traverse_defs(ast, acc, _current_scope, _current_op) do
{ast, acc}
end
end