class RuboCop::Cop::Packaging::RelativeRequireToLib
require_relative ‘foo/bar’
require_relative ‘spec_helper’
# good
require ‘foo/bar’
# good
require ‘foo.rb’
# good
require_relative ‘../../lib/foo/bar’
# bad
require_relative ‘lib/foo.rb’
# bad
@example
instead.
mapping to the “lib” directory and suggests to use ‘require`
This cop is used to identify the `require_relative` calls,
:nodoc:
:nodoc:
:nodoc:
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 require_relative(node) add_offense(node) end
def target_falls_in_lib?(str)
This method is called from inside `#def_node_matcher`.
def target_falls_in_lib?(str) root_dir = RuboCop::ConfigLoader.project_root File.expand_path(str, @file_directory).start_with?(root_dir + '/lib') end