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

[Rails/Blank & Rails/Present cops] new cop configs allowing return unless present?|blank? for multiple unless guard clauses #1327

Closed
wants to merge 2 commits into from

Conversation

RDeckard
Copy link

@RDeckard RDeckard commented Aug 12, 2024

Proposition

TL;DR

Both Rails/Blank cop and Rails/Present cop are really useful, but their UnlessPresent and UnlessBlank rules can be frustrating when dealing with multiple return unless guard clauses, so we should have some cop configs allowing those return unless foo.present?/return unless foo.blank? ONLY in this very specific case (i.e. in multiple unless guard clauses).

Context

Here the current targeted rules in a guard clause context:

# bad
return unless foo.present?

# good
return if foo.blank?

(This is related to the Rails/Blank cop here, but that's the same inverted logic for Rails/Present.)

Specific case this PR aims to address

Those rules are simple and legit in most cases 👌, but we should be able to disable them specifically on multiple return unless guard clauses like in this example:

return unless foo.active?
return unless bar.present? # <= here the case that we want be able to allow through a new cop config
return unless baz.enable?

New cop configs

In order to address the case described just above ☝️, this PR propose those two new optional cop configs: (which of course should stay false by default to avoid any changed behaviors)

Rails/Blank:
  AllowMultipleUnlessPresentGuardClauses: true

Rails/Present:
  AllowMultipleUnlessBlankGuardClauses: true

Reasoning

Arguments in favor of these two new cop configs:

  • return unless a_condition is a very common idiom in guard clauses, as it simply means that a_condition is required in order to go further.
  • In the example above ⬆️⬆️, the second line (return unless bar.present?) triggers the UnlessPresent rule, which suggests changing the code to return if bar.blank?. But IMO:
    • This change would disrupt the reading flow in this very common list of guard clauses.
    • As a get around, we could actually just omit .present? in some cases, which is not good for explicitness, and therefore a counterproductive incentive.

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.

@RDeckard RDeckard changed the title [Rails/Blank & Rails/Present cops] new optional rule allowing unless present?|blank? on multiple guard clauses [Rails/Blank & Rails/Present cops] new optional rule configs allowing unless present?|blank? on multiple guard clauses Aug 12, 2024
@RDeckard RDeckard changed the title [Rails/Blank & Rails/Present cops] new optional rule configs allowing unless present?|blank? on multiple guard clauses [Rails/Blank & Rails/Present cops] new optional rule configs allowing return unless present?|blank? on multiple guard clauses Aug 12, 2024
@RDeckard RDeckard force-pushed the master branch 6 times, most recently from 5b64452 to 88812fe Compare August 12, 2024 16:42
@RDeckard RDeckard changed the title [Rails/Blank & Rails/Present cops] new optional rule configs allowing return unless present?|blank? on multiple guard clauses [Rails/Blank & Rails/Present cops] new cop configs allowing return unless present?|blank? for multiple guard clauses Aug 12, 2024
@RDeckard RDeckard changed the title [Rails/Blank & Rails/Present cops] new cop configs allowing return unless present?|blank? for multiple guard clauses [Rails/Blank & Rails/Present cops] new cop configs allowing return unless present?|blank? for multiple unless guard clauses Aug 14, 2024
@koic
Copy link
Member

koic commented Oct 29, 2024

Enabling the Style/InvertibleUnlessCondition cop, which is disabled by default, should resolve this. In RuboCop Rails, it can detect present? and blank? by default.
#1097

Additionally, adding similar configuration options would compromise simplicity, so this proposal will be closed. Thank you.

@koic koic closed this Oct 29, 2024
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.

2 participants