Class: RuboCop::Cop::Packaging::RequireWithRelativePath
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Packaging::RequireWithRelativePath
- Includes:
- Packaging::LibHelperModule
- Defined in:
- lib/rubocop/cop/packaging/require_with_relative_path.rb
Overview
This cop flags the require calls, from anywhere mapping to the “lib” directory, except originating from lib/.
Constant Summary collapse
- MSG =
This is the message that will be displayed when RuboCop::Packaging finds an offense of using
requirewith relative path to lib. "Avoid using `require` with relative path to `lib/`."
Instance Method Summary collapse
-
#falls_in_lib?(str) ⇒ Boolean
This method is called from inside
#def_node_matcher. -
#falls_in_lib_using_file?(str) ⇒ Boolean
This method is called from inside
#def_node_matcher. -
#falls_in_lib_with_file_dirname_plus_str?(str) ⇒ Boolean
This method preprends a “.” to the string that starts with “/”.
-
#on_new_investigation ⇒ Object
Extended from the Base class.
-
#on_send(node) ⇒ Object
Extended from AST::Traversal.
Methods included from Packaging::LibHelperModule
#inspected_file_falls_in_lib?, #inspected_file_is_gemspec?, #root_dir, #target_falls_in_lib?, #target_falls_in_lib_using_file?
Instance Method Details
#falls_in_lib?(str) ⇒ Boolean
This method is called from inside #def_node_matcher. It flags an offense if the require call is made from anywhere except the “lib” directory.
73 74 75 |
# File 'lib/rubocop/cop/packaging/require_with_relative_path.rb', line 73 def falls_in_lib?(str) target_falls_in_lib?(str) && !inspected_file_falls_in_lib? end |
#falls_in_lib_using_file?(str) ⇒ Boolean
This method is called from inside #def_node_matcher. It flags an offense if the require call (using the __FILE__ arguement) is made from anywhere except the “lib” directory.
80 81 82 |
# File 'lib/rubocop/cop/packaging/require_with_relative_path.rb', line 80 def falls_in_lib_using_file?(str) target_falls_in_lib_using_file?(str) && !inspected_file_falls_in_lib? end |
#falls_in_lib_with_file_dirname_plus_str?(str) ⇒ Boolean
This method preprends a “.” to the string that starts with “/”. And then determines if that call is made to “lib/”.
86 87 88 89 |
# File 'lib/rubocop/cop/packaging/require_with_relative_path.rb', line 86 def falls_in_lib_with_file_dirname_plus_str?(str) str.prepend(".") target_falls_in_lib?(str) end |
#on_new_investigation ⇒ Object
Extended from the Base class. More about the #on_new_investigation method can be found here: github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
Processing of the AST happens here.
56 57 58 59 |
# File 'lib/rubocop/cop/packaging/require_with_relative_path.rb', line 56 def on_new_investigation @file_path = processed_source.file_path @file_directory = File.dirname(@file_path) end |
#on_send(node) ⇒ Object
Extended from AST::Traversal. More about the #on_send method can be found here: github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
64 65 66 67 68 |
# File 'lib/rubocop/cop/packaging/require_with_relative_path.rb', line 64 def on_send(node) return unless require?(node) add_offense(node) end |