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.



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

  super
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



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

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)


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

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

#nameObject



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

def name
  @node.children.first
end

#operatorObject



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

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

#operator_assignment?Boolean

Returns:

  • (Boolean)


54
55
56
57
# 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

#reference!Object



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

def reference!
  @referenced = true
end

#reference_penetrable?Boolean

Returns:

  • (Boolean)


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

def reference_penetrable?
  REFERENCE_PENETRABLE_BRANCH_TYPES.include?(branch_type)
end

#regexp_named_capture?Boolean

Returns:

  • (Boolean)


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

def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end

#scopeObject



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

def scope
  @variable.scope
end

#used?Boolean

Returns:

  • (Boolean)


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

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