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

New RuboCop autocorrect #955

Merged
merged 1 commit into from Jul 9, 2020
Merged

New RuboCop autocorrect #955

merged 1 commit into from Jul 9, 2020

Conversation

bquorning
Copy link
Collaborator

@bquorning bquorning commented Jul 7, 2020

The autocorrection API was changed in Rubocop v0.87.0 (pull request rubocop/rubocop#7868). This PR makes rubocop-rspec compatible with those changes.

The superclass of RuboCop::Cop::RSpec::Cop is changed from ::RuboCop::Cop::Cop to ::RuboCop::Cop::Base.

This has a few consequences:

  • Our #message methods get called with a different argument than before. It can be customized by defining a #callback_argument method, but I think it is clearer to avoid callbacks altogether.
  • Our #autocorrect methods don't get called anymore. Instead, we extend Autocorrector, and the corrector is being yielded when calling #add_offense, so the code is mostly moved in there. For some cases, this means that some code can be removed, which is nice. For some cases, it means that the methods get too long, or the code complexity gets too high, and in those cases I chose to just call out to an #autocorrect method anyway, but of course passing the corrector and any usable local variables along.
  • Our dependency of RuboCop is bumped from '>= 0.68.1' to '>= 0.87.0'.

See also https://github.com/rubocop-hq/rubocop/blob/master/docs/modules/ROOT/pages/v1_upgrade_notes.adoc for documentation of many of the changes.

We probably need some documentation changes too; I haven’t looked into that yet.


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

lib/rubocop/cop/rspec/align_left_let_brace.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/align_right_let_brace.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/capybara/current_path_expectation.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/capybara/feature_methods.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/context_method.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/empty_hook.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/example_wording.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/factory_bot/create_list.rb Outdated Show resolved Hide resolved
@bquorning
Copy link
Collaborator Author

CodeClimate complains about some added complexity, which should probably be addressed. But I think it’s important that we get a new release out pretty quickly, because right now we are incompatible with the most recent version of RuboCop.

@marcandre
Copy link
Contributor

There's a lot of activity here 👍
Let me know if I should review this now, or ping me when the dust settles 😄

Copy link
Member

@Darhazer Darhazer 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 taking care of this.
I left a few comments, but I'm fine with merging as-is 🚀

lib/rubocop/cop/rspec/hook_argument.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/implicit_expect.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/leading_subject.rb Show resolved Hide resolved
@@ -301,6 +270,7 @@ def replacement_matcher(node)
# # good - the above code is rewritten to it by this cop
# expect(foo.something?).to be_truthy
class PredicateMatcher < Cop
extend AutoCorrector
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A cop must extend AutoCorrector to be able to autocorrect: https://github.com/rubocop-hq/rubocop/blob/v0.87.1/lib/rubocop/cop/base.rb#L304

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didn't notice this is the actual cop, as it is like 270 lines below the start of the code, though it was a helper class

Copy link
Member

Choose a reason for hiding this comment

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

Still somehow confusing, as the offsense/correction is provided by the included modules. Do we need the autocorrect method in this class?

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be PredicateMatcher < Base?

If is it reused, the module should probably do it itself:

def self.included(base)
  base.extend AutoCorrector
end

lib/rubocop/cop/rspec/return_from_stub.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/return_from_stub.rb Outdated Show resolved Hide resolved
@bquorning
Copy link
Collaborator Author

bquorning commented Jul 9, 2020

@Darhazer I won’t have time to work on this today. If you have time could you please squash, merge and release? If not, I’ll look at it tomorrow. Update: I found some time. Addressed your last comments and squashed the commits. Will merge on green and then cut a new release soon.

The autocorrection API was changed in Rubocop v0.87.0 (pull request rubocop/rubocop#7868).

Here, I change the superclass of `RuboCop::Cop::RSpec::Cop` from
`::RuboCop::Cop::Cop` to `::RuboCop::Cop::Base`.

This has a few consequences:

- Our `#message` methods get called with a different argument than
  before. It *can* be customized by defining a `#callback_argument`
  method, but I think it is clearer to avoid callbacks altogether.
- Our `#autocorrect` methods don't get called anymore. Instead, we
  extend `Autocorrector`, and the `corrector` is being yielded when
  calling `#add_offense`, so the code is mostly moved in there. For some
  cases, this means that some code can be removed, which is nice. For
  some cases, it means that the methods get too long, or the code
  complexity gets too high, and in those cases I chose to just call out
  to an `#autocorrect` method anyway, but of course passing the
  `corrector` and any usable local variables along.

This also means we bump the dependency of RuboCop quite a bit, from
'>= 0.68.1' to '>= 0.87.0'.
@bquorning bquorning merged commit b53f08a into master Jul 9, 2020
@bquorning bquorning deleted the new-rubocop-autocorrect branch July 9, 2020 19:24
@bquorning bquorning mentioned this pull request Jul 9, 2020
4 tasks
@marcandre
Copy link
Contributor

Wow, impressive PR 💪!

@bquorning
Copy link
Collaborator Author

Thanks. I hope I got everything right 😅

@pirj
Copy link
Member

pirj commented Jul 10, 2020

Sleek! 👏
Thanks a lot guys.

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

5 participants