Sha256: c353270d4a100747c283d5c83da290dc8f013c5d1c2bade77c135c0c2113144c

Contents?: true

Size: 1.78 KB

Versions: 20

Compression:

Stored size: 1.78 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|
            prefer = style == :then && node.receiver.nil? ? 'self.then' : style

            corrector.replace(node.loc.selector, prefer)
          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

20 entries across 20 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/object_then.rb
rubocop-1.69.0 lib/rubocop/cop/style/object_then.rb
rubocop-1.68.0 lib/rubocop/cop/style/object_then.rb
rubocop-1.67.0 lib/rubocop/cop/style/object_then.rb
rubocop-1.66.1 lib/rubocop/cop/style/object_then.rb
rubocop-1.66.0 lib/rubocop/cop/style/object_then.rb
rubocop-1.65.1 lib/rubocop/cop/style/object_then.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/object_then.rb
rubocop-1.65.0 lib/rubocop/cop/style/object_then.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/object_then.rb
rubocop-1.64.1 lib/rubocop/cop/style/object_then.rb
rubocop-1.63.4 lib/rubocop/cop/style/object_then.rb
rubocop-1.63.3 lib/rubocop/cop/style/object_then.rb
rubocop-1.63.2 lib/rubocop/cop/style/object_then.rb
rubocop-1.63.1 lib/rubocop/cop/style/object_then.rb
rubocop-1.63.0 lib/rubocop/cop/style/object_then.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/object_then.rb
rubocop-1.62.1 lib/rubocop/cop/style/object_then.rb
rubocop-1.62.0 lib/rubocop/cop/style/object_then.rb
rubocop-1.61.0 lib/rubocop/cop/style/object_then.rb