Sha256: 5b1f4f358d1c1abff19fd5f1bc5717af710b989d926310f5ba0d6b1fecf40842

Contents?: true

Size: 993 Bytes

Versions: 4

Compression:

Stored size: 993 Bytes

Contents

# encoding: utf-8

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.loc.expression.source.lines.to_a.size

          if length > 1
            if node.loc.begin && node.loc.begin.is?('do')
              add_offence(:convention,
                          node.loc.begin,
                          error_message(node.type))
              do_autocorrect(node)
            end
          end
        end

        private

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

        def autocorrect_action(node)
          @corrections << lambda do |corrector|
            corrector.remove(node.loc.begin)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.11.1 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.11.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.10.0 lib/rubocop/cop/style/while_until_do.rb