Sha256: 9fe88926b8f5c91c3be495354cc97cba9fd1395be03a9326968fe30da5cddf21

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 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 This is considered bad practice:
      #
      #   if cond then
      #   end
      #
      # @example If statements can contain `then` on the same line:
      #
      #   if cond then a
      #   elsif cond then b
      #   end
      class MultilineIfThen < Cop
        include OnNormalIfUnless

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

        MSG = 'Do not use `then` for multi-line `%s`.'.freeze

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

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

        private

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.50.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.49.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.49.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.48.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.48.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.47.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.47.0 lib/rubocop/cop/style/multiline_if_then.rb