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 an error for RSpec/ChangeByZero when change (...) .by (0) and change (...), concatenated with and and or #1984

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

ydah
Copy link
Member

@ydah ydah commented Oct 23, 2024

fix: #1983


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • [-] Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

… `change (...)`, concatenated with `and` and `or`

fix: #1983
@ydah ydah requested a review from a team as a code owner October 23, 2024 12:28
@ydah ydah merged commit 954a45f into master Oct 23, 2024
24 checks passed
@ydah ydah deleted the fixcbz branch October 23, 2024 15:26
@pirj
Copy link
Member

pirj commented Oct 23, 2024

@@ -118,7 +118,11 @@ def register_offense(node, change_node)
# rubocop:enable Metrics/MethodLength

def compound_expectations?(node)
%i[and or & |].include?(node.parent.method_name)
if node.parent.send_type?
%i[and or & |].include?(node.parent.method_name)
Copy link
Member

Choose a reason for hiding this comment

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

This fixes it for for and and or compound aliases. We've missed that, and you’ve added specs for this. It is the gist of the fix and it’s fine.

if node.parent.send_type?
%i[and or & |].include?(node.parent.method_name)
else
node.parent.and_type? || node.parent.or_type?
Copy link
Member

@pirj pirj Oct 24, 2024

Choose a reason for hiding this comment

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

This is different. Remove this and no spec will fail.
It fixes the case for the and/or operator usage.

100% coverage would detected this.

Two problems here:

  1. This is nonsensical for RSpec, doesn’t work as it looks like and we should not add specs for this
  2. It is incomplete, as the cop would still break for eg inline if change.by 0 (which makes even less sense than and)

It fixes rubocop for someone, but they instead should fix their specs to properly use RSpec.

Would you agree to revert this statement from the fix, and to disconnect this fix from the original issue? @ydah @bquorning

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right @pirj. This code seems to be added to handle (irrelevant) cases like to (...) and (...) (note the missing . before the and).

And as you mention in #1983, we have to choose between raising an exception or allowing what’s most likely unintended syntax. Detecting unintended syntax might be something we should add to our list of non-goals?

Copy link
Member

Choose a reason for hiding this comment

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

They way you put it totally appeals to me. I can’t tell off the top of my head if we already had cases that fall under this definition. But we need some statement to be able to refer to when deciding how to deal with this.

I recall doing something like expect { … }.to raise(…), which was obviously not what I wanted.

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.

An error occurred while RSpec/ChangeByZero cop was inspecting
3 participants