Class: RuboCop::Cop::Style::For

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, RangeHelp
Defined in:
lib/rubocop/cop/style/for.rb

Overview

Looks for uses of the ‘for` keyword or `each` method. The preferred alternative is set in the EnforcedStyle configuration parameter. An `each` call with a block on a single line is always allowed.

Examples:

EnforcedStyle: each (default)

# bad
def foo
  for n in [1, 2, 3] do
    puts n
  end
end

# good
def foo
  [1, 2, 3].each do |n|
    puts n
  end
end

EnforcedStyle: for

# bad
def foo
  [1, 2, 3].each do |n|
    puts n
  end
end

# good
def foo
  for n in [1, 2, 3] do
    puts n
  end
end

Constant Summary collapse

EACH_LENGTH =
'each'.length
PREFER_EACH =
'Prefer `each` over `for`.'
PREFER_FOR =
'Prefer `for` over `each`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

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

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/style/for.rb', line 65

def on_block(node)
  return unless suspect_enumerable?(node)

  if style == :for
    return unless node.receiver

    add_offense(node, message: PREFER_FOR) do |corrector|
      EachToForCorrector.new(node).call(corrector)
      opposite_style_detected
    end
  else
    correct_style_detected
  end
end

#on_for(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/style/for.rb', line 54

def on_for(node)
  if style == :each
    add_offense(node, message: PREFER_EACH) do |corrector|
      ForToEachCorrector.new(node).call(corrector)
      opposite_style_detected
    end
  else
    correct_style_detected
  end
end