Sha256: ceea0c6ef6a2bff4c704765e763d09f10a7bb9e2f73793202187d5c97405a36c

Contents?: true

Size: 1.21 KB

Versions: 54

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of the `then` keyword in multi-line if statements.
      #
      # @example
      #   # bad
      #   # This is considered bad practice.
      #   if cond then
      #   end
      #
      #   # good
      #   # If statements can contain `then` on the same line.
      #   if cond then a
      #   elsif cond then b
      #   end
      class MultilineIfThen < Cop
        include OnNormalIfUnless
        include RangeHelp

        NON_MODIFIER_THEN = /then\s*(#.*)?$/.freeze

        MSG = 'Do not use `then` for multi-line `%<keyword>s`.'

        def on_normal_if_unless(node)
          return unless non_modifier_then?(node)

          add_offense(node, location: :begin,
                            message: format(MSG, keyword: node.keyword))
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.remove(
              range_with_surrounding_space(range: node.loc.begin, side: :left)
            )
          end
        end

        private

        def non_modifier_then?(node)
          node.loc.begin && node.loc.begin.source_line =~ NON_MODIFIER_THEN
        end
      end
    end
  end
end

Version data entries

54 entries across 35 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/multiline_if_then.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/multiline_if_then.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.85.1 lib/rubocop/cop/style/multiline_if_then.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.85.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.84.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.83.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.82.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.81.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.80.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.80.0 lib/rubocop/cop/style/multiline_if_then.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/multiline_if_then.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.79.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.78.0 lib/rubocop/cop/style/multiline_if_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/multiline_if_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/multiline_if_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/multiline_if_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/multiline_if_then.rb