Skip to content

Commit

Permalink
Add RSpec/RedundantAround cop
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura authored and pirj committed Jan 23, 2023
1 parent 0868629 commit 1d652a5
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ RSpec/NoExpectationExample:
Enabled: true
RSpec/PendingWithoutReason:
Enabled: true
RSpec/RedundantAround:
Enabled: true
RSpec/SortMetadata:
Enabled: true
RSpec/SubjectDeclaration:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fix a false positive for `RSpec/VariableName` when inside non-spec code. ([@ydah])
- Fix a false positive for `RSpec/VariableDefinition` when inside non-spec code. ([@ydah])
- Add new `RSpec/PendingBlockInsideExample` cop. ([@ydah])
- Add `RSpec/RedundantAround` cop. ([@r7kamura])

## 2.18.1 (2023-01-19)

Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,12 @@ RSpec/ReceiveNever:
VersionAdded: '1.28'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReceiveNever

RSpec/RedundantAround:
Description: Remove redundant `around` hook.
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround

RSpec/RepeatedDescription:
Description: Check for repeated description strings in example groups.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
* xref:cops_rspec.adoc#rspecpredicatematcher[RSpec/PredicateMatcher]
* xref:cops_rspec.adoc#rspecreceivecounts[RSpec/ReceiveCounts]
* xref:cops_rspec.adoc#rspecreceivenever[RSpec/ReceiveNever]
* xref:cops_rspec.adoc#rspecredundantaround[RSpec/RedundantAround]
* xref:cops_rspec.adoc#rspecrepeateddescription[RSpec/RepeatedDescription]
* xref:cops_rspec.adoc#rspecrepeatedexample[RSpec/RepeatedExample]
* xref:cops_rspec.adoc#rspecrepeatedexamplegroupbody[RSpec/RepeatedExampleGroupBody]
Expand Down
30 changes: 30 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4136,6 +4136,36 @@ expect(foo).not_to receive(:bar)

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ReceiveNever

== RSpec/RedundantAround

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| <<next>>
| -
|===

Remove redundant `around` hook.

=== Examples

[source,ruby]
----
# bad
around do |example|
example.run
end
# good
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround

== RSpec/RepeatedDescription

|===
Expand Down
69 changes: 69 additions & 0 deletions lib/rubocop/cop/rspec/redundant_around.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Remove redundant `around` hook.
#
# @example
# # bad
# around do |example|
# example.run
# end
#
# # good
#
class RedundantAround < Base
extend AutoCorrector

MSG = 'Remove redundant `around` hook.'

RESTRICT_ON_SEND = %i[around].freeze

def on_block(node)
return unless match_redundant_around_hook_block?(node)

add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
alias on_numblock on_block

def on_send(node)
return unless match_redundant_around_hook_send?(node)

add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end

private

# @!method match_redundant_around_hook_block?(node)
def_node_matcher :match_redundant_around_hook_block?, <<~PATTERN
(block
(send _ :around ...)
(args _?)
(send _ :run)
)
PATTERN

# @!method match_redundant_around_hook_send?(node)
def_node_matcher :match_redundant_around_hook_send?, <<~PATTERN
(send
_
:around
...
(block-pass
(sym :run)
)
)
PATTERN

def autocorrect(corrector, node)
corrector.remove(node)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
require_relative 'rspec/predicate_matcher'
require_relative 'rspec/receive_counts'
require_relative 'rspec/receive_never'
require_relative 'rspec/redundant_around'
require_relative 'rspec/repeated_description'
require_relative 'rspec/repeated_example'
require_relative 'rspec/repeated_example_group_body'
Expand Down
98 changes: 98 additions & 0 deletions spec/rubocop/cop/rspec/redundant_around_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::RedundantAround do
context 'with another node in `around`' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
around do |example|
example.run
foo
end
RUBY
end
end

context 'with block-surrounded `run` in `around`' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
around do |example|
foo { example.run }
end
RUBY
end
end

context 'with redundant `around`' do
it 'registers offense' do
expect_offense(<<~RUBY)
around do |example|
^^^^^^^^^^^^^^^^^^^ Remove redundant `around` hook.
example.run
end
RUBY

expect_correction(<<~RUBY)
RUBY
end
end

context 'with redundant block-pass `around`' do
it 'registers offense' do
expect_offense(<<~RUBY)
around(&:run)
^^^^^^^^^^^^^ Remove redundant `around` hook.
RUBY

expect_correction(<<~RUBY)
RUBY
end
end

context 'with redundant numblock `around`' do
it 'registers offense' do
expect_offense(<<~RUBY)
around do
^^^^^^^^^ Remove redundant `around` hook.
_1.run
end
RUBY

expect_correction(<<~RUBY)
RUBY
end
end

context 'with redundant `config.around' do
it 'registers offense' do
expect_offense(<<~RUBY)
config.around do |example|
^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove redundant `around` hook.
example.run
end
RUBY

expect_correction(<<~RUBY)
RUBY
end
end

context 'with redundant `config.around(:each)' do
it 'registers offense' do
expect_offense(<<~RUBY)
config.around(:each) do |example|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove redundant `around` hook.
example.run
end
RUBY

expect_correction(<<~RUBY)
RUBY
end
end
end

0 comments on commit 1d652a5

Please sign in to comment.