Class: Nitpick::LocalVariableCounter

Inherits:
Nitpicker
  • Object
show all
Defined in:
lib/nitpick/local_variable_counter.rb

Direct Known Subclasses

ArgumentNitpicker, LocalVariableNitpicker

Instance Attribute Summary

Attributes inherited from Nitpicker

#warnings

Instance Method Summary collapse

Methods inherited from Nitpicker

#nitpick!, #process_cfunc, #process_defn, #scan_for, #warn

Constructor Details

#initialize(klass, meth) ⇒ LocalVariableCounter

Returns a new instance of LocalVariableCounter.



3
4
5
6
7
8
9
# File 'lib/nitpick/local_variable_counter.rb', line 3

def initialize(klass, meth)
  super
  # uses.succ each time a local variable is referenced or assigned to
  # calls.succ each time a message is sent to a lvar (foo.bar)
  @lvars = Hash.new {|h,k| h[k] = {:uses => 0, :calls => 0}}
  @args = []
end

Instance Method Details

#call(name) ⇒ Object



19
20
21
# File 'lib/nitpick/local_variable_counter.rb', line 19

def call(name)
  @lvars[name][:calls] += 1
end

#process_args(exp) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nitpick/local_variable_counter.rb', line 58

def process_args(exp)
  exp.each do |arg|
    next unless arg.is_a? Symbol
    
    name = arg.to_s.gsub(/^\*/, '')
    next if name == ""
    
    @args << name.to_sym
    use name.to_sym
  end
  
  exp.clear
  
  s(:args, *@args)
end

#process_block_arg(exp) ⇒ Object



74
75
76
77
78
79
# File 'lib/nitpick/local_variable_counter.rb', line 74

def process_block_arg(exp)
  name = exp.shift
  @args << name
  use name
  s(:block_arg, name)
end

#process_call(exp) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/nitpick/local_variable_counter.rb', line 42

def process_call(exp)
  recv = process(exp.shift)
  meth = exp.shift
  args = process(exp.shift)

  call recv.last if recv.first == :lvar
  
  s(:call, recv, meth, args)
end

#process_iasgn(exp) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/nitpick/local_variable_counter.rb', line 33

def process_iasgn(exp)
  name = exp.shift
  value = exp.shift
  
  process(value)
  
  s(:iasgn, name, value)
end

#process_lasgn(exp) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/nitpick/local_variable_counter.rb', line 23

def process_lasgn(exp)
  name = exp.shift
  value = exp.shift
  
  use name
  process(value)
  
  s(:lasgn, name, value)
end

#process_lvar(exp) ⇒ Object



52
53
54
55
56
# File 'lib/nitpick/local_variable_counter.rb', line 52

def process_lvar(exp)
  name = exp.shift
  use name
  s(:lvar, name)
end

#use(name) ⇒ Object



15
16
17
# File 'lib/nitpick/local_variable_counter.rb', line 15

def use(name)
  @lvars[name][:uses] += 1
end

#uses(name) ⇒ Object



11
12
13
# File 'lib/nitpick/local_variable_counter.rb', line 11

def uses(name)
  @lvars[name][:uses]
end