Sha256: 38777ff0f40cb4a205bcc1df239b15304d35535acd6b3356fcf5c0c6ec8501ab

Contents?: true

Size: 1.46 KB

Versions: 152

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks if `include` or `prepend` is called in `refine` block.
      # These methods are deprecated and should be replaced with `Refinement#import_methods`.
      #
      # It emulates deprecation warnings in Ruby 3.1.
      #
      # @safety
      #   This cop's autocorrection is unsafe because `include M` will affect the included class
      #   if any changes are made to module `M`.
      #   On the other hand, `import_methods M` uses a snapshot of method definitions,
      #   thus it will not be affected if module `M` changes.
      #
      # @example
      #
      #   # bad
      #   refine Foo do
      #     include Bar
      #   end
      #
      #   # bad
      #   refine Foo do
      #     prepend Bar
      #   end
      #
      #   # good
      #   refine Foo do
      #     import_methods Bar
      #   end
      #
      class RefinementImportMethods < Base
        extend TargetRubyVersion

        MSG = 'Use `import_methods` instead of `%<current>s` because it is deprecated in Ruby 3.1.'
        RESTRICT_ON_SEND = %i[include prepend].freeze

        minimum_target_ruby_version 3.1

        def on_send(node)
          return if node.receiver
          return unless (parent = node.parent)
          return unless parent.block_type? && parent.method?(:refine)

          add_offense(node.loc.selector, message: format(MSG, current: node.method_name))
        end
      end
    end
  end
end

Version data entries

152 entries across 151 versions & 16 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.67.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.66.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.66.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.65.1 lib/rubocop/cop/lint/refinement_import_methods.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.65.0 lib/rubocop/cop/lint/refinement_import_methods.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.64.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.63.4 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.63.3 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.63.2 lib/rubocop/cop/lint/refinement_import_methods.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.63.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.63.0 lib/rubocop/cop/lint/refinement_import_methods.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.62.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.62.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.61.0 lib/rubocop/cop/lint/refinement_import_methods.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/refinement_import_methods.rb