Sha256: 937f861c2f7c539a549102f2edb324c50c31f1d78b04cfcb2c0ae1db06fd1a87

Contents?: true

Size: 1.11 KB

Versions: 76

Compression:

Stored size: 1.11 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 < Base
        include OnNormalIfUnless
        include RangeHelp
        extend AutoCorrector

        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.loc.begin, message: format(MSG, keyword: node.keyword)) do |corrector|
            corrector.remove(range_with_surrounding_space(range: node.loc.begin, side: :left))
          end
        end

        private

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

Version data entries

76 entries across 76 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_if_then.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.30.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.30.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.29.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.29.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.28.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.28.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.28.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.27.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.26.1 lib/rubocop/cop/style/multiline_if_then.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.26.0 lib/rubocop/cop/style/multiline_if_then.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_if_then.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.25.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.25.0 lib/rubocop/cop/style/multiline_if_then.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_if_then.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_if_then.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/multiline_if_then.rb