Class: RuboCop::Cop::VariableForce::Scope
- Inherits:
-
Object
- Object
- RuboCop::Cop::VariableForce::Scope
- 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
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #body_node ⇒ Object
- #each_node(&block) ⇒ Object
-
#initialize(node) ⇒ Scope
constructor
A new instance of Scope.
- #name ⇒ Object
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
#node ⇒ Object (readonly)
Returns the value of attribute node.
19 20 21 |
# File 'lib/rubocop/cop/variable_force/scope.rb', line 19 def node @node end |
#variables ⇒ Object (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_node ⇒ Object
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 |
#name ⇒ Object
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 |