-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmethod_locals.rb
60 lines (47 loc) · 1.25 KB
/
method_locals.rb
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
module NamingNormalizers
class MethodLocals < Base
def initialize(code)
super(code)
@method_name = nil
@methods_vars = {}
end
%i[
on_argument
on_const
on_var
on_vasgn
].each do |method_name|
define_method method_name do |node|
replace_loc_name(node)
super(node)
end
end
def on_def(node)
@method_name = node.loc.name.source
@methods_vars[@method_name] = {}
super(node)
end
def on_end(node)
node.pry
end
def handler_missing(node)
super(node)
end
def replace_loc_name(node)
replace(node.loc.name, placeholder_for(node.loc.name.source))
end
def replace_loc_selector(node)
replace(node.loc.selector, placeholder_for(node.loc.selector.source))
end
def placeholder_for(token)
# Return if we're outside of a method
return token unless instance_variable_defined?("@method_name") && @method_name
# Get the vars for this method_naem
meth_vars = @methods_vars[@method_name]
# Return if we have one
return meth_vars[token] if meth_vars.key?(token)
# Else get out of here
meth_vars[token] = "#{@method_name}_#{meth_vars.length}"
end
end
end