Class: RuboCop::Cop::VariableForce::Assignment
- Inherits:
-
Object
- Object
- RuboCop::Cop::VariableForce::Assignment
show all
- Includes:
- Locatable
- Defined in:
- lib/rubocop/cop/variable_force/assignment.rb
Overview
This class represents each assignment of a variable.
Constant Summary
collapse
- MULTIPLE_LEFT_HAND_SIDE_TYPE =
:mlhs
- REFERENCE_PENETRABLE_BRANCH_TYPES =
%w(rescue_main ensure_main).freeze
Constants included
from Locatable
Locatable::BRANCH_TYPES, Locatable::CONDITION_INDEX_OF_BRANCH_NODE, Locatable::ENSURE_INDEX_OF_ENSURE_NODE, Locatable::ENSURE_TYPE, Locatable::LEFT_SIDE_INDEX_OF_LOGICAL_OPERATOR_NODE, Locatable::LOGICAL_OPERATOR_TYPES
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, variable) ⇒ Assignment
Returns a new instance of Assignment.
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 16
def initialize(node, variable)
unless VARIABLE_ASSIGNMENT_TYPES.include?(node.type)
fail ArgumentError,
"Node type must be any of #{VARIABLE_ASSIGNMENT_TYPES}, " \
"passed #{node.type}"
end
@node = node
@variable = variable
@referenced = false
end
|
Instance Attribute Details
#node ⇒ Object
Returns the value of attribute node.
13
14
15
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 13
def node
@node
end
|
#referenced ⇒ Object
Also known as:
referenced?
Returns the value of attribute referenced.
13
14
15
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 13
def referenced
@referenced
end
|
#variable ⇒ Object
Returns the value of attribute variable.
13
14
15
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 13
def variable
@variable
end
|
Instance Method Details
67
68
69
70
71
72
73
74
|
# 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? ⇒ Boolean
57
58
59
60
|
# 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 ⇒ Object
28
29
30
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 28
def name
@node.children.first
end
|
#operator ⇒ Object
62
63
64
65
|
# 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? ⇒ Boolean
52
53
54
55
|
# 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! ⇒ Object
36
37
38
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 36
def reference!
@referenced = true
end
|
#reference_penetrable? ⇒ Boolean
#regexp_named_capture? ⇒ Boolean
48
49
50
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 48
def regexp_named_capture?
@node.type == REGEXP_NAMED_CAPTURE_TYPE
end
|
#scope ⇒ Object
32
33
34
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 32
def scope
@variable.scope
end
|
#used? ⇒ Boolean
40
41
42
|
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 40
def used?
@variable.captured_by_block? || @referenced
end
|