Sha256: e5ce9d16d20508a7eaedfd1939b2433f4fb803853e95c20a3dc104a33e120ee6

Contents?: true

Size: 1.24 KB

Versions: 34

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks for the usage of `require_dependency`.
      #
      # `require_dependency` is an obsolete method for Rails applications running in Zeitwerk mode.
      # In Zeitwerk mode, the semantics should match Ruby's and no need to be defensive with load order,
      # just refer to classes and modules normally.
      # If the constant name is dynamic, camelize if needed, and constantize.
      #
      # Applications running in Zeitwerk mode should not use `require_dependency`.
      #
      # NOTE: This cop is disabled by default. Please enable it if you are using Zeitwerk mode.
      #
      # @example
      #   # bad
      #   require_dependency 'some_lib'
      class RequireDependency < Base
        extend TargetRailsVersion

        minimum_target_rails_version 6.0

        MSG = 'Do not use `require_dependency` with Zeitwerk mode.'
        RESTRICT_ON_SEND = %i[require_dependency].freeze

        def_node_matcher :require_dependency_call?, <<~PATTERN
          (send {nil? (const {nil? cbase} :Kernel)} :require_dependency _)
        PATTERN

        def on_send(node)
          require_dependency_call?(node) { add_offense(node) }
        end
      end
    end
  end
end

Version data entries

34 entries across 33 versions & 6 rubygems

Version Path
rubocop-rails-2.29.0 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/require_dependency.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/require_dependency.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/require_dependency.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/require_dependency.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/require_dependency.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/require_dependency.rb