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
-
MULTIPLE_LEFT_HAND_SIDE_TYPE =
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 10:mlhs
Class Method Summary
- .new(node, variable) ⇒ Assignment constructor Internal use only
Instance Attribute Summary
- #exception_assignment? ⇒ Boolean readonly Internal use only
- #for_assignment? ⇒ Boolean readonly Internal use only
- #multiple_assignment? ⇒ Boolean readonly Internal use only
- #node readonly Internal use only
- #operator_assignment? ⇒ Boolean readonly Internal use only
-
#reassigned?
readonly
Alias for #reassigned.
-
#referenced?
readonly
Alias for #referenced.
- #references readonly Internal use only
- #regexp_named_capture? ⇒ Boolean readonly Internal use only
- #rest_assignment? ⇒ Boolean readonly Internal use only
- #used? ⇒ Boolean readonly Internal use only
- #variable readonly Internal use only
Instance Method Summary
- #meta_assignment_node Internal use only
- #name Internal use only
- #operator Internal use only
- #reassigned (also: #reassigned?) readonly Internal use only
- #reassigned! Internal use only
- #reference!(node) Internal use only
- #referenced (also: #referenced?) readonly Internal use only
- #scope Internal use only
- #find_multiple_assignment_node(grandparent_node) private Internal use only
- #for_assignment_node private Internal use only
- #multiple_assignment_node private Internal use only
- #operator_assignment_node private Internal use only
- #rest_assignment_node private Internal use only
Branchable
- Included
Constructor Details
.new(node, variable) ⇒ Assignment
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 17
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 = [] @reassigned = false end
Instance Attribute Details
#exception_assignment? ⇒ Boolean
(readonly)
#for_assignment? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 80
def for_assignment? return false unless .for_type? end
#multiple_assignment? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 68
def multiple_assignment? return false unless .type == MULTIPLE_ASSIGNMENT_TYPE end
#node (readonly)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12
attr_reader :node, :variable, :referenced, :references, :reassigned
#operator_assignment? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 62
def operator_assignment? return false unless OPERATOR_ASSIGNMENT_TYPES.include?( .type) end
#reassigned? (readonly)
Alias for #reassigned.
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 15
alias reassigned? reassigned
#referenced? (readonly)
Alias for #referenced.
# 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, :reassigned
#regexp_named_capture? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 54
def regexp_named_capture? @node.type == REGEXP_NAMED_CAPTURE_TYPE end
#rest_assignment? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 74
def rest_assignment? return false unless .type == REST_ASSIGNMENT_TYPE end
#used? ⇒ Boolean
(readonly)
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 50
def used? (!reassigned? && @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, :reassigned
Instance Method Details
#find_multiple_assignment_node(grandparent_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 140
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 130
def for_assignment_node return unless (parent_node = node.parent) return parent_node if parent_node.for_type? grandparent_node = parent_node.parent return grandparent_node if parent_node.mlhs_type? && grandparent_node&.for_type? nil end
#meta_assignment_node
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 91
def 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 112
def multiple_assignment_node return nil unless node.parent&.mlhs_type? 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 31
def name @node.children.first end
#operator
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 86
def operator assignment_node = || @node assignment_node.loc.operator.source end
#operator_assignment_node (private)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 104
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
#reassigned (readonly) Also known as: #reassigned?
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12
attr_reader :node, :variable, :referenced, :references, :reassigned
#reassigned!
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 44
def reassigned! return if referenced? @reassigned = true end
#reference!(node)
[ GitHub ]#referenced (readonly) Also known as: #referenced?
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 12
attr_reader :node, :variable, :referenced, :references, :reassigned
#rest_assignment_node (private)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/assignment.rb', line 123
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 35
def scope @variable.scope end