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

Handle case when node.receiver is nil in Style/InvertibleUnlessCondition #12661

Conversation

shioyama
Copy link

@shioyama shioyama commented Jan 30, 2024

Style/InvertibleUnlessCondition changes in #12562 caused this cop to fail in the case where the node receiver is nil. See the failing test. I made a fix.

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

@shioyama
Copy link
Author

shioyama commented Jan 30, 2024

Two complexity metrics are failing. I fundamentally disagree with these so if you'd like to fix them, please edit the changes:

Offenses:

lib/rubocop/cop/style/invertible_unless_condition.rb:102:9: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for preferred_send_condition is too high. [8/7]
        def preferred_send_condition(node) ...
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/cop/style/invertible_unless_condition.rb:102:9: C: Metrics/PerceivedComplexity: Perceived complexity for preferred_send_condition is too high. [9/8]
        def preferred_send_condition(node) ...
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@shioyama shioyama force-pushed the shioyama/fix_invertible_unless_condition branch from 9d4f883 to fbd3ebe Compare January 30, 2024 02:59
elsif node.parenthesized?
"#{inverse_method_source}(#{argument_list})"
else
"#{inverse_method_source} #{argument_list}"
Copy link
Author

Choose a reason for hiding this comment

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

To me a series of if/else is much clearer than a bunch of guard clauses.

@shioyama shioyama force-pushed the shioyama/fix_invertible_unless_condition branch from fbd3ebe to a7a036d Compare January 30, 2024 04:40
@shioyama
Copy link
Author

cc @sambostock

@sambostock
Copy link
Contributor

🤦 I missed your ping @shioyama and ended up coincidentally opening #12711 to fix the same bug. I'll leave it open for comparison though, as the approach is slightly different, and it has a couple more tests.

@shioyama
Copy link
Author

Thanks! I'll close this, no worries.

@shioyama shioyama closed this Feb 28, 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.

None yet

2 participants