Sha256: 82eaea3361b553da3739a7618de63fe1b4a3e681aad4221960871899af3485ee

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for conditions that are not on the same line as
      # if/while/until.
      #
      # @example
      #
      #   if
      #     some_condition
      #     do_something
      #   end
      class ConditionPosition < Cop
        def on_if(node)
          return if node.loc.respond_to?(:question)

          check(node)
        end

        def on_while(node)
          check(node)
        end

        def on_until(node)
          check(node)
        end

        private

        def check(node)
          condition, = *node

          if on_different_line?(node.loc.keyword.line,
                                condition.loc.expression.line)
            add_offence(condition, :expression,
                        message(node.loc.keyword.source))
          end
        end

        def message(keyword)
          "Place the condition on the same line as #{keyword}."
        end

        def on_different_line?(keyword_line, cond_line)
          keyword_line != cond_line
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.18.1 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.18.0 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.17.0 lib/rubocop/cop/lint/condition_position.rb