class RuboCop::Cop::VariableForce::Assignment

This class represents each assignment of a variable.

Constants

MULTIPLE_LEFT_HAND_SIDE_TYPE
REFERENCE_PENETRABLE_BRANCH_TYPES

Attributes

node[R]
referenced[R]
referenced?[R]
variable[R]

Public Class Methods

new(node, variable) click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 16
def initialize(node, variable)
  unless VARIABLE_ASSIGNMENT_TYPES.include?(node.type)
    raise ArgumentError,
          "Node type must be any of #{VARIABLE_ASSIGNMENT_TYPES}, "                    "passed #{node.type}"
  end

  @node = node
  @variable = variable
  @referenced = false
end

Public Instance Methods

meta_assignment_node() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 67
def meta_assignment_node
  unless instance_variable_defined?(:@meta_assignment_node)
    @meta_assignment_node =
      operator_assignment_node || multiple_assignment_node
  end

  @meta_assignment_node
end
multiple_assignment?() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 57
def multiple_assignment?
  return false unless meta_assignment_node
  meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
end
name() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 28
def name
  @node.children.first
end
operator() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 62
def operator
  assignment_node = meta_assignment_node || @node
  assignment_node.loc.operator.source
end
operator_assignment?() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 52
def operator_assignment?
  return false unless meta_assignment_node
  OPERATOR_ASSIGNMENT_TYPES.include?(meta_assignment_node.type)
end
reference!() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 36
def reference!
  @referenced = true
end
reference_penetrable?() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 44
def reference_penetrable?
  REFERENCE_PENETRABLE_BRANCH_TYPES.include?(branch_type)
end
regexp_named_capture?() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 48
def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end
scope() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 32
def scope
  @variable.scope
end
used?() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 40
def used?
  @variable.captured_by_block? || @referenced
end

Private Instance Methods

multiple_assignment_node() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 85
def multiple_assignment_node
  grandparent_node = node.parent ? node.parent.parent : nil
  return nil unless grandparent_node
  return nil unless grandparent_node.type == MULTIPLE_ASSIGNMENT_TYPE
  return nil unless node.parent.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
  grandparent_node
end
operator_assignment_node() click to toggle source
# File lib/rubocop/cop/variable_force/assignment.rb, line 78
def operator_assignment_node
  return nil unless node.parent
  return nil unless OPERATOR_ASSIGNMENT_TYPES.include?(node.parent.type)
  return nil unless node.parent.children.index(node) == 0
  node.parent
end