Sha256: 49f393a9ec18df1ebaa20e85cdd680da62ecbfd9f02acd60a7c94f3699338212

Contents?: true

Size: 1.37 KB

Versions: 22

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8

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

        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 block_length(node) == 0

          method, _args, _body = *node
          return unless method.type == :send

          _receiver, method_name, *args = *method
          return unless method_name == :each && args.empty?

          if style == :for
            end_pos = method.loc.expression.end_pos
            range = Parser::Source::Range.new(processed_source.buffer,
                                              end_pos - 'each'.length,
                                              end_pos)
            add_offense(range, range, 'Prefer `for` over `each`.') do
              opposite_style_detected
            end
          else
            correct_style_detected
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/for.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/for.rb
rubocop-0.34.2 lib/rubocop/cop/style/for.rb
rubocop-0.34.1 lib/rubocop/cop/style/for.rb
rubocop-0.34.0 lib/rubocop/cop/style/for.rb
rubocop-0.33.0 lib/rubocop/cop/style/for.rb
rubocop-0.32.1 lib/rubocop/cop/style/for.rb
rubocop-0.32.0 lib/rubocop/cop/style/for.rb
rubocop-0.31.0 lib/rubocop/cop/style/for.rb
rubocop-0.30.1 lib/rubocop/cop/style/for.rb
rubocop-0.30.0 lib/rubocop/cop/style/for.rb
rubocop-0.29.1 lib/rubocop/cop/style/for.rb
rubocop-0.29.0 lib/rubocop/cop/style/for.rb
rubocop-0.28.0 lib/rubocop/cop/style/for.rb
rubocop-0.27.1 lib/rubocop/cop/style/for.rb
rubocop-0.27.0 lib/rubocop/cop/style/for.rb
rubocop-0.26.1 lib/rubocop/cop/style/for.rb
rubocop-0.26.0 lib/rubocop/cop/style/for.rb
rubocop-0.25.0 lib/rubocop/cop/style/for.rb
rubocop-0.24.1 lib/rubocop/cop/style/for.rb