Class: RuboCop::Cop::VariableForce::Scope Private
Relationships & Source Files | |
Inherits: | Object |
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
-
OUTER_SCOPE_CHILD_INDICES =
# File 'lib/rubocop/cop/variable_force/scope.rb', line 10{ defs: 0..0, module: 0..0, class: 0..1, sclass: 0..0, block: 0..0 }.freeze
Class Method Summary
- .new(node) ⇒ Scope constructor Internal use only
Instance Attribute Summary
-
#naked_top_level?
readonly
Alias for #naked_top_level.
- #node readonly Internal use only
- #variables readonly Internal use only
Instance Method Summary
- #==(other) Internal use only
- #body_node Internal use only
- #each_node {|node| ... } Internal use only
- #include?(target_node) ⇒ Boolean Internal use only
- #naked_top_level (also: #naked_top_level?) readonly Internal use only
- #name Internal use only
- #ancestor_node?(target_node) ⇒ Boolean private Internal use only
- #belong_to_inner_scope?(target_node) ⇒ Boolean private Internal use only
- #belong_to_outer_scope?(target_node) ⇒ Boolean private Internal use only
- #scan_node(node, &block) private Internal use only
Constructor Details
.new(node) ⇒ Scope
# File 'lib/rubocop/cop/variable_force/scope.rb', line 22
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
Instance Attribute Details
#naked_top_level? (readonly)
Alias for #naked_top_level.
# File 'lib/rubocop/cop/variable_force/scope.rb', line 20
alias naked_top_level? naked_top_level
#node (readonly)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/scope.rb', line 18
attr_reader :node, :variables, :naked_top_level
#variables (readonly)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/scope.rb', line 18
attr_reader :node, :variables, :naked_top_level
Instance Method Details
#==(other)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/scope.rb', line 35
def ==(other) @node.equal?(other.node) end
#ancestor_node?(target_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/variable_force/scope.rb', line 100
def ancestor_node?(target_node) node.each_ancestor.any? { |ancestor_node| ancestor_node.equal?(target_node) } end
#belong_to_inner_scope?(target_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/variable_force/scope.rb', line 90
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
#belong_to_outer_scope?(target_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/variable_force/scope.rb', line 79
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
#body_node
[ GitHub ]# File 'lib/rubocop/cop/variable_force/scope.rb', line 43
def body_node if naked_top_level? node else child_index = case node.type when :module, :sclass then 1 when :def, :class, :block, :numblock then 2 when :defs then 3 end node.children[child_index] end end
#each_node {|node| ... }
# File 'lib/rubocop/cop/variable_force/scope.rb', line 61
def each_node(&block) return to_enum(__method__) unless block yield node if naked_top_level? scan_node(node, &block) end
#include?(target_node) ⇒ Boolean
# File 'lib/rubocop/cop/variable_force/scope.rb', line 57
def include?(target_node) !belong_to_outer_scope?(target_node) && !belong_to_inner_scope?(target_node) end
#naked_top_level (readonly) Also known as: #naked_top_level?
[ GitHub ]#name
[ GitHub ]# File 'lib/rubocop/cop/variable_force/scope.rb', line 39
def name @node.method_name end