Sha256: fcf1e7b0bae32f5d756345b3882abba25f94cd7f53aaa40a90095142665d1d75

Contents?: true

Size: 1.57 KB

Versions: 173

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for duplicate ``require``s and ``require_relative``s.
      #
      # @safety
      #   This cop's autocorrection is unsafe because it may break the dependency order
      #   of `require`.
      #
      # @example
      #   # bad
      #   require 'foo'
      #   require 'bar'
      #   require 'foo'
      #
      #   # good
      #   require 'foo'
      #   require 'bar'
      #
      #   # good
      #   require 'foo'
      #   require_relative 'foo'
      #
      class DuplicateRequire < Base
        include RangeHelp
        extend AutoCorrector

        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)) do |corrector|
            corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
          end
        end
      end
    end
  end
end

Version data entries

173 entries across 170 versions & 17 rubygems

Version Path
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.62.1 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.62.0 lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.61.0 lib/rubocop/cop/lint/duplicate_require.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.60.2 lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.60.1 lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/duplicate_require.rb
rubocop-1.60.0 lib/rubocop/cop/lint/duplicate_require.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/duplicate_require.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/duplicate_require.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/duplicate_require.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/duplicate_require.rb