Sha256: 2548f8b0ace0ee547d523631084d42c167b5f1ed61ad34b54054418930d7be92

Contents?: true

Size: 1.1 KB

Versions: 201

Compression:

Stored size: 1.1 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(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

201 entries across 194 versions & 20 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.73.2 lib/rubocop/cop/style/multiline_if_then.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.73.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.73.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.72.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.72.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.72.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.71.2 lib/rubocop/cop/style/multiline_if_then.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.71.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.71.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.70.0 lib/rubocop/cop/style/multiline_if_then.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.69.2 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.69.1 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.69.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.68.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.67.0 lib/rubocop/cop/style/multiline_if_then.rb
rubocop-1.66.1 lib/rubocop/cop/style/multiline_if_then.rb