Class: Nitpick::Warnings::ShadowedVariable

Inherits:
SimpleWarning show all
Defined in:
lib/nitpick/warnings/shadowed_variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleWarning

#==, discover

Constructor Details

#initialize(*args) ⇒ ShadowedVariable

ShadowedVariable.new takes one or more block variable assignment sexps



9
10
11
12
# File 'lib/nitpick/warnings/shadowed_variable.rb', line 9

def initialize(*args)
  @block_vars = args.shift
  @vars = Set.new
end

Instance Attribute Details

#varsObject (readonly)

Returns the value of attribute vars.



6
7
8
# File 'lib/nitpick/warnings/shadowed_variable.rb', line 6

def vars
  @vars
end

Instance Method Details

#matches?Boolean

Returns:

  • (Boolean)


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
# File 'lib/nitpick/warnings/shadowed_variable.rb', line 14

def matches?
  case @block_vars && @block_vars.first
  when :masgn
    matched = false
    #                     label,  args,                                     splats,       ..
    # :masgns look like [:masgn, [:array, [:dasgn_curr, :x]],               [:lasgn, :a], nil]
    #         or        [:masgn, nil,                                       [:lasgn, :a], nil]
    #         or        [:masgn, [:array, [:dasgn_curr, :x], [:lasgn, :a]], nil,          nil]
    
    to_check = []
    
    assigns = @block_vars[1].deep_clone
    if assigns
      assigns.shift # bump the array
      to_check += assigns
    end
    
    to_check += s(@block_vars[2].deep_clone) if @block_vars[2]
    
    to_check.each do |sexp|
      next unless sexp.first == :lasgn
      vars << sexp[1]
      matched = true
    end
    
    matched
  when :lasgn
    vars << @block_vars[1]
    true
  end
end

#messageObject



46
47
48
# File 'lib/nitpick/warnings/shadowed_variable.rb', line 46

def message
  "One or more variables are being shadowed (#{@vars.to_a.join(',')})"
end