Sha256: c96ca2a393c2e641363a31b798a53617ff1f057770ae400bbb74d1f11f369996

Contents?: true

Size: 1.21 KB

Versions: 62

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of `do` in multi-line `while/until` statements.
      #
      # @example
      #
      #   # bad
      #   while x.any? do
      #     do_something(x.pop)
      #   end
      #
      #   # good
      #   while x.any?
      #     do_something(x.pop)
      #   end
      #
      # @example
      #
      #   # bad
      #   until x.empty? do
      #     do_something(x.pop)
      #   end
      #
      #   # good
      #   until x.empty?
      #     do_something(x.pop)
      #   end
      class WhileUntilDo < Cop
        MSG = 'Do not use `do` with multi-line `%<keyword>s`.'

        def on_while(node)
          handle(node)
        end

        def on_until(node)
          handle(node)
        end

        def handle(node)
          return unless node.multiline? && node.do?

          add_offense(node, location: :begin,
                            message: format(MSG, keyword: node.keyword))
        end

        def autocorrect(node)
          do_range = node.condition.source_range.end.join(node.loc.begin)

          lambda do |corrector|
            corrector.remove(do_range)
          end
        end
      end
    end
  end
end

Version data entries

62 entries across 43 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/while_until_do.rb
rubocop-0.89.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.89.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.88.0 lib/rubocop/cop/style/while_until_do.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.87.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.87.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.86.0 lib/rubocop/cop/style/while_until_do.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/while_until_do.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/while_until_do.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.85.1 lib/rubocop/cop/style/while_until_do.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.85.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.84.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.83.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.82.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.81.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.80.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.80.0 lib/rubocop/cop/style/while_until_do.rb