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::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

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#referencedObject (readonly) 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

#variableObject (readonly)

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

#meta_assignment_nodeObject



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

Returns:

  • (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

#nameObject



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

def name
  @node.children.first
end

#operatorObject



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

Returns:

  • (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

Returns:

  • (Boolean)


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

def reference_penetrable?
  REFERENCE_PENETRABLE_BRANCH_TYPES.include?(branch_type)
end

#regexp_named_capture?Boolean

Returns:

  • (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

#scopeObject



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

def scope
  @variable.scope
end

#used?Boolean

Returns:

  • (Boolean)


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

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