Sha256: 86a2b887820ddf799beb4485cc0ff1c3e5a35647e06ad4d35608245f89b14d43

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Use `Kernel#loop` for infinite loops.
      #
      # @example
      #   # bad
      #   while true
      #     work
      #   end
      #
      #   # good
      #   loop do
      #     work
      #   end
      class InfiniteLoop < Cop
        MSG = 'Use `Kernel#loop` for infinite loops.'

        TRUTHY_LITERALS = [:str, :dstr, :int, :float, :array,
                           :hash, :regexp, :true]

        FALSEY_LITERALS = [:nil, :false]

        def on_while(node)
          condition, = *node

          return unless TRUTHY_LITERALS.include?(condition.type)

          add_offense(node, :keyword)
        end

        def on_until(node)
          condition, = *node

          return unless FALSEY_LITERALS.include?(condition.type)

          add_offense(node, :keyword)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            condition_node, = *node
            start_range = node.loc.keyword.begin
            end_range = if node.loc.begin
                          node.loc.begin.end
                        else
                          condition_node.loc.expression.end
                        end
            corrector.replace(start_range.join(end_range), 'loop do')
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/infinite_loop.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.30.1 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.30.0 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.29.1 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.29.0 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.28.0 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.27.1 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.27.0 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.26.1 lib/rubocop/cop/style/infinite_loop.rb
rubocop-0.26.0 lib/rubocop/cop/style/infinite_loop.rb