Sha256: 6b1d663cb9a3fe2cf6d67a0af0ff395cb4a8442f9390db855e575467e34c05e5

Contents?: true

Size: 1.52 KB

Versions: 19

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks uses of the `then` keyword in multi-line `in` statement.
      #
      # @example
      #   # bad
      #   case expression
      #   in pattern then
      #   end
      #
      #   # good
      #   case expression
      #   in pattern
      #   end
      #
      #   # good
      #   case expression
      #   in pattern then do_something
      #   end
      #
      #   # good
      #   case expression
      #   in pattern then do_something(arg1,
      #                                arg2)
      #   end
      #
      class MultilineInPatternThen < Base
        include RangeHelp
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 2.7

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

        def on_in_pattern(node)
          return if !node.then? || require_then?(node)

          range = node.loc.begin
          add_offense(range) do |corrector|
            corrector.remove(
              range_with_surrounding_space(range: range, side: :left, newlines: false)
            )
          end
        end

        private

        # Requires `then` for write `in` and its body on the same line.
        def require_then?(in_pattern_node)
          return true if in_pattern_node.pattern.first_line != in_pattern_node.pattern.last_line
          return false unless in_pattern_node.body

          same_line?(in_pattern_node, in_pattern_node.body)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_in_pattern_then.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.29.1 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.29.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.28.2 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.28.1 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.28.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.27.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.26.1 lib/rubocop/cop/style/multiline_in_pattern_then.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.26.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.25.1 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.25.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.24.1 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.24.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.23.0 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.22.3 lib/rubocop/cop/style/multiline_in_pattern_then.rb
rubocop-1.22.2 lib/rubocop/cop/style/multiline_in_pattern_then.rb