File tree Expand file tree Collapse file tree 3 files changed +41
-8
lines changed
Expand file tree Collapse file tree 3 files changed +41
-8
lines changed Original file line number Diff line number Diff line change 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 ] )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1414 expect_correction ( <<~RUBY )
1515 describe Foo do
1616 before { bar
17- baz }
17+ baz }
1818 end
1919 RUBY
2020 end
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
5353 expect_correction ( <<~RUBY )
5454 describe Foo do
5555 before(:all) { bar
56- baz }
56+ baz }
5757 end
5858 RUBY
5959 end
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
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
171196end
You can’t perform that action at this time.
0 commit comments