Sha256: 0bd31fb82bf9450fbf42cd613667c43b270507c44c466409c26a12b434f4294c

Contents?: true

Size: 953 Bytes

Versions: 3

Compression:

Stored size: 953 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)

          super
        end

        def on_until(node)
          handle(node)

          super
        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)
          remove(node.loc.begin)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/while_until_do.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/while_until_do.rb
rubocop-0.9.0 lib/rubocop/cop/style/while_until_do.rb