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)
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)
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?
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
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)
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