Skip to content

Commit

Permalink
Merge pull request #1176 from rubocop/fix-eager-whitespace-removal-in…
Browse files Browse the repository at this point in the history
…-empty-hook

Fix excessive RSpec/EmptyHook whitespace removal
  • Loading branch information
pirj committed Jul 30, 2021
2 parents 198952f + f82e18a commit d705584
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Exclude unrelated Rails directories from `RSpec/DescribeClass`. ([@MothOnMars][])
* Add `RSpec/ExcessiveDocstringSpacing` cop. ([@G-Rath][])
* Add `RSpec/SubjectDeclaration` cop. ([@dswij][])
* Fix excessive whitespace removal in `RSpec/EmptyHook' autocorrection. ([@pirj][])

## 2.4.0 (2021-06-09)

Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/rspec/empty_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class EmptyHook < Base
def on_block(node)
empty_hook?(node) do |hook|
add_offense(hook) do |corrector|
range = range_with_surrounding_space(range: node.loc.expression)
range = range_with_surrounding_space(
range: node.loc.expression,
side: :left
)
corrector.remove(range)
end
end
Expand Down
49 changes: 37 additions & 12 deletions spec/rubocop/cop/rspec/empty_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'detects offense for empty `before` with :each scope' do
Expand All @@ -17,7 +17,7 @@
^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'detects offense for empty `before` with :example scope' do
Expand All @@ -26,7 +26,7 @@
^^^^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'detects offense for empty `before` with :context scope' do
Expand All @@ -35,7 +35,7 @@
^^^^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'detects offense for empty `before` with :all scope' do
Expand All @@ -44,7 +44,7 @@
^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'detects offense for empty `before` with :suite scope' do
Expand All @@ -53,7 +53,7 @@
^^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `before` hook' do
Expand Down Expand Up @@ -90,7 +90,7 @@
^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `after` hook' do
Expand All @@ -116,7 +116,7 @@
^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `around` hook' do
Expand All @@ -142,7 +142,7 @@
^^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `prepend_before` hook' do
Expand All @@ -168,7 +168,7 @@
^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `append_before` hook' do
Expand All @@ -194,7 +194,7 @@
^^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `prepend_after` hook' do
Expand All @@ -220,7 +220,7 @@
^^^^^^^^^^^^ Empty hook detected.
RUBY

expect_correction('')
expect_correction("\n")
end

it 'accepts non-empty `append_after` hook' do
Expand All @@ -238,4 +238,29 @@
RUBY
end
end

context 'when the hook is between other blocks' do
it 'detects offense for empty `append_after`' do
expect_offense(<<~RUBY)
let(:foo) { 'bar' }
before do
^^^^^^ Empty hook detected.
# commented out code
end
it do
expect(foo).to eq('bar')
end
RUBY

expect_correction(<<~RUBY)
let(:foo) { 'bar' }
it do
expect(foo).to eq('bar')
end
RUBY
end
end
end

0 comments on commit d705584

Please sign in to comment.