Sha256: 6ba327219bfea1f4852e61df27747826a9cfe26e99da8bd6e09570c99ddf2205

Contents?: true

Size: 1.21 KB

Versions: 14

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*(#.*)?$/

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

        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

14 entries across 14 versions & 2 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.59.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.59.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.58.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.58.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.58.0 lib/rubocop/cop/style/multiline_if_then.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.57.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.57.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.57.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.56.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.55.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.54.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-0.53.0 lib/rubocop/cop/style/multiline_if_then.rb