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

Make some cops aware of safe navigation operator #1204

Merged

Conversation

koic
Copy link
Member

@koic koic commented Dec 5, 2023

Fixes #1191, #1192, #1193, #1194, #1196, #1197, #1201, and #1202.

This PR makes Rails/ActiveSupportAliases, Rails/FindBy, Rails/FindById, Rails/Inquiry, Rails/Pick Rails/PluckId, Rails/PluckInWhere, Rails/WhereEquals, Rails/WhereExists, and Rails/WhereNot cops aware of safe navigation operator.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic force-pushed the make_some_cops_aware_of_safe_navigation_operator branch from 5eb526a to 3ef1c5b Compare December 5, 2023 09:46
@@ -112,6 +123,17 @@
RUBY
end

it 'registers an offense and corrects when using `!=` and anonymous placeholder with safe navigation' do
expect_offense(<<~RUBY)
User&.where(['name != ?', 'Gabe'])
Copy link

@ydakuka ydakuka Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a violation for some cops.

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop app/models/user.rb 
Inspecting 1 file
W

Offenses:

app/models/user.rb:26:9: W: [Correctable] Lint/RedundantSafeNavigation: Redundant safe navigation detected.
    User&.where.not(name: 'Gabe')
        ^^^^^^^
app/models/user.rb:26:16: W: [Correctable] Lint/SafeNavigationChain: Do not chain ordinary method call after safe navigation operator.
    User&.where.not(name: 'Gabe')
               ^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses autocorrectable

I think I should be replaced with the following:

users = User.all
users&.where&.not(name: 'Gabe')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this seems like an overreach for the role of this cop. The role is ensure that "manually constructed SQL in where can be replaced with where.not(...)" only.

RUBY

expect_correction(<<~RUBY)
User&.where.not(name: 'Gabe')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):007:0> users = nil
=> nil
irb(main):008:0> users&.where.not(name: 'Gabe')
Traceback (most recent call last):
        1: from (irb):8
NoMethodError (undefined method `not' for nil:NilClass)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I've updated it.

RUBY

expect_correction(<<~RUBY)
User&.where(name: 'john').exists?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):010:0> users = nil
=> nil
irb(main):011:0> users&.where(name: 'john').exists?
Traceback (most recent call last):
        1: from (irb):11
NoMethodError (undefined method `exists?' for nil:NilClass)

@koic koic force-pushed the make_some_cops_aware_of_safe_navigation_operator branch 5 times, most recently from 496fa6d to 43d7d73 Compare December 13, 2023 14:36
Fixes rubocop#1191, rubocop#1192, rubocop#1193, rubocop#1194, rubocop#1196, rubocop#1197, rubocop#1201, and rubocop#1202.

This PR makes `Rails/ActiveSupportAliases`, `Rails/FindBy`, `Rails/FindById`, `Rails/Inquiry`,
`Rails/Pick` `Rails/PluckId`, `Rails/PluckInWhere`, `Rails/WhereEquals`, `Rails/WhereExists`,
and `Rails/WhereNot` cops aware of safe navigation operator.
@koic koic force-pushed the make_some_cops_aware_of_safe_navigation_operator branch from 43d7d73 to 7c672a0 Compare December 15, 2023 16:53
@koic koic merged commit cc81598 into rubocop:master Dec 15, 2023
12 checks passed
@koic koic deleted the make_some_cops_aware_of_safe_navigation_operator branch December 15, 2023 17:09
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.

Make Rails/Pick aware of safe navigation operator
2 participants