123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::VariableForce::Assignment Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Branchable
Inherits: Object
Defined in: lib/rubocop/cop/variable_force/assignment.rb

Overview

This class represents each assignment of a variable.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Branchable - Included

Constructor Details

.new(node, variable) ⇒ Assignment

[ GitHub ]

  
# 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
  @references = []
end

Instance Attribute Details

#exception_assignment?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 50

def exception_assignment?
  node.parent&.resbody_type? && node.parent.exception_variable == node
end

#for_assignment?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 72

def for_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.for_type?
end

#multiple_assignment?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 60

def multiple_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
end

#node (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12

attr_reader :node, :variable, :referenced, :references

#operator_assignment?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 54

def operator_assignment?
  return false unless meta_assignment_node

  OPERATOR_ASSIGNMENT_TYPES.include?(meta_assignment_node.type)
end

#referenced? (readonly)

Alias for #referenced.

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 14

alias referenced? referenced

#references (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12

attr_reader :node, :variable, :referenced, :references

#regexp_named_capture?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 46

def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end

#rest_assignment?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 66

def rest_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.type == REST_ASSIGNMENT_TYPE
end

#used?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 42

def used?
  @variable.captured_by_block? || @referenced
end

#variable (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12

attr_reader :node, :variable, :referenced, :references

Instance Method Details

#find_multiple_assignment_node(grandparent_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 125

def find_multiple_assignment_node(grandparent_node)
  return unless grandparent_node.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
  return if grandparent_node.children.any?(&:splat_type?)

  parent = grandparent_node.parent
  return parent if parent.type == MULTIPLE_ASSIGNMENT_TYPE

  find_multiple_assignment_node(parent)
end

#for_assignment_node (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 121

def for_assignment_node
  node.ancestors.find(&:for_type?)
end

#meta_assignment_node

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 83

def meta_assignment_node
  unless instance_variable_defined?(:@meta_assignment_node)
    @meta_assignment_node = operator_assignment_node ||
                            multiple_assignment_node ||
                            rest_assignment_node ||
                            for_assignment_node
  end

  @meta_assignment_node
end

#multiple_assignment_node (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 104

def multiple_assignment_node
  return nil unless (grandparent_node = node.parent&.parent)
  if (node = find_multiple_assignment_node(grandparent_node))
    return node
  end
  return nil unless grandparent_node.type == MULTIPLE_ASSIGNMENT_TYPE

  grandparent_node
end

#name

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 29

def name
  @node.children.first
end

#operator

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 78

def operator
  assignment_node = meta_assignment_node || @node
  assignment_node.loc.operator.source
end

#operator_assignment_node (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 96

def operator_assignment_node
  return nil unless node.parent
  return nil unless OPERATOR_ASSIGNMENT_TYPES.include?(node.parent.type)
  return nil unless node.sibling_index.zero?

  node.parent
end

#reference!(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 37

def reference!(node)
  @references << node
  @referenced = true
end

#referenced (readonly) Also known as: #referenced?

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12

attr_reader :node, :variable, :referenced, :references

#rest_assignment_node (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 114

def rest_assignment_node
  return nil unless node.parent
  return nil unless node.parent.type == REST_ASSIGNMENT_TYPE

  node.parent
end

#scope

[ GitHub ]

  
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 33

def scope
  @variable.scope
end