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 2
2
3
3
## Master (Unreleased)
4
4
5
+ - Fix wrong autocorrect for ` RSpec/ScatteredSetup ` when hook contains heredoc. ([ @earlopain ] )
6
+
5
7
## 3.0.1 (2024-06-11)
6
8
7
9
- Bump RuboCop requirement to +1.61. ([ @ydah ] )
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ module RSpec
23
23
# end
24
24
#
25
25
class ScatteredSetup < Base
26
+ include FinalEndLocation
26
27
include RangeHelp
27
28
extend AutoCorrector
28
29
@@ -75,8 +76,13 @@ def message(occurrences, occurrence)
75
76
def autocorrect ( corrector , first_occurrence , occurrence )
76
77
return if first_occurrence == occurrence || !first_occurrence . body
77
78
79
+ # Take heredocs into account
80
+ body = occurrence . body &.source_range &.with (
81
+ end_pos : final_end_location ( occurrence ) . begin_pos
82
+ )
83
+
78
84
corrector . insert_after ( first_occurrence . body ,
79
- "\n #{ occurrence . body &.source } " )
85
+ "\n #{ body &.source } " )
80
86
corrector . remove ( range_by_whole_lines ( occurrence . source_range ,
81
87
include_final_newline : true ) )
82
88
end
Original file line number Diff line number Diff line change 14
14
expect_correction ( <<~RUBY )
15
15
describe Foo do
16
16
before { bar
17
- baz }
17
+ baz }
18
18
end
19
19
RUBY
20
20
end
34
34
expect_correction ( <<~RUBY )
35
35
describe Foo do
36
36
after { bar
37
- baz
38
- baz }
37
+ baz#{ ' ' }
38
+ baz }
39
39
end
40
40
RUBY
41
41
end
53
53
expect_correction ( <<~RUBY )
54
54
describe Foo do
55
55
before(:all) { bar
56
- baz }
56
+ baz }
57
57
end
58
58
RUBY
59
59
end
143
143
expect_correction ( <<~RUBY )
144
144
describe Foo do
145
145
before(:each, :special_case) { foo
146
- bar
147
- bar
148
- bar }
146
+ bar#{ ' ' }
147
+ bar#{ ' ' }
148
+ bar }
149
149
before(:example, special_case: false) { bar }
150
150
end
151
151
RUBY
168
168
end
169
169
RUBY
170
170
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
171
196
end
You can’t perform that action at this time.
0 commit comments