Sha256: 2ae02d65a91a0b642660316a606421e06e6ada4ae440c5cbbce8e1aa5a52a921

Contents?: true

Size: 1.43 KB

Versions: 34

Compression:

Stored size: 1.43 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 node.parent.block_type? && node.parent.method?(:refine)

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

Version data entries

34 entries across 30 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/refinement_import_methods.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/refinement_import_methods.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/refinement_import_methods.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.45.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.45.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.44.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.44.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.43.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.42.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.41.1 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.41.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.40.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.39.0 lib/rubocop/cop/lint/refinement_import_methods.rb
rubocop-1.38.0 lib/rubocop/cop/lint/refinement_import_methods.rb