Sha256: 642546f3cf1c470ec84a458cf37abda5192fdf9de58b80b90ecb9501596731ed

Contents?: true

Size: 1.08 KB

Versions: 2

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_offense(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

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.19.1 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.19.0 lib/rubocop/cop/lint/condition_position.rb