Fix RedundantCurrentDirectoryInPath cop exception in case of require_relative without any arguments#12755
Merged
koic merged 1 commit intorubocop:masterfrom Mar 7, 2024
Conversation
89511c3 to
48a1685
Compare
koic
reviewed
Mar 7, 2024
| begin_pos = node.first_argument.source_range.begin.begin_pos + index | ||
|
|
||
| range_between(begin_pos, begin_pos + 2) | ||
| end |
Member
There was a problem hiding this comment.
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|
Contributor
Author
There was a problem hiding this comment.
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
e5a9ac8 to
231e8b0
Compare
Member
|
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.
231e8b0 to
f8facdf
Compare
Member
|
Thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Consider the following (completely valid) ruby program:
Checking it with rubocop produces the following exception:
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_argumentisnil1.62.0 (using Parser 3.3.0.5, rubocop-ast 1.31.1, running on ruby 3.3.0) [x86_64-linux]