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

#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



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

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)


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

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

#nameObject



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

def name
  @node.children.first
end

#operatorObject



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

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

#operator_assignment?Boolean

Returns:

  • (Boolean)


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

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

#reference!Object



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

def reference!
  @referenced = true
end

#regexp_named_capture?Boolean

Returns:

  • (Boolean)


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

def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end

#scopeObject



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

def scope
  @variable.scope
end

#used?Boolean

Returns:

  • (Boolean)


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

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