class RuboCop::Cop::VariableForce::Scope
A scope instance holds a scope node and variable entries.
This is a place where local variables belong to.
A Scope represents a context of local variable visibility.
def ==(other)
def ==(other) @node.equal?(other.node) end
def ancestor_node?(target_node)
def ancestor_node?(target_node) node.each_ancestor.any? do |ancestor_node| ancestor_node.equal?(target_node) end end
def belong_to_inner_scope?(target_node)
def belong_to_inner_scope?(target_node) return false if !target_node.parent || target_node.parent.equal?(node) return false unless SCOPE_TYPES.include?(target_node.parent.type) indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type] return true unless indices !indices.include?(target_node.sibling_index) end
def belong_to_outer_scope?(target_node)
def belong_to_outer_scope?(target_node) return true if !naked_top_level? && target_node.equal?(node) return true if ancestor_node?(target_node) return false unless target_node.parent.equal?(node) indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type] return false unless indices indices.include?(target_node.sibling_index) end
def body_node
def body_node if naked_top_level? node else child_index = case node.type when :module, :sclass then 1 when :def, :class, :block then 2 when :defs then 3 end node.children[child_index] end end
def each_node(&block)
def each_node(&block) return to_enum(__method__) unless block_given? yield node if naked_top_level? scan_node(node, &block) end
def include?(target_node)
def include?(target_node) !belong_to_outer_scope?(target_node) && !belong_to_inner_scope?(target_node) end
def initialize(node)
def initialize(node) unless SCOPE_TYPES.include?(node.type) # Accept any node type for top level scope if node.parent raise ArgumentError, "Node type must be any of #{SCOPE_TYPES}, " \ "passed #{node.type}" end @naked_top_level = true end @node = node @variables = {} end
def name
def name @node.method_name end
def scan_node(node, &block)
def scan_node(node, &block) node.each_child_node do |child_node| next unless include?(child_node) yield child_node scan_node(child_node, &block) end end