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

Inherits:
Object
  • Object
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::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, variable) ⇒ Assignment

Returns a new instance of Assignment.



17
18
19
20
21
22
23
24
25
26
27
# 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
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#referencedObject (readonly) Also known as: referenced?

Returns the value of attribute referenced.



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

def referenced
  @referenced
end

#variableObject (readonly)

Returns the value of attribute variable.



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

def variable
  @variable
end

Instance Method Details

#meta_assignment_nodeObject



68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 68

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

Returns:

  • (Boolean)


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

def multiple_assignment?
  return false unless meta_assignment_node
  meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
end

#nameObject



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

def name
  @node.children.first
end

#operatorObject



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

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

#operator_assignment?Boolean

Returns:

  • (Boolean)


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

def operator_assignment?
  return false unless meta_assignment_node
  OPERATOR_ASSIGNMENT_TYPES.include?(meta_assignment_node.type)
end

#reference!Object



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

def reference!
  @referenced = true
end

#reference_penetrable?Boolean

Returns:

  • (Boolean)


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

def reference_penetrable?
  REFERENCE_PENETRABLE_BRANCH_TYPES.include?(branch_type)
end

#regexp_named_capture?Boolean

Returns:

  • (Boolean)


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

def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end

#scopeObject



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

def scope
  @variable.scope
end

#used?Boolean

Returns:

  • (Boolean)


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

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