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.



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

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.



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

def node
  @node
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

Instance Method Details

#==(other) ⇒ Object



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

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

#body_nodeObject



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

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



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

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

#nameObject



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

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