Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 318244a

Browse files
committed
Add block name and other lines to RSpec/ScatteredSetup message
on-behalf-of: @Cofense <[email protected]>
1 parent 5fcb4c9 commit 318244a

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Add autocorrect support for `RSpec/ExpectActual` cop. ([@dduugg][], [@pirj][])
88
* Add `RSpec/RepeatedExampleGroupBody` cop. ([@lazycoder9][])
99
* Add `RSpec/RepeatedExampleGroupDescription` cop. ([@lazycoder9][])
10+
* Add block name and other lines to `RSpec/ScatteredSetup` message. ([@elebow][])
1011

1112
## 1.37.1 (2019-12-16)
1213

@@ -480,3 +481,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
480481
[@ybiquitous]: https://github.com/ybiquitous
481482
[@dduugg]: https://github.com/dduugg
482483
[@lazycoder9]: https://github.com/lazycoder9
484+
[@elebow]: https://github.com/elebow

lib/rubocop/cop/rspec/scattered_setup.rb

+25-7
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,43 @@ module RSpec
2323
# end
2424
#
2525
class ScatteredSetup < Cop
26-
MSG = 'Do not define multiple hooks in the same example group.'
26+
MSG = 'Do not define multiple `%<hook_name>s` hooks in the same '\
27+
'example group (also defined on %<lines>s).'
2728

2829
def on_block(node)
2930
return unless example_group?(node)
3031

31-
analyzable_hooks(node).each do |repeated_hook|
32-
add_offense(repeated_hook)
32+
repeated_hooks(node).each do |occurrences|
33+
lines = occurrences.map(&:first_line)
34+
35+
occurrences.each do |occurrence|
36+
lines_except_current = lines - [occurrence.first_line]
37+
message = format(MSG, hook_name: occurrences.first.method_name,
38+
lines: lines_msg(lines_except_current))
39+
add_offense(occurrence, message: message)
40+
end
3341
end
3442
end
3543

36-
def analyzable_hooks(node)
37-
RuboCop::RSpec::ExampleGroup.new(node)
44+
def repeated_hooks(node)
45+
hooks = RuboCop::RSpec::ExampleGroup.new(node)
3846
.hooks
3947
.select(&:knowable_scope?)
4048
.group_by { |hook| [hook.name, hook.scope, hook.metadata] }
4149
.values
4250
.reject(&:one?)
43-
.flatten
44-
.map(&:to_node)
51+
52+
hooks.map do |hook|
53+
hook.map(&:to_node)
54+
end
55+
end
56+
57+
def lines_msg(numbers)
58+
if numbers.size == 1
59+
"line #{numbers.first}"
60+
else
61+
"lines #{numbers.join(', ')}"
62+
end
4563
end
4664
end
4765
end

spec/rubocop/cop/rspec/scattered_setup_spec.rb

+14-14
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
expect_offense(<<-RUBY)
88
describe Foo do
99
before { bar }
10-
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
10+
^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
1111
before { baz }
12-
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
12+
^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
1313
end
1414
RUBY
1515
end
1616

1717
it 'flags multiple hooks of the same scope with different symbols' do
1818
expect_offense(<<-RUBY)
1919
describe Foo do
20-
before { bar }
21-
^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
22-
before(:each) { baz }
23-
^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
24-
before(:example) { baz }
25-
^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
20+
after { bar }
21+
^^^^^^^^^^^^^ Do not define multiple `after` hooks in the same example group (also defined on lines 3, 4).
22+
after(:each) { baz }
23+
^^^^^^^^^^^^^^^^^^^^ Do not define multiple `after` hooks in the same example group (also defined on lines 2, 4).
24+
after(:example) { baz }
25+
^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `after` hooks in the same example group (also defined on lines 2, 3).
2626
end
2727
RUBY
2828
end
@@ -31,9 +31,9 @@
3131
expect_offense(<<-RUBY)
3232
describe Foo do
3333
before(:all) { bar }
34-
^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
34+
^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
3535
before(:all) { baz }
36-
^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
36+
^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
3737
end
3838
RUBY
3939
end
@@ -109,13 +109,13 @@
109109
expect_offense(<<-RUBY)
110110
describe Foo do
111111
before(:each, :special_case) { foo }
112-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
112+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on lines 3, 4, 5).
113113
before(:example, :special_case) { bar }
114-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
114+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on lines 2, 4, 5).
115115
before(:example, special_case: true) { bar }
116-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
116+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on lines 2, 3, 5).
117117
before(special_case: true) { bar }
118-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple hooks in the same example group.
118+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on lines 2, 3, 4).
119119
before(:example, special_case: false) { bar }
120120
end
121121
RUBY

0 commit comments

Comments
 (0)