Sha256: 675dfb69ee71c64ecb0a144c0abb220633a1ff6fcbf9ede426284189f746a335

Contents?: true

Size: 1.02 KB

Versions: 11

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of `do` in multi-line `while/until` statements.
      class WhileUntilDo < Cop
        def on_while(node)
          handle(node)
        end

        def on_until(node)
          handle(node)
        end

        def handle(node)
          length = node.source.lines.to_a.size
          return unless length > 1
          return unless node.loc.begin && node.loc.begin.is?('do')

          add_offense(node, :begin, error_message(node.type))
        end

        private

        def error_message(node_type)
          format('Do not use `do` with multi-line `%s`.', node_type)
        end

        def autocorrect(node)
          condition_node, = *node
          end_of_condition_range = condition_node.source_range.end
          do_range = node.loc.begin
          whitespaces_and_do_range = end_of_condition_range.join(do_range)
          ->(corrector) { corrector.remove(whitespaces_and_do_range) }
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/while_until_do.rb
rubocop-0.46.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.45.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.44.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.44.0 lib/rubocop/cop/style/while_until_do.rb