Sha256: 646df1538274bf364f811d26235e6909e7cb1a1ffb8dac1223fea0fc9ebb0f31

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 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 < Cop
        include RangeHelp

        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

        def autocorrect(node)
          lambda do |corrector|
            range = range_by_whole_lines(
              node.source_range, include_final_newline: true
            )

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

        private

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

          add_offense(node.condition)
        end

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

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.89.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.88.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.87.1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.87.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.86.0 lib/rubocop/cop/layout/condition_position.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/layout/condition_position.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.85.1 lib/rubocop/cop/layout/condition_position.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.85.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.84.0 lib/rubocop/cop/layout/condition_position.rb
rubocop-0.83.0 lib/rubocop/cop/layout/condition_position.rb