Sha256: a2bdcb024184ea32934d5e10d62187f6657ccd54ecf3099d3b1374df6411bfd9

Contents?: true

Size: 1.88 KB

Versions: 17

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for uses of `begin...end while/until something`.
      #
      # @example
      #
      #   # bad
      #
      #   # using while
      #   begin
      #     do_something
      #   end while some_condition
      #
      # @example
      #
      #   # bad
      #
      #   # using until
      #   begin
      #     do_something
      #   end until some_condition
      #
      # @example
      #
      #   # good
      #
      #   # while replacement
      #   loop do
      #     do_something
      #     break unless some_condition
      #   end
      #
      # @example
      #
      #   # good
      #
      #   # until replacement
      #   loop do
      #     do_something
      #     break if some_condition
      #   end
      class Loop < Base
        extend AutoCorrector

        MSG = 'Use `Kernel#loop` with `break` rather than ' \
              '`begin/end/until`(or `while`).'

        def on_while_post(node)
          register_offense(node)
        end

        def on_until_post(node)
          register_offense(node)
        end

        private

        def register_offense(node)
          body = node.body

          add_offense(node.loc.keyword) do |corrector|
            corrector.replace(body.loc.begin, 'loop do')
            corrector.remove(keyword_and_condition_range(node))
            corrector.insert_before(body.loc.end, build_break_line(node))
          end
        end

        def keyword_and_condition_range(node)
          node.body.loc.end.end.join(node.source_range.end)
        end

        def build_break_line(node)
          conditional_keyword = node.while_post_type? ? 'unless' : 'if'
          "break #{conditional_keyword} #{node.condition.source}\n#{indent(node)}"
        end

        def indent(node)
          ' ' * node.loc.column
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/loop.rb
rubocop-1.1.0 lib/rubocop/cop/lint/loop.rb
rubocop-1.0.0 lib/rubocop/cop/lint/loop.rb
rubocop-0.93.1 lib/rubocop/cop/lint/loop.rb
rubocop-0.93.0 lib/rubocop/cop/lint/loop.rb
rubocop-0.92.0 lib/rubocop/cop/lint/loop.rb
rubocop-0.91.1 lib/rubocop/cop/lint/loop.rb
rubocop-0.91.0 lib/rubocop/cop/lint/loop.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/lint/loop.rb
rubocop-0.90.0 lib/rubocop/cop/lint/loop.rb
rubocop-0.89.1 lib/rubocop/cop/lint/loop.rb
rubocop-0.89.0 lib/rubocop/cop/lint/loop.rb