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

Inherits:
Object
  • Object
show all
Includes:
Branchable
Defined in:
lib/rubocop/cop/variable_force/assignment.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class represents each assignment of a variable.

Constant Summary collapse

MULTIPLE_LEFT_HAND_SIDE_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

:mlhs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Branchable

#branch, #run_exclusively_with?

Constructor Details

#initialize(node, variable) ⇒ Assignment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Assignment.



16
17
18
19
20
21
22
23
24
25
26
27
# 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
  @references = []
end

Instance Attribute Details

#nodeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def node
  @node
end

#referencedObject (readonly) Also known as: referenced?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def referenced
  @referenced
end

#referencesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def references
  @references
end

#variableObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def variable
  @variable
end

Instance Method Details

#exception_assignment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def exception_assignment?
  node.parent&.resbody_type? && node.parent.exception_variable == node
end

#for_assignment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def for_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.for_type?
end

#meta_assignment_nodeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 83

def meta_assignment_node
  unless instance_variable_defined?(:@meta_assignment_node)
    @meta_assignment_node = operator_assignment_node ||
                            multiple_assignment_node ||
                            rest_assignment_node ||
                            for_assignment_node
  end

  @meta_assignment_node
end

#multiple_assignment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def multiple_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def name
  @node.children.first
end

#operatorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
# File 'lib/rubocop/cop/variable_force/assignment.rb', line 78

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

#operator_assignment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


54
55
56
57
58
# 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!(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#regexp_named_capture?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def regexp_named_capture?
  @node.type == REGEXP_NAMED_CAPTURE_TYPE
end

#rest_assignment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def rest_assignment?
  return false unless meta_assignment_node

  meta_assignment_node.type == REST_ASSIGNMENT_TYPE
end

#scopeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def scope
  @variable.scope
end

#used?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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