Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10084] Add new Lint/RequireRelativeSelfPath cop #10085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# require_relative 'bar'

I would suggest removing this line because bar does not raise offense, only foo does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not be correct for foo to be bar. I'd like to keep this for contrasted with the good case.

#
# # 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always raise ArgumentError and hence should be detected by rubocop. Maybe we change the cop name to Lint/RequireRelativePath and accomodate this case too?

Copy link
Member Author

@koic koic Sep 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case has confirmed that it does not cause an error with a valid Ruby syntax. And I think it should do method argument checking with RBS instead of RuboCop, so the cop name and role will remain the same.

RUBY
end
end