Sha256: be2a5a194cdd74af052107c4a6f2c971612a71beb165915a88fdd1017697bd14

Contents?: true

Size: 1.48 KB

Versions: 183

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for `in;` uses in `case` expressions.
      #
      # @example
      #   # bad
      #   case expression
      #   in pattern_a; foo
      #   in pattern_b; bar
      #   end
      #
      #   # good
      #   case expression
      #   in pattern_a then foo
      #   in pattern_b then bar
      #   end
      #
      class InPatternThen < Base
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 2.7

        MSG = 'Do not use `in %<pattern>s;`. Use `in %<pattern>s then` instead.'

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

          pattern = node.pattern
          pattern_source = if pattern.match_alt_type?
                             alternative_pattern_source(pattern)
                           else
                             pattern.source
                           end

          add_offense(node.loc.begin, message: format(MSG, pattern: pattern_source)) do |corrector|
            corrector.replace(node.loc.begin, ' then')
          end
        end

        private

        def alternative_pattern_source(pattern)
          return pattern.children.map(&:source) unless pattern.children.first.match_alt_type?

          pattern_sources = alternative_pattern_source(pattern.children.first)

          (pattern_sources << pattern.children[1].source).join(' | ')
        end
      end
    end
  end
end

Version data entries

183 entries across 176 versions & 18 rubygems

Version Path
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/in_pattern_then.rb