Skip to content

Commit

Permalink
[Fix #10084] Add new Lint/RequireRelativeSelfPath cop
Browse files Browse the repository at this point in the history
Fixes #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
koic authored and bbatsov committed Sep 16, 2021
1 parent 5402b1a commit d1cf26a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10084](https://github.com/rubocop/rubocop/issues/10084): Add new `Lint/RequireRelativeSelfPath` cop. ([@koic][])
5 changes: 5 additions & 0 deletions config/default.yml
Expand Up @@ -2045,6 +2045,11 @@ Lint/RequireParentheses:
Enabled: true
VersionAdded: '0.18'

Lint/RequireRelativeSelfPath:
Description: 'Checks for uses a file requiring itself with `require_relative`.'
Enabled: pending
VersionAdded: '<<next>>'

Lint/RescueException:
Description: 'Avoid rescuing the Exception class.'
StyleGuide: '#no-blind-rescues'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -346,6 +346,7 @@
require_relative 'rubocop/cop/lint/redundant_with_object'
require_relative 'rubocop/cop/lint/regexp_as_condition'
require_relative 'rubocop/cop/lint/require_parentheses'
require_relative 'rubocop/cop/lint/require_relative_self_path'
require_relative 'rubocop/cop/lint/rescue_exception'
require_relative 'rubocop/cop/lint/rescue_type'
require_relative 'rubocop/cop/lint/return_in_void_context'
Expand Down
45 changes: 45 additions & 0 deletions lib/rubocop/cop/lint/require_relative_self_path.rb
@@ -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
39 changes: 39 additions & 0 deletions spec/rubocop/cop/lint/require_relative_self_path_spec.rb
@@ -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

0 comments on commit d1cf26a

Please sign in to comment.