class RuboCop::Cop::ForToEachCorrector
This class auto-corrects `for` iteration to `#each` enumeration.
Constants
- CORRECTION
Attributes
collection_node[R]
for_node[R]
variable_node[R]
Public Class Methods
new(for_node)
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 11 def initialize(for_node) @for_node = for_node @variable_node = for_node.variable @collection_node = for_node.collection end
Public Instance Methods
call(corrector)
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 17 def call(corrector) corrector.replace(offending_range, correction) end
Private Instance Methods
collection_end()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 54 def collection_end if collection_node.begin_type? collection_node.loc.end else collection_node.loc.expression end end
collection_source()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 30 def collection_source if requires_parentheses? "(#{collection_node.source})" else collection_node.source end end
correction()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 25 def correction format(CORRECTION, collection: collection_source, argument: variable_node.source) end
end_position()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 42 def end_position if for_node.do? keyword_begin.end_pos else collection_end.end_pos end end
keyword_begin()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 50 def keyword_begin for_node.loc.begin end
offending_range()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 62 def offending_range replacement_range(end_position) end
replacement_range(end_pos)
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 66 def replacement_range(end_pos) Parser::Source::Range.new(for_node.loc.expression.source_buffer, for_node.loc.expression.begin_pos, end_pos) end
requires_parentheses?()
click to toggle source
# File lib/rubocop/cop/correctors/for_to_each_corrector.rb, line 38 def requires_parentheses? collection_node.range_type? end