-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add StrNode#single_quoted?
, StrNode#double_quoted?
and StrNode#percent_literal?
to simplify checking for string delimiters
#343
Add StrNode#single_quoted?
, StrNode#double_quoted?
and StrNode#percent_literal?
to simplify checking for string delimiters
#343
Conversation
StrNode#single_quoted?
, StrNode#double_quoted?
and StrNode#percent_literal?` to simplify checking for string delimitersStrNode#single_quoted?
, StrNode#double_quoted?
and StrNode#percent_literal?
to simplify checking for string delimiters
1db7de5
to
c61b2e6
Compare
lib/rubocop/ast/node/str_node.rb
Outdated
:% => /^%(?![qQ])/, | ||
:q => /^%q/, | ||
:Q => /^%Q/ |
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.
It seems that matching the beginning of lines in a multiline string is not expected.
:% => /^%(?![qQ])/, | |
:q => /^%q/, | |
:Q => /^%Q/ | |
:% => /\A%(?![qQ])/, | |
:q => /\A%q/, | |
:Q => /\A%Q/ |
lib/rubocop/ast/node/str_node.rb
Outdated
return false unless opening_delimiter | ||
|
||
if type | ||
opening_delimiter.source =~ PERCENT_LITERAL_TYPES[type] |
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.
Since the captured value is not used, match?
seems to be the best choice.
opening_delimiter.source =~ PERCENT_LITERAL_TYPES[type] | |
opening_delimiter.source.match?(PERCENT_LITERAL_TYPES[type]) |
lib/rubocop/ast/node/str_node.rb
Outdated
@@ -8,13 +8,56 @@ module AST | |||
class StrNode < Node | |||
include BasicLiteralNode | |||
|
|||
PERCENT_LITERAL_TYPES = { | |||
:% => /^%(?![qQ])/, |
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.
%%foo%
, probably to the horror of some, is a valid string. I'm not sure if any cop currently cares, just wanted to point it out. I'm not 100% on what would be a valid delimiter for these, maybe [^a-zA-Z]
would be good enough?
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.
That'd still be a %(...)
string though, wouldn't it? The extra %s replace brackets but the macro type is still a single %.
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.
Yes, I think so. It's in a similar vein as %w!a!
being a valid array (hopefully no one actually writes such code).
c61b2e6
to
d015111
Compare
Thanks @koic and @Earlopain, the requested changes have been made. |
lib/rubocop/ast/node/str_node.rb
Outdated
return false unless opening_delimiter | ||
|
||
if type | ||
opening_delimiter.source.match?(PERCENT_LITERAL_TYPES[type]) |
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.
Let's use fetch
for a better error message in case type
doesn't have an expected value
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.
Updated.
lib/rubocop/ast/node/str_node.rb
Outdated
private | ||
|
||
def opening_delimiter | ||
return unless loc.respond_to?(:begin) |
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.
Is it possible that a str
node's location does not respond to begin?
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.
(We could use loc_is?
that I propose in #345)
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.
Is it possible that a
str
node's location does not respond to begin?
Yes, there are str
nodes without delimiters inside other nodes (such as dstr
or regexp
, not an exhaustive list) that have no delimiters and thus no loc.begin
. I added a test to cover this.
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.
(We could use
loc_is?
that I propose in #345)
I like it!
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.
Updated to use loc_is?
now.
…ercent_literal?` to simplify checking for string delimiters
d015111
to
1df9114
Compare
This would allow rubocop code that checks for different string delimiter types to do so in a simpler way.
I took inspiration from
ArrayNode#percent_literal?
for the interface forStrNode#percent_literal?
.