forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#10084. This PR adds new `Lint/RequireRelativeSelfPath` cop. It checks for uses of `require_relative` with self file path argument. ```ruby # bad # foo.rb require_relative 'foo' require_relative 'bar' # good # foo.rb require_relative 'bar' ```
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10084](https://github.com/rubocop/rubocop/issues/10084): Add new `Lint/RequireRelativeSelfPath` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# 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' | ||
# | ||
class RequireRelativeSelfPath < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the `require_relative` that requires itself.' | ||
RESTRICT_ON_SEND = %i[require_relative].freeze | ||
|
||
def on_send(node) | ||
return unless (required_feature = node.first_argument) | ||
return unless remove_ext(processed_source.file_path) == remove_ext(required_feature.value) | ||
|
||
add_offense(node) do |corrector| | ||
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def remove_ext(file_path) | ||
File.basename(file_path, File.extname(file_path)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::RequireRelativeSelfPath, :config do | ||
it 'registers an offense when using `require_relative` with self file path argument' do | ||
expect_offense(<<~RUBY, 'foo.rb') | ||
require_relative 'foo' | ||
^^^^^^^^^^^^^^^^^^^^^^ Remove the `require_relative` that requires itself. | ||
require_relative 'bar' | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `require_relative` with self file path argument (with ext)' do | ||
expect_offense(<<~RUBY, 'foo.rb') | ||
require_relative 'foo.rb' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the `require_relative` that requires itself. | ||
require_relative 'bar' | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `require_relative` without self file path argument' do | ||
expect_no_offenses(<<~RUBY, 'foo.rb') | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `require_relative` without argument' do | ||
expect_no_offenses(<<~RUBY, 'foo.rb') | ||
require_relative | ||
RUBY | ||
end | ||
end |