Class: RuboCop::Cop::VariableForce::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/variable_force/scope.rb

Overview

A Scope represents a context of local variable visibility. This is a place where local variables belong to. A scope instance holds a scope node and variable entries.

Constant Summary collapse

OUTER_SCOPE_CHILD_INDICES =
{
  defs:   0..0,
  module: 0..0,
  class:  0..1,
  sclass: 0..0,
  block:  0..0
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Scope

Returns a new instance of Scope.



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/cop/variable_force/scope.rb', line 20

def initialize(node)
  # Accept any node type for top level scope
  unless SCOPE_TYPES.include?(node.type) || !node.parent
    raise ArgumentError,
          "Node type must be any of #{SCOPE_TYPES}, " \
          "passed #{node.type}"
  end
  @node = node
  @variables = {}
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'lib/rubocop/cop/variable_force/scope.rb', line 18

def node
  @node
end

#variablesObject (readonly)

Returns the value of attribute variables.



18
19
20
# File 'lib/rubocop/cop/variable_force/scope.rb', line 18

def variables
  @variables
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
# File 'lib/rubocop/cop/variable_force/scope.rb', line 31

def ==(other)
  @node.equal?(other.node)
end

#body_nodeObject



43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/variable_force/scope.rb', line 43

def body_node
  child_index = case @node.type
                when :module, :sclass     then 1
                when :def, :class, :block then 2
                when :defs                then 3
                end

  child_index ? @node.children[child_index] : @node
end

#each_node(&block) ⇒ Object



53
54
55
56
# File 'lib/rubocop/cop/variable_force/scope.rb', line 53

def each_node(&block)
  return to_enum(__method__) unless block_given?
  scan_node(node, &block)
end

#nameObject



35
36
37
38
39
40
41
# File 'lib/rubocop/cop/variable_force/scope.rb', line 35

def name
  # TODO: Add an else clause
  case @node.type
  when :def  then @node.children[0]
  when :defs then @node.children[1]
  end
end