Class: RuboCop::Cop::VariableForce::Reference

Inherits:
Object
  • Object
show all
Includes:
Locatable
Defined in:
lib/rubocop/cop/variable_force/reference.rb

Overview

This class represents each reference of a variable.

Constant Summary collapse

VARIABLE_REFERENCE_TYPES =
(
  [VARIABLE_REFERENCE_TYPE] +
  OPERATOR_ASSIGNMENT_TYPES +
  [ZERO_ARITY_SUPER_TYPE, SEND_TYPE]
).freeze

Constants included from Locatable

Locatable::BRANCH_TYPES, Locatable::CONDITION_INDEX_OF_BRANCH_NODE, Locatable::ENSURE_INDEX_OF_ENSURE_NODE, Locatable::ENSURE_TYPE, Locatable::FOR_LOOP_CHILD_INDEX, Locatable::FOR_LOOP_TYPE, Locatable::LEFT_SIDE_INDEX_OF_LOGICAL_OPERATOR_NODE, Locatable::LOGICAL_OPERATOR_TYPES, Locatable::NON_FOR_LOOP_TYPES, Locatable::NON_FOR_LOOP_TYPES_CHILD_INDEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Locatable

#branch_body_name, #branch_body_node, #branch_id, #branch_point_node, #branch_type, #inside_of_branch?, #run_exclusively_with?

Constructor Details

#initialize(node, scope) ⇒ Reference

Returns a new instance of Reference.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/cop/variable_force/reference.rb', line 18

def initialize(node, scope)
  unless VARIABLE_REFERENCE_TYPES.include?(node.type)
    raise ArgumentError,
          "Node type must be any of #{VARIABLE_REFERENCE_TYPES}, " \
          "passed #{node.type}"
  end

  @node = node
  @scope = scope

  super
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



16
17
18
# File 'lib/rubocop/cop/variable_force/reference.rb', line 16

def node
  @node
end

#scopeObject (readonly)

Returns the value of attribute scope.



16
17
18
# File 'lib/rubocop/cop/variable_force/reference.rb', line 16

def scope
  @scope
end

Instance Method Details

#explicit?Boolean

There’s an implicit variable reference by the zero-arity ‘super`:

def some_method(foo)
  super
end

Another case is ‘binding`:

def some_method(foo)
  do_something(binding)
end

In these cases, the variable ‘foo` is not explicitly referenced, but it can be considered used implicitly by the `super` or `binding`.

Returns:

  • (Boolean)


45
46
47
# File 'lib/rubocop/cop/variable_force/reference.rb', line 45

def explicit?
  ![ZERO_ARITY_SUPER_TYPE, SEND_TYPE].include?(@node.type)
end