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
Do not register an offense for UnneededPercentQ when %q or %Q occurs in an interpolated string #2274
Conversation
3c403cc
to
b38bc4c
Compare
b38bc4c
to
443e94e
Compare
@@ -14,6 +14,7 @@ def on_dstr(node) | |||
end | |||
|
|||
def on_str(node) | |||
return unless standalone_string?(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's odd. A string with interpolation is actually a dstr
and we have a check for it in on_dstr
. I guess we can do more or less the same check here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was surprised by this as well. Given the example string "%q(a)#{b}"
, this cop was called twice. Once for dstr
with "%q(a)#{b}"
, and a second time for str
with the substring "%q(a)"
. It seemed weird to me that this was the behavior. That is why I had to add in the check to see if the string is a substring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like some parser bug. You should check the generated AST.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch this, it's not really a bug of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll think about this a bit more - at the very least standalone_string?
should be string_literal?
and there should probably be some explanation about why this check is needed. The cop could use a better class comment in general. Probably using the same check
method for both cases wasn't a great idea either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make the change to the method name and add comments. I will also look into refactoring this cop to implement different checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great.
609913e
to
1d6246c
Compare
8755ea1
to
1be4572
Compare
1be4572
to
d48bee1
Compare
I think that the source code is about as clean as it is going to get. It turns out that my fix also handles the case for regex. I tried to extract the code for |
Do not register an offense for UnneededPercentQ when %q or %Q occurs in an interpolated string
Looks OK to me. |
I came across a strange issue with
UnneededPercentQ
earlier. It would register an offense for a double quoted string that uses interpolation and has a section that begins with%q
or%Q
.example offenses