Sha256: 42e71b0ef75d1fcd60f8d4931cacbe5bdf7becc0fc7bc13652cce5987b2c7340

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 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
          oneline = "#{body.loc.expression.source} " \
                    "#{node.loc.keyword.source} " +
                    cond.loc.expression.source
          ->(corrector) { corrector.replace(node.loc.expression, oneline) }
        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

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.35.0 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.34.2 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.34.1 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.34.0 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.33.0 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.32.1 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.32.0 lib/rubocop/cop/style/while_until_modifier.rb
rubocop-0.31.0 lib/rubocop/cop/style/while_until_modifier.rb