Sha256: c5a55cf27e9b24b6d44212d77acbd7896a8f3bc7f91ac5698705ae826e0b8dba

Contents?: true

Size: 1.15 KB

Versions: 11

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

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
        include IfNode

        def on_if(node)
          return if ternary?(node)

          check(node)
        end

        def on_while(node)
          check(node)
        end

        def on_until(node)
          check(node)
        end

        private

        def check(node)
          return if !node.loc.keyword.is?('elsif') && node.loc.end.nil?

          condition, = *node
          return unless on_different_line?(node.loc.keyword.line,
                                           condition.source_range.line)

          add_offense(condition, :expression, message(node.loc.keyword.source))
        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

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/condition_position.rb
rubocop-0.46.0 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.45.0 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.44.1 lib/rubocop/cop/lint/condition_position.rb
rubocop-0.44.0 lib/rubocop/cop/lint/condition_position.rb