Skip to content

Commit 4ee16c9

Browse files
authored
Merge pull request #1918 from Earlopain/invalid-autocorrect-scattered-setup
[Fix #1913] Fix broken autocorrect for `RSpec/ScatteredSetup` when block contains heredoc
2 parents e59abf3 + e6106a3 commit 4ee16c9

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Fix wrong autocorrect for `RSpec/ScatteredSetup` when hook contains heredoc. ([@earlopain])
6+
57
## 3.0.1 (2024-06-11)
68

79
- Bump RuboCop requirement to +1.61. ([@ydah])

lib/rubocop/cop/rspec/scattered_setup.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module RSpec
2323
# end
2424
#
2525
class ScatteredSetup < Base
26+
include FinalEndLocation
2627
include RangeHelp
2728
extend AutoCorrector
2829

@@ -75,8 +76,13 @@ def message(occurrences, occurrence)
7576
def autocorrect(corrector, first_occurrence, occurrence)
7677
return if first_occurrence == occurrence || !first_occurrence.body
7778

79+
# Take heredocs into account
80+
body = occurrence.body&.source_range&.with(
81+
end_pos: final_end_location(occurrence).begin_pos
82+
)
83+
7884
corrector.insert_after(first_occurrence.body,
79-
"\n#{occurrence.body&.source}")
85+
"\n#{body&.source}")
8086
corrector.remove(range_by_whole_lines(occurrence.source_range,
8187
include_final_newline: true))
8288
end

spec/rubocop/cop/rspec/scattered_setup_spec.rb

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
expect_correction(<<~RUBY)
1515
describe Foo do
1616
before { bar
17-
baz }
17+
baz }
1818
end
1919
RUBY
2020
end
@@ -34,8 +34,8 @@
3434
expect_correction(<<~RUBY)
3535
describe Foo do
3636
after { bar
37-
baz
38-
baz }
37+
baz#{' '}
38+
baz }
3939
end
4040
RUBY
4141
end
@@ -53,7 +53,7 @@
5353
expect_correction(<<~RUBY)
5454
describe Foo do
5555
before(:all) { bar
56-
baz }
56+
baz }
5757
end
5858
RUBY
5959
end
@@ -143,9 +143,9 @@
143143
expect_correction(<<~RUBY)
144144
describe Foo do
145145
before(:each, :special_case) { foo
146-
bar
147-
bar
148-
bar }
146+
bar#{' '}
147+
bar#{' '}
148+
bar }
149149
before(:example, special_case: false) { bar }
150150
end
151151
RUBY
@@ -168,4 +168,29 @@
168168
end
169169
RUBY
170170
end
171+
172+
it 'flags hooks that contain heredoc arguments and autocorrects correctly' do
173+
expect_offense(<<~RUBY)
174+
describe Foo do
175+
before { foo }
176+
^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
177+
before do
178+
^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
179+
bar(<<~'TEXT')
180+
Hello World!
181+
TEXT
182+
end
183+
end
184+
RUBY
185+
186+
expect_correction(<<~RUBY)
187+
describe Foo do
188+
before { foo
189+
bar(<<~'TEXT')
190+
Hello World!
191+
TEXT
192+
}
193+
end
194+
RUBY
195+
end
171196
end

0 commit comments

Comments
 (0)