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
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/condition_position.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.29.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.29.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.28.2 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.28.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.28.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.27.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.26.1 lib/rubocop/cop/layout/condition_position.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.26.0 lib/rubocop/cop/layout/condition_position.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/condition_position.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.25.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.25.0 lib/rubocop/cop/layout/condition_position.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/condition_position.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/condition_position.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.24.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.24.0 lib/rubocop/cop/layout/condition_position.rb