Sha256: deacb9f991af7151e97d61c1e24ffe9716caa8f191639a3e482b9178f3ed853f

Contents?: true

Size: 1.04 KB

Versions: 14

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8
# 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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/while_until_do.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/while_until_do.rb
rubocop-0.43.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.42.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.41.2 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.41.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.41.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.40.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.39.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.38.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.37.2 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.37.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.37.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.36.0 lib/rubocop/cop/style/while_until_do.rb