Sha256: e816b6bfb29857a3c5422f44eadbadc172fd878ff69a7cb1ee6f967c5a49a08e

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks uses of the `then` keyword
      # in multi-line when statements.
      #
      # @example
      #   # bad
      #   case foo
      #   when bar then
      #   end
      #
      #   # good
      #   case foo
      #   when bar
      #   end
      #
      #   # good
      #   case foo
      #   when bar then do_something
      #   end
      #
      class MultilineWhenThen < Cop
        include RangeHelp

        MSG = 'Do not use `then` for multiline `when` statement.'

        def on_when(node)
          # Without `then`, there's no offense
          return unless node.then?

          # Single line usage of `then` is not an offense
          return if !node.children.last.nil? && !node.multiline? && node.then?

          # With more than one statements after then, there's not offense
          return if node.children.last&.begin_type?

          add_offense(node, location: :begin)
        end

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

Version data entries

6 entries across 5 versions & 2 rubygems

Version Path
rubocop-0.78.0 lib/rubocop/cop/style/multiline_when_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/multiline_when_then.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/multiline_when_then.rb
rubocop-0.77.0 lib/rubocop/cop/style/multiline_when_then.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/multiline_when_then.rb
rubocop-0.76.0 lib/rubocop/cop/style/multiline_when_then.rb