Class: RuboCop::Cop::Packaging::RequireHardcodingLib

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Packaging::LibHelperModule
Defined in:
lib/rubocop/cop/packaging/require_hardcoding_lib.rb

Overview

This cop flags the ‘require` calls, from anywhere mapping to the “lib” directory, except originating from lib/.

Examples:


# bad
require "../lib/foo/bar"

# good
require "foo/bar"

# bad
require File.expand_path("../../lib/foo", __FILE__)

# good
require "foo"

# bad
require File.expand_path("../../../lib/foo/bar/baz/qux", __dir__)

# good
require "foo/bar/baz/qux"

# bad
require File.dirname(__FILE__) + "/../../lib/baz/qux"

# good
require "baz/qux"

Constant Summary collapse

MSG =

This is the message that will be displayed when RuboCop::Packaging finds an offense of using ‘require` with relative path to lib.

"Avoid using `require` with relative path to `lib/`. " \
"Use `require` with absolute path instead."

Instance Method Summary collapse

Methods included from Packaging::LibHelperModule

#inspected_file_falls_in_lib?, #inspected_file_is_gemspec?, #inspected_file_is_not_in_lib_or_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.

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 85

def falls_in_lib?(str)
  @str = str
  target_falls_in_lib?(str) && inspected_file_is_not_in_lib_or_gemspec?
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.

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 93

def falls_in_lib_using_file?(str)
  @str = str
  target_falls_in_lib_using_file?(str) && inspected_file_is_not_in_lib_or_gemspec?
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/”.

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 100

def falls_in_lib_with_file_dirname_plus_str?(str)
  @str = str
  str.prepend(".")
  target_falls_in_lib?(str) && inspected_file_is_not_in_lib_or_gemspec?
end

#good_require_callObject

Called from on_send, this method helps to replace the “bad” require call with the “good” one.



77
78
79
80
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 77

def good_require_call
  good_call = @str.sub(%r{^.*/lib/}, "")
  %(require "#{good_call}")
end

#on_new_investigationObject

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.



59
60
61
62
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 59

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



67
68
69
70
71
72
73
# File 'lib/rubocop/cop/packaging/require_hardcoding_lib.rb', line 67

def on_send(node)
  return unless require?(node)

  add_offense(node) do |corrector|
    corrector.replace(node, good_require_call)
  end
end