Sha256: b3dd908c1a2f5d39aaae6e2deb816e1901165b07a337360429a63a2736053b34

Contents?: true

Size: 1.26 KB

Versions: 36

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for duplicate `require`s and `require_relative`s.
      #
      # @example
      #   # bad
      #   require 'foo'
      #   require 'bar'
      #   require 'foo'
      #
      #   # good
      #   require 'foo'
      #   require 'bar'
      #
      #   # good
      #   require 'foo'
      #   require_relative 'foo'
      #
      class DuplicateRequire < Base
        MSG = 'Duplicate `%<method>s` detected.'
        REQUIRE_METHODS = Set.new(%i[require require_relative]).freeze
        RESTRICT_ON_SEND = REQUIRE_METHODS

        # @!method require_call?(node)
        def_node_matcher :require_call?, <<~PATTERN
          (send {nil? (const _ :Kernel)} %REQUIRE_METHODS _)
        PATTERN

        def on_new_investigation
          # Holds the known required files for a given parent node (used as key)
          @required = Hash.new { |h, k| h[k] = Set.new }.compare_by_identity
          super
        end

        def on_send(node)
          return unless require_call?(node)
          return if @required[node.parent].add?("#{node.method_name}#{node.first_argument}")

          add_offense(node, message: format(MSG, method: node.method_name))
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_require.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.27.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.26.1 lib/rubocop/cop/lint/duplicate_require.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.26.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.25.1 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.25.0 lib/rubocop/cop/lint/duplicate_require.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.24.1 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.24.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.23.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.22.3 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.22.2 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.22.1 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.22.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.21.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.20.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.19.1 lib/rubocop/cop/lint/duplicate_require.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/duplicate_require.rb