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 #647] Add OrderedHooks cop #1119

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

[Fix #647] Add OrderedHooks cop #1119

wants to merge 1 commit into from

Conversation

Darhazer
Copy link
Member

No auto-correction at this point since MoveNode doesn't play nicely with each_cons
Some people may prefer around hooks to be before before hooks, so making it configurable would also be nice.


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.

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.

Looks great! Thank you.
And sorry for the delay in reviewing.


If multiple hooks are defined in example group, they should appear in
the following order:
before :suite
Copy link
Member

Choose a reason for hiding this comment

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

around :example
after :example
after :context
after :suite
Copy link
Member

Choose a reason for hiding this comment

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

' at line %<line>d.'

EXPECTED_HOOK_ORDER = %i[before around after].freeze
EXPECTED_SCOPE_ORDER = %i[suite context each].freeze
Copy link
Member

Choose a reason for hiding this comment

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

Maybe refer to :each as :example?


RuboCop::RSpec::ExampleGroup.new(node)
.hooks
.each_cons(2) { |previous, current| check_order(previous, current) }
Copy link
Member

Choose a reason for hiding this comment

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

❤️

Comment on lines +30 to +31
MSG = '`%<hook>s` is supposed to appear before `%<previous>s`' \
' at line %<line>d.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
MSG = '`%<hook>s` is supposed to appear before `%<previous>s`' \
' at line %<line>d.'
MSG = '`%<hook>s` is supposed to appear before `%<previous>s` ' \
'at line %<line>d.'

RuboCop (Parser) unfortunately cannot check this.

# after { run_cleanup }
#
class OrderedHooks < Base
extend AutoCorrector
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be removed, right?


| Pending
| Yes
| Yes
Copy link
Collaborator

Choose a reason for hiding this comment

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

The PR description says there's no autocorrection yet.

Suggested change
| Yes
| No

def format_hook(hook)
msg = hook.name.to_s
raw_scope_name = hook.send(:scope_name)
msg += "(:#{raw_scope_name})" if raw_scope_name
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps add the scope only when the hooks are identical? To avoid messages like “before(:each) is supposed to appear before after(:suite)” where I think a message like “before is supposed to appear before after” might be sufficient.

Comment on lines +61 to +65
if current.name == :after # for after we expect reversed order
order_violation(previous, current) if previous_idx < current_idx
elsif previous_idx > current_idx
order_violation(previous, current)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was hoping to get a more symmetric comparison of previous_idx with current_idx. What do you think about e.g.

Suggested change
if current.name == :after # for after we expect reversed order
order_violation(previous, current) if previous_idx < current_idx
elsif previous_idx > current_idx
order_violation(previous, current)
end
case current.name
when :after # for after we expect reversed order
order_violation(previous, current) if previous_idx < current_idx
else
order_violation(previous, current) if previous_idx > current_idx
end

@@ -4,6 +4,7 @@

* Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])
* Improve rubocop-rspec performance. ([@Darhazer][])
* Add `RSpec/OrderdHooks` cop. ([@Darhazer][])
Copy link

Choose a reason for hiding this comment

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

Suggested change
* Add `RSpec/OrderdHooks` cop. ([@Darhazer][])
* Add `RSpec/OrderedHooks` cop. ([@Darhazer][])

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.

4 participants