Sha256: 03814dd9343d146f14964391893ada4d6f322eddefd6f0e264c94e6fcec8511d

Contents?: true

Size: 1.7 KB

Versions: 144

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Enforces the use of consistent method names
      # `Object#yield_self` or `Object#then`.
      #
      # @example EnforcedStyle: then (default)
      #
      #   # bad
      #   obj.yield_self { |x| x.do_something }
      #
      #   # good
      #   obj.then { |x| x.do_something }
      #
      # @example EnforcedStyle: yield_self
      #
      #   # bad
      #   obj.then { |x| x.do_something }
      #
      #   # good
      #   obj.yield_self { |x| x.do_something }
      #
      class ObjectThen < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 2.6

        MSG = 'Prefer `%<prefer>s` over `%<current>s`.'

        def on_block(node)
          check_method_node(node.send_node)
        end

        alias on_numblock on_block

        def on_send(node)
          return unless node.arguments.one? && node.first_argument.block_pass_type?

          check_method_node(node)
        end

        private

        def check_method_node(node)
          return unless preferred_method(node)

          message = message(node)
          add_offense(node.loc.selector, message: message) do |corrector|
            corrector.replace(node.loc.selector, style.to_s)
          end
        end

        def preferred_method(node)
          case style
          when :then
            node.method?(:yield_self)
          when :yield_self
            node.method?(:then)
          else
            false
          end
        end

        def message(node)
          format(MSG, prefer: style.to_s, current: node.method_name)
        end
      end
    end
  end
end

Version data entries

144 entries across 143 versions & 13 rubygems

Version Path
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/object_then.rb