123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/rubocop/cop/style/parallel_assignment.rb

Overview

An internal class for correcting parallel assignment

Constant Summary

::RuboCop::Cop::Alignment - Included

SPACE

Class Method Summary

Instance Attribute Summary

::RuboCop::Cop::Alignment - Included

Instance Method Summary

Constructor Details

.new(node, rhs, modifier, config, new_elements) ⇒ GenericCorrector

[ GitHub ]

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

def initialize(node, rhs, modifier, config, new_elements)
  @node = node
  @rhs = rhs
  _, _, @rescue_result = *modifier
  @config = config
  @new_elements = new_elements
end

Instance Attribute Details

#config (readonly)

[ GitHub ]

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

attr_reader :node, :rhs, :rescue_result, :config

#node (readonly)

[ GitHub ]

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

attr_reader :node, :rhs, :rescue_result, :config

#rescue_result (readonly)

[ GitHub ]

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

attr_reader :node, :rhs, :rescue_result, :config

#rhs (readonly)

[ GitHub ]

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

attr_reader :node, :rhs, :rescue_result, :config

Instance Method Details

#cop_config (private)

[ GitHub ]

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

def cop_config
  @config.for_cop('Style/ParallelAssignment')
end

#correction

[ GitHub ]

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

def correction
  assignment.join("\n#{offset(node)}")
end

#correction_range

[ GitHub ]

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

def correction_range
  node.source_range
end

#extract_sources(node) (private)

[ GitHub ]

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

def extract_sources(node)
  node.children.map(&:source)
end

#quote(string) (private)

[ GitHub ]

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

def quote(string)
  "'#{string.gsub(/[\\']/) { |char| "\\#{char}" }}'"
end

#source(node, loc) (private)

[ GitHub ]

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

def source(node, loc)
  # __FILE__ is treated as a StrNode but has no begin
  if node.str_type? && loc.respond_to?(:begin) && loc.begin.nil?
    # `%w` elements have no per-element delimiter, so the value must be
    # quoted and escaped to stay valid (e.g. `%w(it's)` -> `'it\'s'`).
    quote(node.value)
  elsif node.sym_type? && !node.loc?(:begin)
    # `%i` elements have no per-element delimiter, so a symbol that needs
    # quoting must be emitted as `:"..."` (e.g. `%i(foo-bar)` -> `:"foo-bar"`),
    # otherwise `:foo-bar` would parse as `:foo.-(bar)`.
    node.value.inspect
  else
    node.source
  end
end