Skip to content

Commit 56d801c

Browse files
authored
Merge pull request #1109 from dvandersluis/focus-autocorrect
Update `RSpec/Focus` to have auto-correction.
2 parents dc10758 + 5c75b3b commit 56d801c

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

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

78
## 2.0.1 (2020-12-02)
89

@@ -593,3 +594,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
593594
[@PhilCoggins]: https://github.com/PhilCoggins
594595
[@sl4vr]: https://github.com/sl4vr
595596
[@ahukkanen]: https://github.com/ahukkanen
597+
[@dvandersluis]: https://github.com/dvandersluis

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ RSpec/Focus:
328328
Description: Checks if examples are focused.
329329
Enabled: true
330330
VersionAdded: '1.5'
331+
VersionChanged: '2.1'
331332
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Focus
332333

333334
RSpec/HookArgument:

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,9 @@ my_class_spec.rb # describe MyClass, '#method'
15251525

15261526
| Enabled
15271527
| Yes
1528-
| No
1528+
| Yes
15291529
| 1.5
1530-
| -
1530+
| 2.1
15311531
|===
15321532

15331533
Checks if examples are focused.

lib/rubocop/cop/rspec/focus.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module RSpec
2020
# describe MyClass do
2121
# end
2222
class Focus < Base
23+
extend AutoCorrector
24+
include RangeHelp
25+
2326
MSG = 'Focused spec found.'
2427

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

4548
def on_send(node)
4649
focus_metadata(node) do |focus|
47-
add_offense(focus)
50+
add_offense(focus) do |corrector|
51+
if focus.pair_type? || focus.str_type? || focus.sym_type?
52+
corrector.remove(with_surrounding(focus))
53+
elsif focus.send_type?
54+
correct_send(corrector, focus)
55+
end
56+
end
4857
end
4958
end
5059

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

5665
metadata(node, &block)
5766
end
67+
68+
def with_surrounding(focus)
69+
range_with_space = range_with_surrounding_space(
70+
range: focus.loc.expression,
71+
side: :left
72+
)
73+
74+
range_with_surrounding_comma(range_with_space, :left)
75+
end
76+
77+
def correct_send(corrector, focus)
78+
range = focus.loc.selector
79+
unfocused = focus.method_name.to_s.sub(/^f/, '')
80+
unless Examples.regular(unfocused) || ExampleGroups.regular(unfocused)
81+
return
82+
end
83+
84+
corrector.replace(range,
85+
range.source.sub(focus.method_name.to_s, unfocused))
86+
end
5887
end
5988
end
6089
end

spec/rubocop/cop/rspec/focus_spec.rb

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@
3939
pending 'test', meta: true, focus: true do; end
4040
^^^^^^^^^^^ Focused spec found.
4141
RUBY
42+
43+
expect_correction(<<-RUBY)
44+
example 'test', meta: true do; end
45+
xit 'test', meta: true do; end
46+
describe 'test', meta: true do; end
47+
RSpec.describe 'test', meta: true do; end
48+
it 'test', meta: true do; end
49+
xspecify 'test', meta: true do; end
50+
specify 'test', meta: true do; end
51+
example_group 'test', meta: true do; end
52+
scenario 'test', meta: true do; end
53+
xexample 'test', meta: true do; end
54+
xdescribe 'test', meta: true do; end
55+
context 'test', meta: true do; end
56+
xcontext 'test', meta: true do; end
57+
feature 'test', meta: true do; end
58+
xfeature 'test', meta: true do; end
59+
xscenario 'test', meta: true do; end
60+
pending 'test', meta: true do; end
61+
RUBY
4262
end
4363

4464
it 'flags all rspec example blocks that include `:focus`' do
@@ -78,6 +98,26 @@
7898
pending 'test', :focus do; end
7999
^^^^^^ Focused spec found.
80100
RUBY
101+
102+
expect_correction(<<-RUBY)
103+
example_group 'test' do; end
104+
feature 'test' do; end
105+
xexample 'test' do; end
106+
xdescribe 'test' do; end
107+
xscenario 'test' do; end
108+
specify 'test' do; end
109+
example 'test' do; end
110+
xfeature 'test' do; end
111+
xspecify 'test' do; end
112+
scenario 'test' do; end
113+
describe 'test' do; end
114+
RSpec.describe 'test' do; end
115+
xit 'test' do; end
116+
context 'test' do; end
117+
xcontext 'test' do; end
118+
it 'test' do; end
119+
pending 'test' do; end
120+
RUBY
81121
end
82122
# rubocop:enable RSpec/ExampleLength
83123

@@ -101,12 +141,17 @@
101141
RUBY
102142
end
103143

104-
it 'does not flag a method that is focused twice' do
144+
it 'flags a method that is focused twice' do
105145
expect_offense(<<-RUBY)
106146
fit "foo", :focus do
107147
^^^^^^^^^^^^^^^^^ Focused spec found.
108148
end
109149
RUBY
150+
151+
expect_correction(<<-RUBY)
152+
it "foo" do
153+
end
154+
RUBY
110155
end
111156

112157
it 'ignores non-rspec code with :focus blocks' do
@@ -116,7 +161,7 @@
116161
RUBY
117162
end
118163

119-
it 'flags focused block types' do
164+
it 'flags focused block types' do # rubocop:disable RSpec/ExampleLength
120165
expect_offense(<<-RUBY)
121166
fdescribe 'test' do; end
122167
^^^^^^^^^^^^^^^^ Focused spec found.
@@ -137,12 +182,28 @@
137182
focus 'test' do; end
138183
^^^^^^^^^^^^ Focused spec found.
139184
RUBY
185+
186+
expect_correction(<<-RUBY)
187+
describe 'test' do; end
188+
RSpec.describe 'test' do; end
189+
feature 'test' do; end
190+
context 'test' do; end
191+
it 'test' do; end
192+
scenario 'test' do; end
193+
example 'test' do; end
194+
specify 'test' do; end
195+
focus 'test' do; end
196+
RUBY
140197
end
141198

142199
it 'flags rspec example blocks that include `:focus` preceding a hash' do
143200
expect_offense(<<-RUBY)
144201
describe 'test', :focus, js: true do; end
145202
^^^^^^ Focused spec found.
146203
RUBY
204+
205+
expect_correction(<<-RUBY)
206+
describe 'test', js: true do; end
207+
RUBY
147208
end
148209
end

0 commit comments

Comments
 (0)