Sha256: 48af62411dfc1c1db11b876f59cbf76ab74fa6502e3c133f997bdddf984c2780

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Checks for while and until statements that would fit on one line
      # if written as a modifier while/until.
      # The maximum line length is configurable.
      class WhileUntilModifier < Cop
        include StatementModifier

        def on_while(node)
          check(node)
        end

        def on_until(node)
          check(node)
        end

        def autocorrect(node)
          cond, body = *node
          @corrections << lambda do |corrector|
            oneline = "#{body.loc.expression.source} " \
                      "#{node.loc.keyword.source} " +
                      cond.loc.expression.source
            corrector.replace(node.loc.expression, oneline)
          end
        end

        private

        def check(node)
          return unless node.loc.end
          return unless fit_within_line_as_modifier_form?(node)
          add_offense(node, :keyword, message(node.loc.keyword.source))
        end

        def message(keyword)
          "Favor modifier `#{keyword}` usage when having a single-line body."
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.30.0 lib/rubocop/cop/style/while_until_modifier.rb