Skip to content

Commit

Permalink
Add new RSpec/EmptyHook cop
Browse files Browse the repository at this point in the history
Closes #811
  • Loading branch information
tejasbubane committed Apr 28, 2020
1 parent b5c18e1 commit aaebd28
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
* Add new `Capybara/VisibilityMatcher` cop. ([@aried3r][])
* Ignore String constants by `RSpec/Describe`. ([@AlexWayfer][])
* Drop support for ruby 2.3. ([@bquorning][])
* Add new `RSpec/EmptyHook` cop. ([@tejasbubane][])

## 1.38.1 (2020-02-15)

Expand Down Expand Up @@ -497,3 +498,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@eitoball]: https://github.com/eitoball
[@aried3r]: https://github.com/aried3r
[@AlexWayfer]: https://github.com/AlexWayfer
[@tejasbubane]: https://github.com/tejasbubane
6 changes: 6 additions & 0 deletions config/default.yml
Expand Up @@ -105,6 +105,12 @@ RSpec/EmptyExampleGroup:
CustomIncludeMethods: []
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup

RSpec/EmptyHook:
Description: Checks for empty before and after hooks.
Enabled: true
VersionAdded: 1.38.2
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyHook

RSpec/EmptyLineAfterExample:
Description: Checks if there is an empty line after example blocks.
Enabled: true
Expand Down
44 changes: 44 additions & 0 deletions lib/rubocop/cop/rspec/empty_hook.rb
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Checks for empty before and after hooks.
#
# @example
# # bad
# before {}
# after do; end
# before(:all) do
# end
# after(:all) { }
#
# # good
# before { create_users }
# after do
# cleanup_users
# end
# before(:all) do
# create_feed
# end
# after(:all) { cleanup_feed }
class EmptyHook < Cop
MSG = 'Empty hook detected.'

def_node_matcher :empty_hook?, <<~PATTERN
(block
$(send nil? {:prepend_before :before :append_before :around
:prepend_after :after :append_after}
(sym {:each :example :context :all :suite})?)
_ nil?)
PATTERN

def on_block(node)
empty_hook?(node) do |hook|
add_offense(hook)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Expand Up @@ -30,6 +30,7 @@
require_relative 'rspec/described_class_module_wrapping'
require_relative 'rspec/dialect'
require_relative 'rspec/empty_example_group'
require_relative 'rspec/empty_hook'
require_relative 'rspec/empty_line_after_example'
require_relative 'rspec/empty_line_after_example_group'
require_relative 'rspec/empty_line_after_final_let'
Expand Down
1 change: 1 addition & 0 deletions manual/cops.md
Expand Up @@ -29,6 +29,7 @@
* [RSpec/DescribedClassModuleWrapping](cops_rspec.md#rspecdescribedclassmodulewrapping)
* [RSpec/Dialect](cops_rspec.md#rspecdialect)
* [RSpec/EmptyExampleGroup](cops_rspec.md#rspecemptyexamplegroup)
* [RSpec/EmptyHook](cops_rspec.md#rspecemptyhook)
* [RSpec/EmptyLineAfterExample](cops_rspec.md#rspecemptylineafterexample)
* [RSpec/EmptyLineAfterExampleGroup](cops_rspec.md#rspecemptylineafterexamplegroup)
* [RSpec/EmptyLineAfterFinalLet](cops_rspec.md#rspecemptylineafterfinallet)
Expand Down
39 changes: 39 additions & 0 deletions manual/cops_rspec.md
Expand Up @@ -639,6 +639,45 @@ CustomIncludeMethods | `[]` | Array

* [https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup](https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyExampleGroup)

## RSpec/EmptyHook

Enabled by default | Supports autocorrection
--- | ---
Enabled | No

Checks for empty before and after hooks.

### Examples

```ruby
# bad
before {}
after do; end
before(:all) do
end
after(:all) { }

# good
before { create_users }
after do
cleanup_users
end
before(:all) do
create_feed
end
after(:all) { cleanup_feed }
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
VersionAdded | `1.38.2` | String

### References

* [https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyHook](https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyHook)

## RSpec/EmptyLineAfterExample

Enabled by default | Supports autocorrection
Expand Down

0 comments on commit aaebd28

Please sign in to comment.