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.
Constant Summary
-
CORRECTION =
# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 9'%<collection>s.each do |%<argument>s|'
Class Method Summary
- .new(for_node) ⇒ ForToEachCorrector constructor
Instance Attribute Summary
- #collection_node readonly private
- #for_node readonly private
- #requires_parentheses? ⇒ Boolean readonly private
- #variable_node readonly private
Instance Method Summary
- #call(corrector)
- #collection_end private
- #collection_source private
- #correction private
- #end_range private
- #keyword_begin private
Constructor Details
.new(for_node) ⇒ ForToEachCorrector
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 39
def requires_parentheses? return true if collection_node.send_type? && collection_node.operator_method? collection_node.range_type? || collection_node.operator_keyword? 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
#call(corrector)
[ GitHub ]# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 17
def call(corrector) offending_range = for_node.source_range.begin.join(end_range) corrector.replace(offending_range, correction) end
#collection_end (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 57
def collection_end if collection_node.begin_type? collection_node.loc.end else collection_node.source_range end end
#collection_source (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 31
def collection_source if requires_parentheses? "(#{collection_node.source})" else collection_node.source end end
#correction (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 27
def correction format(CORRECTION, collection: collection_source, argument: variable_node.source) end
#end_range (private)
[ GitHub ]# File 'lib/rubocop/cop/correctors/for_to_each_corrector.rb', line 45
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 53
def keyword_begin for_node.loc.begin end