Skip to content

Fix RedundantCurrentDirectoryInPath cop exception in case of require_relative without any arguments#12755

Merged
koic merged 1 commit intorubocop:masterfrom
viralpraxis:fix-redundant-current-directory-in-path-cop-failure-on-require-relative-without-arguments
Mar 7, 2024
Merged

Fix RedundantCurrentDirectoryInPath cop exception in case of require_relative without any arguments#12755
koic merged 1 commit intorubocop:masterfrom
viralpraxis:fix-redundant-current-directory-in-path-cop-failure-on-require-relative-without-arguments

Conversation

@viralpraxis
Copy link
Contributor

Consider the following (completely valid) ruby program:

# frozen_string_literal: true

require_relative

Checking it with rubocop produces the following exception:

bundle exec rubocop -d test.rb
...
undefined method `str_content' for nil:NilClass
/home/viralpraxis/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/rubocop-1.61.0/lib/rubocop/cop/style/redundant_current_directory_in_path.rb:25:in `on_send'
...

To fix the issue we can simply enhance

return unless node.first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)

so that it returns if node.first_argument is nil

1.62.0 (using Parser 3.3.0.5, rubocop-ast 1.31.1, running on ruby 3.3.0) [x86_64-linux]

@viralpraxis viralpraxis force-pushed the fix-redundant-current-directory-in-path-cop-failure-on-require-relative-without-arguments branch 2 times, most recently from 89511c3 to 48a1685 Compare March 7, 2024 02:34
begin_pos = node.first_argument.source_range.begin.begin_pos + index

range_between(begin_pos, begin_pos + 2)
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you modify it as below instead of creating a private method?

--- a/lib/rubocop/cop/style/redundant_current_directory_in_path.rb
+++ b/lib/rubocop/cop/style/redundant_current_directory_in_path.rb
@@ -22,10 +22,11 @@ module RuboCop

         def on_send(node)
           return unless node.method?(:require_relative)
-          return unless node.first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)
-          return unless (index = node.first_argument.source.index(CURRENT_DIRECTORY_PATH))
+          return unless (first_argument = node.first_argument)
+          return unless first_argument.value.start_with?(CURRENT_DIRECTORY_PATH)
+          return unless (index = first_argument.source.index(CURRENT_DIRECTORY_PATH))

-          begin_pos = node.first_argument.source_range.begin.begin_pos + index
+          begin_pos = first_argument.source_range.begin.begin_pos + index
           range = range_between(begin_pos, begin_pos + 2)

           add_offense(range) do |corrector|

Copy link
Contributor Author

@viralpraxis viralpraxis Mar 7, 2024

Choose a reason for hiding this comment

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

Sure, done

UPD: I replaced
return unless first_argument.value.start_with?(CURRENT_DIRECTORY_PATH)
with
return unless first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)

so that it does not fail in case of non-string require_relative argument

@viralpraxis viralpraxis force-pushed the fix-redundant-current-directory-in-path-cop-failure-on-require-relative-without-arguments branch 2 times, most recently from e5a9ac8 to 231e8b0 Compare March 7, 2024 03:36
@koic
Copy link
Member

koic commented Mar 7, 2024

This looks good to me. Can you squash your commits into one?

Cop `RedundantCurrentDirectoryInPath` throws exception
upon encountering `requre_relative` invocation
(`Kernel.require_relative` without any arguments).

Although it doesn't make much sense, it's still a
valid ruby program that should not trigger any internal
rubocop exceptions.

To fix the issue, an additional check of arguments presence
is added.
@viralpraxis viralpraxis force-pushed the fix-redundant-current-directory-in-path-cop-failure-on-require-relative-without-arguments branch from 231e8b0 to f8facdf Compare March 7, 2024 03:47
@koic koic merged commit abdbe1b into rubocop:master Mar 7, 2024
@koic
Copy link
Member

koic commented Mar 7, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants