Sha256: 89445daae5e896a1e7a4018e8d0587eead88d67200b0626ed7c4636958c846b6

Contents?: true

Size: 1.38 KB

Versions: 182

Compression:

Stored size: 1.38 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
      #
      # @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

182 entries across 175 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/condition_position.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.65.0 lib/rubocop/cop/layout/condition_position.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/condition_position.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/condition_position.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/condition_position.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.64.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.63.4 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.63.3 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.63.2 lib/rubocop/cop/layout/condition_position.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.63.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.63.0 lib/rubocop/cop/layout/condition_position.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.62.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.62.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-1.61.0 lib/rubocop/cop/layout/condition_position.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/layout/condition_position.rb
rubocop-1.60.2 lib/rubocop/cop/layout/condition_position.rb