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 #7569] Make Style/YodaCondition
accept __FILE__ == $0
#7583
[Fix #7569] Make Style/YodaCondition
accept __FILE__ == $0
#7583
Conversation
def_node_matcher :file_constant_equal_doller_zero?, <<~PATTERN | ||
(send #source_file_path_constant? :== (gvar #doller_zero?)) |
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.
The currency is called dollar
with a
.
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 changed to program_name?
based on feedback #7583 (comment).
@@ -67,9 +67,14 @@ class YodaCondition < Cop | |||
|
|||
NONCOMMUTATIVE_OPERATORS = %i[===].freeze | |||
|
|||
def_node_matcher :file_constant_equal_doller_zero?, <<~PATTERN | |||
(send #source_file_path_constant? :== (gvar #doller_zero?)) |
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 assume you used (gvar #dollar_zero?)
because (gvar :$0)
is currently not supported syntax in node patterns. I wonder if we should extend node pattern to support symbols like :$0
. But this could be done later in a separate PR.
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. This is because (gvar: $0)
could not be used. Perhaps an extension of node patterns needs to be considered.
CHANGELOG.md
Outdated
@@ -5,6 +5,7 @@ | |||
### Bug fixes | |||
|
|||
* [#7193](https://github.com/rubocop-hq/rubocop/issues/7193): Prevent `Style/PercentLiteralDelimiters` from changing `%i` literals that contain escaped delimiters. ([@buehmann][]) | |||
* [#7569](https://github.com/rubocop-hq/rubocop/issues/7569): Make `Style/YodaCondition` to accepts `__FILE__ == $0`. ([@koic][]) |
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.
English grammar: Make
sth accept
sth
@@ -67,9 +67,14 @@ class YodaCondition < Cop | |||
|
|||
NONCOMMUTATIVE_OPERATORS = %i[===].freeze | |||
|
|||
def_node_matcher :file_constant_equal_doller_zero?, <<~PATTERN | |||
(send #source_file_path_constant? :== (gvar #doller_zero?)) |
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.
Shouldn't we cover some of the other comparison operators as well? <
and friends probably don't make sense, but I could imagine seeing __FILE__ != $0
in the wild.
And what about the English name of $0
, that is $PROGRAM_NAME
?
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.
Sure! I change to accept !=
and $PROGRAM_NAME
.
Style/YodaCondition
to accepts __FILE__ == $0
Style/YodaCondition
accept __FILE__ == $0
47a55ba
to
205dabc
Compare
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.
Looks good, thanks! (PS: file_constant_equal_program_name?
should probably be private
)
205dabc
to
848f9bc
Compare
Thanks for the review! |
That does not seem to be correct: [4] pry(RuboCop)> Cop::Style::YodaCondition.public_instance_methods.grep /program_name/
=> [:file_constant_equal_program_name?] I think you would need to move the definition to after |
Oops!
This is a typo that should be written as public method. |
Is |
Yeah, this PR only covers |
Fixes rubocop#7569. This PR makes `Style/YodaCondition` cop accept `__FILE__ == $0` I think that both operands `__FILE__ == $0` can be assumed to be read-only, as mentioned in rubocop#7569. If users try to assign `__FILE__`, an error will occur. Also, it is not usually assigned to `$0` by users. Therefore, this PR will make `Style/YodaCondition` cop accept both `__FILE__` and `$0` on the left side. Also, this PR will change only an idiom `__FILE__ == $0`.
848f9bc
to
098c722
Compare
👍 |
Fixes #7569.
This PR makes
Style/YodaCondition
cop accept__FILE__ == $0
I think that both operands
__FILE__ == $0
can be assumed to be read-only, as mentioned in #7569. If users try to assign__FILE__
, an error will occur. Also, it is not usually assigned to$0
by users.Therefore, this PR will make
Style/YodaCondition
cop accept both__FILE__
and$0
on the left side.Also, this PR will change only an idiom
__FILE__ == $0
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.