Skip to content
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

Conversation

koic
Copy link
Member

@koic koic commented Dec 22, 2019

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:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Comment on lines 70 to 71
def_node_matcher :file_constant_equal_doller_zero?, <<~PATTERN
(send #source_file_path_constant? :== (gvar #doller_zero?))
Copy link
Contributor

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.

Copy link
Member Author

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?))
Copy link
Contributor

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.

Copy link
Member Author

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][])
Copy link
Contributor

@buehmann buehmann Dec 22, 2019

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?))
Copy link
Contributor

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?

Copy link
Member Author

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.

@koic koic changed the title [Fix #7569] Make Style/YodaCondition to accepts __FILE__ == $0 [Fix #7569] Make Style/YodaCondition accept __FILE__ == $0 Dec 22, 2019
@koic koic force-pushed the make_style_yoda_condition_aware_of_file_eq_doller_zero branch 2 times, most recently from 47a55ba to 205dabc Compare December 22, 2019 09:25
Copy link
Contributor

@buehmann buehmann left a 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)

@koic koic force-pushed the make_style_yoda_condition_aware_of_file_eq_doller_zero branch from 205dabc to 848f9bc Compare December 22, 2019 09:29
@koic
Copy link
Member Author

koic commented Dec 22, 2019

Thanks for the review!
def_node_matcher :file_constant_equal_program_name? is actually a private method. However, it is declared at the upper part of the file because it is a declarative code by DSL.

@buehmann
Copy link
Contributor

def_node_matcher :file_constant_equal_program_name? is actually a private method. However, it is declared at the top of the file because it is a declarative code by DSL.

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 private.

@koic
Copy link
Member Author

koic commented Dec 22, 2019

Oops!

a private method

This is a typo that should be written as public method.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 23, 2019

Is __FILE__ the only such variable? I thought there were more.

@koic
Copy link
Member Author

koic commented Dec 23, 2019

Yeah, this PR only covers __FILE__ where a pragmatic use case is shown, but there may be others.

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`.
@koic koic force-pushed the make_style_yoda_condition_aware_of_file_eq_doller_zero branch from 848f9bc to 098c722 Compare December 25, 2019 14:08
@bbatsov bbatsov merged commit a7dcd1c into rubocop:master Dec 27, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 27, 2019

👍

@koic koic deleted the make_style_yoda_condition_aware_of_file_eq_doller_zero branch December 27, 2019 11:09
matkoniecz added a commit to matkoniecz/matkoniecz-ruby-style that referenced this pull request Dec 27, 2019
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.

Do not report Yoda condition problem where both values are de facto constants (__FILE__ == $0)
3 participants