123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::ParallelAssignment

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/parallel_assignment.rb

Overview

Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.

Examples:

# bad
a, b, c = 1, 2, 3
a, b, c = [1, 2, 3]

# good
one, two = *foo
a, b = foo()
a, b = b, a

a = 1
b = 2
c = 3

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RescueNode - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?,
#target_gem_version

Returns a gems locked versions (i.e.

#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#add_self_to_getters(right_elements) (private)

Converts (send nil :something) nodes to (send (:self) :something). This makes the sorting algorithm work for expressions such as self.a, self.b = b, a.

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 112

def add_self_to_getters(right_elements)
  right_elements.map do |e|
    implicit_self_getter?(e) { |var| s(:send, s(:self), var) } || e
  end
end

#allowed_lhs?(elements) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 61

def allowed_lhs?(elements)
  # Account for edge cases using one variable with a comma
  # E.g.: `foo, = *bar`
  elements.one? || elements.any?(&:splat_type?)
end

#allowed_masign?(lhs_elements, rhs_elements) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 55

def allowed_masign?(lhs_elements, rhs_elements)
  lhs_elements.size != rhs_elements.size ||
    !find_valid_order(lhs_elements,
                      add_self_to_getters(rhs_elements))
end

#allowed_rhs?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 67

def allowed_rhs?(node)
  # Edge case for one constant
  elements = Array(node).compact

  # Account for edge case of `Constant::CONSTANT`
  !node.array_type? || return_of_method_call?(node) || elements.any?(&:splat_type?)
end

#assignment_corrector(node, rhs, order) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 79

def assignment_corrector(node, rhs, order)
  if node.parent&.rescue_type?
    _assignment, modifier = *node.parent
  else
    _assignment, modifier = *rhs.parent
  end

  if modifier_statement?(node.parent)
    ModifierCorrector.new(node, rhs, modifier, config, order)
  elsif rescue_modifier?(modifier)
    RescueCorrector.new(node, rhs, modifier, config, order)
  else
    GenericCorrector.new(node, rhs, modifier, config, order)
  end
end

#autocorrect(corrector, node, rhs) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 48

def autocorrect(corrector, node, rhs)
  order = find_valid_order(node.assignments, Array(rhs).compact)
  correction = assignment_corrector(node, rhs, order)

  corrector.replace(correction.correction_range, correction.correction)
end

#find_valid_order(left_elements, right_elements) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 95

def find_valid_order(left_elements, right_elements)
  # arrange left_elements in an order such that no corresponding right
  # element refers to a left element earlier in the sequence
  # this can be done using an algorithm called a "topological sort"
  # fortunately for us, Ruby's stdlib contains an implementation
  assignments = left_elements.zip(right_elements)

  begin
    AssignmentSorter.new(assignments).tsort
  rescue TSort::Cyclic
    nil
  end
end

#implicit_self_getter?(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 119

def_node_matcher :implicit_self_getter?, '(send nil? $_)'

#modifier_statement?(node) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 178

def modifier_statement?(node)
  return false unless node

  node.basic_conditional? && node.modifier_form?
end

#on_masgn(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 31

def on_masgn(node) # rubocop:disable Metrics/AbcSize
  rhs = node.rhs
  rhs = rhs.body if rhs.rescue_type?
  rhs_elements = Array(rhs).compact # edge case for one constant

  return if allowed_lhs?(node.assignments) || allowed_rhs?(rhs) ||
            allowed_masign?(node.assignments, rhs_elements)

  range = node.source_range.begin.join(rhs.source_range.end)

  add_offense(range) do |corrector|
    autocorrect(corrector, node, rhs)
  end
end

#return_of_method_call?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 75

def return_of_method_call?(node)
  node.block_type? || node.send_type?
end