Skip to content

Commit

Permalink
Merge pull request #1109 from dvandersluis/focus-autocorrect
Browse files Browse the repository at this point in the history
Update `RSpec/Focus` to have auto-correction.
  • Loading branch information
pirj committed Dec 15, 2020
2 parents dc10758 + 5c75b3b commit 56d801c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

* Fix `RSpec/FilePath` false positive for relative file path runs with long namespaces. ([@ahukkanen][])
* Update `RSpec/Focus` to have auto-correction. ([@dvandersluis][])

## 2.0.1 (2020-12-02)

Expand Down Expand Up @@ -593,3 +594,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@PhilCoggins]: https://github.com/PhilCoggins
[@sl4vr]: https://github.com/sl4vr
[@ahukkanen]: https://github.com/ahukkanen
[@dvandersluis]: https://github.com/dvandersluis
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ RSpec/Focus:
Description: Checks if examples are focused.
Enabled: true
VersionAdded: '1.5'
VersionChanged: '2.1'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Focus

RSpec/HookArgument:
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1525,9 +1525,9 @@ my_class_spec.rb # describe MyClass, '#method'

| Enabled
| Yes
| No
| Yes
| 1.5
| -
| 2.1
|===

Checks if examples are focused.
Expand Down
31 changes: 30 additions & 1 deletion lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module RSpec
# describe MyClass do
# end
class Focus < Base
extend AutoCorrector
include RangeHelp

MSG = 'Focused spec found.'

def_node_matcher :focusable_selector?, <<-PATTERN
Expand All @@ -44,7 +47,13 @@ class Focus < Base

def on_send(node)
focus_metadata(node) do |focus|
add_offense(focus)
add_offense(focus) do |corrector|
if focus.pair_type? || focus.str_type? || focus.sym_type?
corrector.remove(with_surrounding(focus))
elsif focus.send_type?
correct_send(corrector, focus)
end
end
end
end

Expand All @@ -55,6 +64,26 @@ def focus_metadata(node, &block)

metadata(node, &block)
end

def with_surrounding(focus)
range_with_space = range_with_surrounding_space(
range: focus.loc.expression,
side: :left
)

range_with_surrounding_comma(range_with_space, :left)
end

def correct_send(corrector, focus)
range = focus.loc.selector
unfocused = focus.method_name.to_s.sub(/^f/, '')
unless Examples.regular(unfocused) || ExampleGroups.regular(unfocused)
return
end

corrector.replace(range,
range.source.sub(focus.method_name.to_s, unfocused))
end
end
end
end
Expand Down
65 changes: 63 additions & 2 deletions spec/rubocop/cop/rspec/focus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@
pending 'test', meta: true, focus: true do; end
^^^^^^^^^^^ Focused spec found.
RUBY

expect_correction(<<-RUBY)
example 'test', meta: true do; end
xit 'test', meta: true do; end
describe 'test', meta: true do; end
RSpec.describe 'test', meta: true do; end
it 'test', meta: true do; end
xspecify 'test', meta: true do; end
specify 'test', meta: true do; end
example_group 'test', meta: true do; end
scenario 'test', meta: true do; end
xexample 'test', meta: true do; end
xdescribe 'test', meta: true do; end
context 'test', meta: true do; end
xcontext 'test', meta: true do; end
feature 'test', meta: true do; end
xfeature 'test', meta: true do; end
xscenario 'test', meta: true do; end
pending 'test', meta: true do; end
RUBY
end

it 'flags all rspec example blocks that include `:focus`' do
Expand Down Expand Up @@ -78,6 +98,26 @@
pending 'test', :focus do; end
^^^^^^ Focused spec found.
RUBY

expect_correction(<<-RUBY)
example_group 'test' do; end
feature 'test' do; end
xexample 'test' do; end
xdescribe 'test' do; end
xscenario 'test' do; end
specify 'test' do; end
example 'test' do; end
xfeature 'test' do; end
xspecify 'test' do; end
scenario 'test' do; end
describe 'test' do; end
RSpec.describe 'test' do; end
xit 'test' do; end
context 'test' do; end
xcontext 'test' do; end
it 'test' do; end
pending 'test' do; end
RUBY
end
# rubocop:enable RSpec/ExampleLength

Expand All @@ -101,12 +141,17 @@
RUBY
end

it 'does not flag a method that is focused twice' do
it 'flags a method that is focused twice' do
expect_offense(<<-RUBY)
fit "foo", :focus do
^^^^^^^^^^^^^^^^^ Focused spec found.
end
RUBY

expect_correction(<<-RUBY)
it "foo" do
end
RUBY
end

it 'ignores non-rspec code with :focus blocks' do
Expand All @@ -116,7 +161,7 @@
RUBY
end

it 'flags focused block types' do
it 'flags focused block types' do # rubocop:disable RSpec/ExampleLength
expect_offense(<<-RUBY)
fdescribe 'test' do; end
^^^^^^^^^^^^^^^^ Focused spec found.
Expand All @@ -137,12 +182,28 @@
focus 'test' do; end
^^^^^^^^^^^^ Focused spec found.
RUBY

expect_correction(<<-RUBY)
describe 'test' do; end
RSpec.describe 'test' do; end
feature 'test' do; end
context 'test' do; end
it 'test' do; end
scenario 'test' do; end
example 'test' do; end
specify 'test' do; end
focus 'test' do; end
RUBY
end

it 'flags rspec example blocks that include `:focus` preceding a hash' do
expect_offense(<<-RUBY)
describe 'test', :focus, js: true do; end
^^^^^^ Focused spec found.
RUBY

expect_correction(<<-RUBY)
describe 'test', js: true do; end
RUBY
end
end

0 comments on commit 56d801c

Please sign in to comment.