Sha256: 68a96e09467b515f78a62d882f757872f5094a75b89d8d8ea93f8232221e7e09

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for conditions that are not on the same line as
      # if/while/until.
      #
      # @example
      #
      #   # bad
      #   if
      #     some_condition
      #     do_something
      #   end
      #
      #   # 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

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.69.2 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.69.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.69.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.68.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.67.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.66.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.66.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.65.1 lib/rubocop/cop/layout/condition_position.rb