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

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Scope.



22
23
24
25
26
27
28
29
30
31
32
33
# 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_levelObject (readonly) Also known as: naked_top_level?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def naked_top_level
  @naked_top_level
end

#nodeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def node
  @node
end

#variablesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def variables
  @variables
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#body_nodeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:



61
62
63
64
65
66
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


57
58
59
# 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

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def name
  @node.method_name
end