-
-
Notifications
You must be signed in to change notification settings - Fork 285
Fix RSpec/ChangeByZero with compound operators #1324
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
Conversation
|
@pirj you reviewed and merged the previous PRs for this cop. Care to take a look at this change? |
pirj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic, thanks!
|
@pirj I just thought of another bad correction. It changes this: expect { foo }.not_to change { Foo.bar }.by(0)to this: expect { foo }.not_to change { Foo.bar }when it should change to this: expect { foo }.to change { Foo.bar }Just added a fix for that. |
248080b to
15fb7c4
Compare
|
@pirj could you review again, and approve the workflows again? |
pirj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSpec wouldn't allow .not_to change { }.by(...) to my best knowledge.
|
@pirj you're right, rspec complains about that now. RSpec version 3 added that complaint, but older versions allowed that. Do you still want me to change it? I don't know if rubocop-rspec supports rspec 2.x or earlier... |
|
@pirj see https://github.com/rspec/rspec-expectations/blob/main/Changelog.md#300beta2--2014-02-17 for details. |
|
@pirj do you still want me to undo the An alternative would be to just not autocorrect in that case, because the old autocorrection was wrong. Then let rspec 3+ complain about it. |
It's a good question. I would say we don't, as probably those with RSpec 2.x are on early Ruby 2.0/2.1 which we don't support. Might be Ruby version update is an easier task than RSpec 2 -> 3 migration. But not to be overly defensive with our code, I'd remove this, and wait for someone to complain about this deficiency. We can reconsider if this happens. |
15fb7c4 to
32f83e2
Compare
|
@pirj requested change made. Care to review again? |
Previously, it didn't recognize compound expressions using `&` or `|`. This led it to correct part of the expressions as regular code, but the generated code was malformed. Now it recognizes compound expressions using the operators.
32f83e2 to
048b90f
Compare
pirj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
Wow I haven't seens specs using that syntax. Do we need a cop to force using |
|
I don’t know.
Those are aliases to or and and.
I’d check existing codebases if they make use of them.
Some may prefer it, and I personally kind of like how it looks like.
|
That's why the configurable enforced styles are ;) |
|
The If you make a cop to prevent their use, make sure that there's an option to enforce their use, or at least to disable it. |
My thought exactly. Do you have an indicative example of that?
There's another reason - code indentation. expect { action! }
.to change { foo.count }
.and change { bar.count }
.to(3)reads weird. But this is what RuboCop indentation cops would insist on, for consistency. Mostly because they have no awareness on the semantics, as those two expect { action! }
.to change { foo.count } &
change { bar.count }
.to(3)looks almost properly indented, and there's no confusion between the "runner" The indentation remains consistent with more chained matchers: expect { action! }
.to change { foo.count } &
change { bar.count }
.to(3) &
change { baz.count }
.to(1)With expect { action! }
.to change { foo.count }
.and change { bar.count }
.to(3)
.and change { baz.count }
.to(1)which is incredibly ugly an forces one to put all (sometimes lengthy) qualifiers onto the same line with the matcher. |
|
My apologies to let the discussion drift. @BrianHawley you are really welcome to open an issue or a PR for the new cop, and to add some reasoning. I'll do my best to find some proof of usage of |
|
Thank you, @BrianHawley ! |
Previously, it didn't recognize compound expressions using
&or|.This led it to correct part of the expressions as regular code, but the
generated code was malformed.
Now it recognizes compound expressions using the operators.
Before submitting the PR make sure the following are checked:
master(if not - rebase it).CHANGELOG.mdif the new code introduces user-observable changes.bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).