Sha256: 39abc7d1b105662177a4045570068f3463fbaa390b8f2d66be6d2176c67d7754

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop 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, however.
      class For < Cop
        include ConfigurableEnforcedStyle
        EACH_LENGTH = 'each'.length

        def on_for(node)
          if style == :each
            add_offense(node, :keyword, 'Prefer `each` over `for`.') do
              opposite_style_detected
            end
          else
            correct_style_detected
          end
        end

        def on_block(node)
          return if node.single_line?

          return unless node.send_node.method?(:each) &&
                        !node.send_node.arguments?

          if style == :for
            incorrect_style_detected(node.send_node)
          else
            correct_style_detected
          end
        end

        private

        def incorrect_style_detected(method)
          end_pos = method.source_range.end_pos
          range = range_between(end_pos - EACH_LENGTH, end_pos)
          add_offense(range, range, 'Prefer `for` over `each`.') do
            opposite_style_detected
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.50.0 lib/rubocop/cop/style/for.rb
rubocop-0.49.1 lib/rubocop/cop/style/for.rb
rubocop-0.49.0 lib/rubocop/cop/style/for.rb