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

Inherits:
Object
  • Object
show all
Includes:
Branchable
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Branchable

#branch, #run_exclusively_with?

Constructor Details

#initialize(node, variable) ⇒ Assignment

Returns a new instance of Assignment.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 15

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 = []
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#referencedObject (readonly) Also known as: referenced?

Returns the value of attribute referenced.



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

def referenced
  @referenced
end

#referencesObject (readonly)

Returns the value of attribute references.



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

def references
  @references
end

#variableObject (readonly)

Returns the value of attribute variable.



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

def variable
  @variable
end

Instance Method Details

#meta_assignment_nodeObject



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

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)


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

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



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

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

#operator_assignment?Boolean

Returns:

  • (Boolean)


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

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

#reference!(node) ⇒ Object



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

def reference!(node)
  @references << node
  @referenced = true
end

#regexp_named_capture?Boolean

Returns:

  • (Boolean)


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

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)


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

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