class RuboCop::Cop::Lint::RequireRelativeSelfPath
Checks for uses a file requiring itself with ‘require_relative`.
@example
# bad # foo.rb require_relative 'foo' require_relative 'bar' # good # foo.rb require_relative 'bar'
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/lint/require_relative_self_path.rb, line 28 def on_send(node) return unless (required_feature = node.first_argument) return unless required_feature.respond_to?(:value) return unless same_file?(processed_source.file_path, required_feature.value) add_offense(node) do |corrector| corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) end end
Private Instance Methods
Source
# File lib/rubocop/cop/lint/require_relative_self_path.rb, line 44 def remove_ext(file_path) File.basename(file_path, File.extname(file_path)) end
Source
# File lib/rubocop/cop/lint/require_relative_self_path.rb, line 40 def same_file?(file_path, required_feature) file_path == required_feature || remove_ext(file_path) == required_feature end