-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |