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

Add new RSpec/ChangeByZero cop #1265

Merged
merged 1 commit into from
May 1, 2022
Merged

Conversation

ydah
Copy link
Member

@ydah ydah commented Apr 23, 2022

Resolves: #1100

This cop enforces the use of not_to change over to change.by(0)

example

# bad
expect { run }.to change(Foo, :bar).by(0)
expect { run }.to change { Foo.bar }.by(0)
expect { run }.to change(Foo, :bar).by(0).and change(Foo, :baz).by(0)
expect { run }.to change { Foo.bar }.by(0).and change { Foo.baz }.by(0)

# good
expect { run }.not_to change(Foo, :bar)
expect { run }.not_to change { Foo.bar }
expect { run }.to not_change(Foo, :bar).and not_change(Foo, :baz)
expect { run }.to not_change { Foo.bar }.and not_change { Foo.baz }

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

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

@ydah ydah marked this pull request as ready for review April 23, 2022 17:58
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you for another contribution!

A few notes below.
Additionally, I've ran the cop against https://github.com/pirj/real-world-rspec, and it errored out on a couple of files:

An error occurred while RSpec/ChangeByZero cop was inspecting /Users/pirj/source/real-world-rspec/canvas-lms/spec/models/content_export_spec.rb:325:30.
undefined method `end_pos' for nil:NilClass
/Users/pirj/source/rubocop-rspec/lib/rubocop/cop/rspec/change_by_zero.rb:67:in `autocorrect'
expect { @ce.save! }.to change(DelayedMessage, :count).by 0

Checked a few cases - seem to be related to by with no parentheses.

And this confirms that this weird by(0) pattern is really used in the wild 🎉

lib/rubocop/cop/rspec/change_by_zero.rb Show resolved Hide resolved
lib/rubocop/cop/rspec/change_by_zero.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/change_by_zero.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/change_by_zero.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/change_by_zero_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/change_by_zero_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/change_by_zero_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/change_by_zero_spec.rb Outdated Show resolved Hide resolved
@ydah ydah force-pushed the add_change_by_zero branch 3 times, most recently from 3b990bb to 146a772 Compare April 26, 2022 16:53
@ydah
Copy link
Member Author

ydah commented Apr 26, 2022

I updated this PR. Thank you so much!

@ydah ydah force-pushed the add_change_by_zero branch from 146a772 to f29791a Compare April 27, 2022 10:04
@ydah ydah requested review from pirj and Darhazer April 27, 2022 10:08
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Just perfect, thank you so much! 🎉

Tested on real-world-rspec with no single hiccup:

181 files inspected, 409 offenses detected, 290 offenses corrected

@pirj pirj requested a review from bquorning April 28, 2022 20:47
PATTERN

def on_send(node)
expect_change_with_arguments(node) do
Copy link
Member

Choose a reason for hiding this comment

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

minor: the cop will be faster if we RESTRICT_ON_SEND to change and then check the pattern against node.parent (as we will be getting the inner send node)

Copy link
Collaborator

Choose a reason for hiding this comment

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

the cop will be faster if we RESTRICT_ON_SEND to […]

Can we write a cop to verify the presence of RESTRICT_ON_SEND when on_send is defined?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you so much. I'll define RESTRICT_ON_SEND to change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we write a cop to verify the presence of RESTRICT_ON_SEND when on_send is defined?

I tried to create a PR rubocop/rubocop#10783 but it is now closed.
The reason is that there was a judgment that there may be more noise than detection efficiency.

Resolves: rubocop#1100

This cop enforces the use of `not_to change` over `to change.by(0)`

## example

```ruby
# bad
expect { run }.to change(Foo, :bar).by(0)
expect { run }.to change { Foo.bar }.by(0)

# good
expect { run }.not_to change(Foo, :bar)
expect { run }.not_to change { Foo.bar }
```
@ydah ydah force-pushed the add_change_by_zero branch from f29791a to 7645439 Compare April 30, 2022 02:33
@ydah
Copy link
Member Author

ydah commented Apr 30, 2022

I add RESTRICT_ON_SEND to change. Improved speed for cop.
I updated this PR! Thank you so much.

@ydah ydah requested a review from Darhazer April 30, 2022 02:37
@pirj pirj merged commit eb0add8 into rubocop:master May 1, 2022
@ydah ydah deleted the add_change_by_zero branch May 1, 2022 21:26
ydah added a commit to ydah/rubocop that referenced this pull request Jul 3, 2022
This cop will make sure that `RESTRICT_ON_SEND` is
defined if `on_send` or `after_send` is defined.

This comment rubocop/rubocop-rspec#1265 (comment)
is also mentioned in and leads to optimize the cop
by `RESTRICT_ON_SEND` whenever possible.

However, this PR shall not be used to resolve to the existing cop.

If we included that much in this PR,
the scale would be enormous,
and we thought it would be better to look at
how to optimize each cop in detail.
ydah added a commit to ydah/workitcop that referenced this pull request Jul 4, 2022
This cop will make sure that RESTRICT_ON_SEND is defined if on_send or after_send is defined.

```ruby
# bad
class FooCop
  def on_send(node)
    # ...
  end
end
# good
class FooCop
  RESTRICT_ON_SEND = %i[bad_method].freeze
  def on_send(node)
    # ...
  end
end
```

This comment rubocop/rubocop-rspec#1265 (comment)
is also mentioned in and leads to optimize the cop by RESTRICT_ON_SEND whenever possible.

However, this PR does not resolve existing cop.

If we included that much in this PR, the scale would be enormous,
and we thought it would be better to look at how to optimize each cop in detail.
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.

Cop idea: prefer not_to change over change.by(0)
4 participants