Sha256: f6a8957181b58f4a5348ef066f76d0395fd18318aa7f7223c5ec51f8f074dc5c

Contents?: true

Size: 1.22 KB

Versions: 6791

Compression:

Stored size: 1.22 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`.'.freeze

        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

6,791 entries across 6,785 versions & 25 rubygems

Version Path
rubocop-0.58.0 lib/rubocop/cop/style/while_until_do.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/while_until_do.rb
rubocop-0.57.2 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.57.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.57.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.56.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.55.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.54.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.53.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.52.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.52.0 lib/rubocop/cop/style/while_until_do.rb