class RuboCop::Cop::Packaging::BundlerSetupInTests


require “foo”
# good
require “bundler/setup”
require “foo”
# bad
@example
made from inside the tests directory.
This cop flags the ‘require “bundler/setup”` calls if they’re
:nodoc:
:nodoc:
:nodoc:

def autocorrect(corrector, node)

the offenses flagged by this cop.
Called from on_send, this method helps to autocorrect
def autocorrect(corrector, node)
  range = range_by_whole_lines(node.source_range, include_final_newline: true)
  corrector.remove(range)
end

def bundler_setup_in_test_dir?(str)

call is made from the tests directory.
It flags an offense if the `require "bundler/setup"`
This method is called from inside `#def_node_matcher`.
def bundler_setup_in_test_dir?(str)
  str.eql?("bundler/setup") && falls_in_test_dir?
end

def falls_in_test_dir?

This method determines if the call is made *from* the tests directory.
def falls_in_test_dir?
  %w[spec specs test tests].any? { |dir| File.expand_path(@file_directory).start_with?("#{root_dir}/#{dir}") }
end

def on_new_investigation

Processing of the AST happens here.

https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
More about the `#on_new_investigation` method can be found here:
Extended from the Base class.
def on_new_investigation
  @file_path = processed_source.file_path
  @file_directory = File.dirname(@file_path)
end

def on_send(node)

https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
More about the `#on_send` method can be found here:
Extended from AST::Traversal.
def on_send(node)
  return unless bundler_setup?(node)
  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end