Sha256: 5a464eb9114b8d4e93527eaef95da9b7709fe5a9540a021ffcec4e69a152ff8d

Contents?: true

Size: 1.38 KB

Versions: 73

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks for conditions that are not on the same line as
      # if/while/until.
      #
      # @example
      #
      #   # bad
      #
      #   if
      #     some_condition
      #     do_something
      #   end
      #
      # @example
      #
      #   # good
      #
      #   if some_condition
      #     do_something
      #   end
      class ConditionPosition < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Place the condition on the same line as `%<keyword>s`.'

        def on_if(node)
          return if node.ternary?

          check(node)
        end

        def on_while(node)
          check(node)
        end
        alias on_until on_while

        private

        def check(node)
          return if node.modifier_form? || node.single_line_condition?

          condition = node.condition
          message = message(condition)

          add_offense(condition, message: message) do |corrector|
            range = range_by_whole_lines(condition.source_range, include_final_newline: true)

            corrector.insert_after(condition.parent.loc.keyword, " #{condition.source}")
            corrector.remove(range)
          end
        end

        def message(condition)
          format(MSG, keyword: condition.parent.keyword)
        end
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 8 rubygems

Version Path
rubocop-1.4.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.3.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.3.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.2.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.1.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.0.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.93.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.93.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.92.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.91.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.91.0 lib/rubocop/cop/layout/condition_position.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/layout/condition_position.rb
rubocop-0.90.0 lib/rubocop/cop/layout/condition_position.rb