class RuboCop::Cop::Lint::DuplicateRequire

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'

Constants

MSG
REQUIRE_METHODS
RESTRICT_ON_SEND

Public Instance Methods

on_new_investigation() click to toggle source
Calls superclass method RuboCop::Cop::Base#on_new_investigation
# File lib/rubocop/cop/lint/duplicate_require.rb, line 39
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
on_send(node) click to toggle source
# File lib/rubocop/cop/lint/duplicate_require.rb, line 45
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