123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::ForToEachCorrector

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macros
Inherits: Object
Defined in: lib/rubocop/cop/correctors/for_to_each_corrector.rb

Overview

This class autocorrects for iteration to #each enumeration.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(for_node) ⇒ ForToEachCorrector

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 9

def initialize(for_node)
  @for_node        = for_node
  @variable_node   = for_node.variable
  @collection_node = for_node.collection
end

Instance Attribute Details

#collection_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 25

attr_reader :for_node, :variable_node, :collection_node

#for_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 25

attr_reader :for_node, :variable_node, :collection_node

#requires_parentheses? ⇒ Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 34

def requires_parentheses?
  return true if collection_node.send_type? && collection_node.operator_method?

  collection_node.range_type? || collection_node.operator_keyword?
end

#trailing_comma_required? ⇒ Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 80

def trailing_comma_required?
  variable_node.mlhs_type? && variable_node.children.one? && !variable_node.loc.begin
end

#variable_node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 25

attr_reader :for_node, :variable_node, :collection_node

Instance Method Details

#block_parameters (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 60

def block_parameters
  parameters = variable_node.source_range
  source = parameters.source

  trailing_commas_in_parentheses.reverse_each do |comma|
    source[comma.begin_pos - parameters.begin_pos, comma.size] = ''
  end
  source << ',' if trailing_comma_required?
  source
end

#call(corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 15

def call(corrector)
  corrector.replace(
    for_node.source_range.begin.join(collection_node.source_range.begin),
    requires_parentheses? ? '(' : ''
  )
  corrector.replace(collection_end.end.join(end_range), correction)
end

#collection_end (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 52

def collection_end
  if collection_node.begin_type?
    collection_node.loc.end
  else
    collection_node.source_range
  end
end

#correction (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 27

def correction
  closing_parenthesis = requires_parentheses? ? ')' : ''
  dot = collection_node.csend_type? ? '&.' : '.'

  "#{closing_parenthesis}#{dot}each do |#{block_parameters}|"
end

#end_range (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 40

def end_range
  if for_node.do?
    keyword_begin.end
  else
    collection_end.end
  end
end

#keyword_begin (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 48

def keyword_begin
  for_node.loc.begin
end

#trailing_commas_in_parentheses (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 71

def trailing_commas_in_parentheses
  variable_node.each_node(:mlhs).filter_map do |mlhs|
    next unless mlhs.loc_is?(:end, ')')

    gap = mlhs.children.last.source_range.end.join(mlhs.loc.end.begin)
    gap if gap.source.include?(',')
  end.sort_by(&:begin_pos)
end