rubocop-rspec 2.0.0.pre → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -2
  3. data/README.md +7 -3
  4. data/config/default.yml +159 -73
  5. data/lib/rubocop-rspec.rb +8 -8
  6. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +7 -3
  7. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +7 -3
  8. data/lib/rubocop/cop/rspec/any_instance.rb +6 -10
  9. data/lib/rubocop/cop/rspec/around_block.rb +3 -1
  10. data/lib/rubocop/cop/rspec/base.rb +6 -55
  11. data/lib/rubocop/cop/rspec/be.rb +3 -2
  12. data/lib/rubocop/cop/rspec/be_eql.rb +2 -0
  13. data/lib/rubocop/cop/rspec/before_after_all.rb +6 -3
  14. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +7 -2
  15. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +6 -2
  16. data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +4 -0
  17. data/lib/rubocop/cop/rspec/context_method.rb +1 -0
  18. data/lib/rubocop/cop/rspec/context_wording.rb +7 -1
  19. data/lib/rubocop/cop/rspec/describe_class.rb +5 -2
  20. data/lib/rubocop/cop/rspec/describe_method.rb +3 -2
  21. data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -0
  22. data/lib/rubocop/cop/rspec/described_class.rb +6 -2
  23. data/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +3 -3
  24. data/lib/rubocop/cop/rspec/dialect.rb +2 -1
  25. data/lib/rubocop/cop/rspec/empty_example_group.rb +6 -45
  26. data/lib/rubocop/cop/rspec/empty_hook.rb +2 -1
  27. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +1 -1
  28. data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +1 -1
  29. data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +1 -1
  30. data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +1 -1
  31. data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +2 -2
  32. data/lib/rubocop/cop/rspec/example_length.rb +26 -12
  33. data/lib/rubocop/cop/rspec/example_without_description.rb +1 -0
  34. data/lib/rubocop/cop/rspec/example_wording.rb +1 -0
  35. data/lib/rubocop/cop/rspec/expect_actual.rb +3 -1
  36. data/lib/rubocop/cop/rspec/expect_change.rb +3 -0
  37. data/lib/rubocop/cop/rspec/expect_in_hook.rb +2 -1
  38. data/lib/rubocop/cop/rspec/expect_output.rb +1 -1
  39. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +3 -0
  40. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +4 -0
  41. data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +2 -0
  42. data/lib/rubocop/cop/rspec/file_path.rb +26 -17
  43. data/lib/rubocop/cop/rspec/focus.rb +46 -8
  44. data/lib/rubocop/cop/rspec/hook_argument.rb +4 -4
  45. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +3 -2
  46. data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +4 -0
  47. data/lib/rubocop/cop/rspec/implicit_expect.rb +2 -1
  48. data/lib/rubocop/cop/rspec/implicit_subject.rb +2 -0
  49. data/lib/rubocop/cop/rspec/instance_spy.rb +3 -1
  50. data/lib/rubocop/cop/rspec/instance_variable.rb +5 -1
  51. data/lib/rubocop/cop/rspec/it_behaves_like.rb +3 -1
  52. data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -1
  53. data/lib/rubocop/cop/rspec/let_before_examples.rb +3 -2
  54. data/lib/rubocop/cop/rspec/let_setup.rb +10 -4
  55. data/lib/rubocop/cop/rspec/message_chain.rb +4 -10
  56. data/lib/rubocop/cop/rspec/message_expectation.rb +3 -0
  57. data/lib/rubocop/cop/rspec/message_spies.rb +5 -3
  58. data/lib/rubocop/cop/rspec/mixin/comments_help.rb +38 -0
  59. data/lib/rubocop/cop/rspec/mixin/empty_line_separation.rb +51 -0
  60. data/lib/rubocop/cop/rspec/mixin/final_end_location.rb +19 -0
  61. data/lib/rubocop/cop/rspec/mixin/top_level_group.rb +54 -0
  62. data/lib/rubocop/cop/rspec/mixin/variable.rb +21 -0
  63. data/lib/rubocop/cop/rspec/multiple_describes.rb +2 -3
  64. data/lib/rubocop/cop/rspec/multiple_expectations.rb +4 -1
  65. data/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +3 -1
  66. data/lib/rubocop/cop/rspec/named_subject.rb +11 -12
  67. data/lib/rubocop/cop/rspec/nested_groups.rb +1 -1
  68. data/lib/rubocop/cop/rspec/not_to_not.rb +2 -0
  69. data/lib/rubocop/cop/rspec/overwriting_setup.rb +4 -1
  70. data/lib/rubocop/cop/rspec/pending.rb +17 -5
  71. data/lib/rubocop/cop/rspec/predicate_matcher.rb +8 -3
  72. data/lib/rubocop/cop/rspec/rails/http_status.rb +2 -0
  73. data/lib/rubocop/cop/rspec/receive_counts.rb +4 -0
  74. data/lib/rubocop/cop/rspec/receive_never.rb +2 -0
  75. data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +8 -1
  76. data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +4 -0
  77. data/lib/rubocop/cop/rspec/repeated_include_example.rb +6 -2
  78. data/lib/rubocop/cop/rspec/return_from_stub.rb +6 -0
  79. data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
  80. data/lib/rubocop/cop/rspec/shared_context.rb +22 -11
  81. data/lib/rubocop/cop/rspec/shared_examples.rb +4 -1
  82. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +4 -1
  83. data/lib/rubocop/cop/rspec/stubbed_mock.rb +2 -1
  84. data/lib/rubocop/cop/rspec/subject_stub.rb +17 -6
  85. data/lib/rubocop/cop/rspec/unspecified_exception.rb +2 -0
  86. data/lib/rubocop/cop/rspec/variable_definition.rb +1 -1
  87. data/lib/rubocop/cop/rspec/variable_name.rb +1 -1
  88. data/lib/rubocop/cop/rspec/verified_doubles.rb +2 -0
  89. data/lib/rubocop/cop/rspec/void_expect.rb +3 -0
  90. data/lib/rubocop/cop/rspec/yield.rb +3 -0
  91. data/lib/rubocop/cop/rspec_cops.rb +0 -1
  92. data/lib/rubocop/rspec/align_let_brace.rb +1 -1
  93. data/lib/rubocop/rspec/concept.rb +2 -2
  94. data/lib/rubocop/rspec/config_formatter.rb +5 -3
  95. data/lib/rubocop/rspec/corrector/move_node.rb +7 -10
  96. data/lib/rubocop/rspec/example.rb +5 -0
  97. data/lib/rubocop/rspec/example_group.rb +15 -5
  98. data/lib/rubocop/rspec/hook.rb +2 -1
  99. data/lib/rubocop/rspec/inject.rb +4 -2
  100. data/lib/rubocop/rspec/language.rb +159 -115
  101. data/lib/rubocop/rspec/language/node_pattern.rb +7 -24
  102. data/lib/rubocop/rspec/node.rb +1 -1
  103. data/lib/rubocop/rspec/version.rb +1 -1
  104. metadata +27 -16
  105. data/lib/rubocop/cop/rspec/cop.rb +0 -10
  106. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +0 -41
  107. data/lib/rubocop/rspec.rb +0 -12
  108. data/lib/rubocop/rspec/empty_line_separation.rb +0 -48
  109. data/lib/rubocop/rspec/final_end_location.rb +0 -17
  110. data/lib/rubocop/rspec/top_level_describe.rb +0 -52
  111. data/lib/rubocop/rspec/top_level_group.rb +0 -57
  112. data/lib/rubocop/rspec/variable.rb +0 -16
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Helper methods for top level example group cops
7
+ module TopLevelGroup
8
+ extend RuboCop::NodePattern::Macros
9
+
10
+ def on_new_investigation
11
+ super
12
+
13
+ top_level_groups.each do |node|
14
+ on_top_level_example_group(node) if example_group?(node)
15
+ on_top_level_group(node)
16
+ end
17
+ end
18
+
19
+ def top_level_groups
20
+ @top_level_groups ||=
21
+ top_level_nodes(root_node).select { |n| spec_group?(n) }
22
+ end
23
+
24
+ private
25
+
26
+ # Dummy methods to be overridden in the consumer
27
+ def on_top_level_example_group(_node); end
28
+
29
+ def on_top_level_group(_node); end
30
+
31
+ def top_level_group?(node)
32
+ top_level_groups.include?(node)
33
+ end
34
+
35
+ def top_level_nodes(node)
36
+ return [] if node.nil?
37
+
38
+ case node.type
39
+ when :begin
40
+ node.children
41
+ when :module, :class
42
+ top_level_nodes(node.body)
43
+ else
44
+ [node]
45
+ end
46
+ end
47
+
48
+ def root_node
49
+ processed_source.ast
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Helps check offenses with variable definitions
7
+ module Variable
8
+ extend RuboCop::NodePattern::Macros
9
+
10
+ Subjects = RuboCop::RSpec::Language::Subjects
11
+ Helpers = RuboCop::RSpec::Language::Helpers
12
+
13
+ # @!method variable_definition?(node)
14
+ def_node_matcher :variable_definition?, <<~PATTERN
15
+ (send nil? {#Subjects.all #Helpers.all}
16
+ $({sym str dsym dstr} ...) ...)
17
+ PATTERN
18
+ end
19
+ end
20
+ end
21
+ end
@@ -23,10 +23,9 @@ module RuboCop
23
23
  # end
24
24
  # end
25
25
  class MultipleDescribes < Base
26
- include RuboCop::RSpec::TopLevelGroup
26
+ include TopLevelGroup
27
27
 
28
- MSG = 'Do not use multiple top-level example groups - '\
29
- 'try to nest them.'
28
+ MSG = 'Do not use multiple top-level example groups - try to nest them.'
30
29
 
31
30
  def on_top_level_group(node)
32
31
  top_level_example_groups =
@@ -53,6 +53,7 @@ module RuboCop
53
53
  ANYTHING = ->(_node) { true }
54
54
  TRUE = ->(node) { node.true_type? }
55
55
 
56
+ # @!method aggregate_failures?(node)
56
57
  def_node_matcher :aggregate_failures?, <<-PATTERN
57
58
  (block {
58
59
  (send _ _ <(sym :aggregate_failures) ...>)
@@ -60,7 +61,9 @@ module RuboCop
60
61
  } ...)
61
62
  PATTERN
62
63
 
63
- def_node_matcher :expect?, Expectations::ALL.send_pattern
64
+ # @!method expect?(node)
65
+ def_node_matcher :expect?, send_pattern('#Expectations.all')
66
+ # @!method aggregate_failures_block?(node)
64
67
  def_node_matcher :aggregate_failures_block?, <<-PATTERN
65
68
  (block (send nil? :aggregate_failures ...) ...)
66
69
  PATTERN
@@ -85,7 +85,7 @@ module RuboCop
85
85
  #
86
86
  class MultipleMemoizedHelpers < Base
87
87
  include ConfigurableMax
88
- include RuboCop::RSpec::Variable
88
+ include Variable
89
89
 
90
90
  MSG = 'Example group has too many memoized helpers [%<count>d/%<max>d]'
91
91
 
@@ -101,6 +101,7 @@ module RuboCop
101
101
  end
102
102
 
103
103
  def on_new_investigation
104
+ super
104
105
  @example_group_memoized_helpers = {}
105
106
  end
106
107
 
@@ -128,6 +129,7 @@ module RuboCop
128
129
 
129
130
  def variable_nodes(node)
130
131
  example_group = RuboCop::RSpec::ExampleGroup.new(node)
132
+
131
133
  if allow_subject?
132
134
  example_group.lets
133
135
  else
@@ -42,24 +42,23 @@ module RuboCop
42
42
  # it { is_expected.to be_valid }
43
43
  # end
44
44
  class NamedSubject < Base
45
- MSG = 'Name your test subject if you need '\
46
- 'to reference it explicitly.'
45
+ MSG = 'Name your test subject if you need to reference it explicitly.'
47
46
 
48
- def_node_matcher :rspec_block?, <<-PATTERN
49
- {
50
- #{Examples::ALL.block_pattern}
51
- #{Hooks::ALL.block_pattern}
52
- }
53
- PATTERN
47
+ # @!method example_or_hook_block?(node)
48
+ def_node_matcher :example_or_hook_block?,
49
+ block_pattern('{#Examples.all #Hooks.all}')
54
50
 
55
- def_node_matcher :shared_example?, <<-PATTERN
56
- #{SharedGroups::EXAMPLES.block_pattern}
57
- PATTERN
51
+ # @!method shared_example?(node)
52
+ def_node_matcher :shared_example?,
53
+ block_pattern('#SharedGroups.examples')
58
54
 
55
+ # @!method subject_usage(node)
59
56
  def_node_search :subject_usage, '$(send nil? :subject)'
60
57
 
61
58
  def on_block(node)
62
- return if !rspec_block?(node) || ignored_shared_example?(node)
59
+ if !example_or_hook_block?(node) || ignored_shared_example?(node)
60
+ return
61
+ end
63
62
 
64
63
  subject_usage(node) do |subject_node|
65
64
  add_offense(subject_node.loc.selector)
@@ -87,7 +87,7 @@ module RuboCop
87
87
  #
88
88
  class NestedGroups < Base
89
89
  include ConfigurableMax
90
- include RuboCop::RSpec::TopLevelGroup
90
+ include TopLevelGroup
91
91
 
92
92
  MSG = 'Maximum example group nesting exceeded [%<total>d/%<max>d].'
93
93
 
@@ -20,7 +20,9 @@ module RuboCop
20
20
  include ConfigurableEnforcedStyle
21
21
 
22
22
  MSG = 'Prefer `%<replacement>s` over `%<original>s`.'
23
+ RESTRICT_ON_SEND = %i[not_to to_not].freeze
23
24
 
25
+ # @!method not_to_not_offense(node)
24
26
  def_node_matcher :not_to_not_offense, '(send _ % ...)'
25
27
 
26
28
  def on_send(node)
@@ -24,7 +24,10 @@ module RuboCop
24
24
  class OverwritingSetup < Base
25
25
  MSG = '`%<name>s` is already defined.'
26
26
 
27
- def_node_matcher :setup?, (Helpers::ALL + Subject::ALL).block_pattern
27
+ # @!method setup?(node)
28
+ def_node_matcher :setup?, block_pattern('{#Helpers.all #Subjects.all}')
29
+
30
+ # @!method first_argument_name(node)
28
31
  def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'
29
32
 
30
33
  def on_block(node)
@@ -34,11 +34,13 @@ module RuboCop
34
34
  class Pending < Base
35
35
  MSG = 'Pending spec found.'
36
36
 
37
- PENDING = Examples::PENDING + Examples::SKIPPED + ExampleGroups::SKIPPED
38
- SKIPPABLE = ExampleGroups::GROUPS + Examples::EXAMPLES
39
-
40
- def_node_matcher :skippable?, SKIPPABLE.send_pattern
37
+ # @!method skippable?(node)
38
+ def_node_matcher :skippable?,
39
+ send_pattern(<<~PATTERN)
40
+ {#ExampleGroups.regular #Examples.regular}
41
+ PATTERN
41
42
 
43
+ # @!method skipped_in_metadata?(node)
42
44
  def_node_matcher :skipped_in_metadata?, <<-PATTERN
43
45
  {
44
46
  (send _ _ <#skip_or_pending? ...>)
@@ -46,8 +48,18 @@ module RuboCop
46
48
  }
47
49
  PATTERN
48
50
 
51
+ # @!method skip_or_pending?(node)
49
52
  def_node_matcher :skip_or_pending?, '{(sym :skip) (sym :pending)}'
50
- def_node_matcher :pending_block?, PENDING.send_pattern
53
+
54
+ # @!method pending_block?(node)
55
+ def_node_matcher :pending_block?,
56
+ send_pattern(<<~PATTERN)
57
+ {
58
+ #ExampleGroups.skipped
59
+ #Examples.skipped
60
+ #Examples.pending
61
+ }
62
+ PATTERN
51
63
 
52
64
  def on_send(node)
53
65
  return unless pending_block?(node) || skipped?(node)
@@ -25,19 +25,22 @@ module RuboCop
25
25
  end
26
26
  end
27
27
 
28
+ # @!method predicate_in_actual?(node)
28
29
  def_node_matcher :predicate_in_actual?, <<-PATTERN
29
30
  (send
30
31
  (send nil? :expect {
31
32
  (block $(send !nil? #predicate? ...) ...)
32
33
  $(send !nil? #predicate? ...)})
33
- $#{Runners::ALL.node_pattern_union}
34
+ $#Runners.all
34
35
  $#boolean_matcher?)
35
36
  PATTERN
36
37
 
38
+ # @!method be_bool?(node)
37
39
  def_node_matcher :be_bool?, <<-PATTERN
38
40
  (send nil? {:be :eq :eql :equal} {true false})
39
41
  PATTERN
40
42
 
43
+ # @!method be_boolthy?(node)
41
44
  def_node_matcher :be_boolthy?, <<-PATTERN
42
45
  (send nil? {:be_truthy :be_falsey :be_falsy :a_truthy_value :a_falsey_value :a_falsy_value})
43
46
  PATTERN
@@ -152,19 +155,21 @@ module RuboCop
152
155
  end
153
156
  end
154
157
 
158
+ # @!method predicate_matcher?(node)
155
159
  def_node_matcher :predicate_matcher?, <<-PATTERN
156
160
  (send
157
161
  (send nil? :expect $!nil?)
158
- #{Runners::ALL.node_pattern_union}
162
+ #Runners.all
159
163
  {$(send nil? #predicate_matcher_name? ...)
160
164
  (block $(send nil? #predicate_matcher_name? ...) ...)})
161
165
  PATTERN
162
166
 
167
+ # @!method predicate_matcher_block?(node)
163
168
  def_node_matcher :predicate_matcher_block?, <<-PATTERN
164
169
  (block
165
170
  (send
166
171
  (send nil? :expect $!nil?)
167
- #{Runners::ALL.node_pattern_union}
172
+ #Runners.all
168
173
  $(send nil? #predicate_matcher_name?))
169
174
  ...)
170
175
  PATTERN
@@ -33,7 +33,9 @@ module RuboCop
33
33
  class HttpStatus < Base
34
34
  extend AutoCorrector
35
35
  include ConfigurableEnforcedStyle
36
+ RESTRICT_ON_SEND = %i[have_http_status].freeze
36
37
 
38
+ # @!method http_status(node)
37
39
  def_node_matcher :http_status, <<-PATTERN
38
40
  (send nil? :have_http_status ${int sym})
39
41
  PATTERN
@@ -28,10 +28,14 @@ module RuboCop
28
28
 
29
29
  MSG = 'Use `%<alternative>s` instead of `%<original>s`.'
30
30
 
31
+ RESTRICT_ON_SEND = %i[times].freeze
32
+
33
+ # @!method receive_counts(node)
31
34
  def_node_matcher :receive_counts, <<-PATTERN
32
35
  (send $(send _ {:exactly :at_least :at_most} (int {1 2})) :times)
33
36
  PATTERN
34
37
 
38
+ # @!method stub?(node)
35
39
  def_node_search :stub?, '(send nil? :receive ...)'
36
40
 
37
41
  def on_send(node)
@@ -16,7 +16,9 @@ module RuboCop
16
16
  class ReceiveNever < Base
17
17
  extend AutoCorrector
18
18
  MSG = 'Use `not_to receive` instead of `never`.'
19
+ RESTRICT_ON_SEND = %i[never].freeze
19
20
 
21
+ # @!method method_on_stub?(node)
20
22
  def_node_search :method_on_stub?, '(send nil? :receive ...)'
21
23
 
22
24
  def on_send(node)
@@ -46,16 +46,23 @@ module RuboCop
46
46
  class RepeatedExampleGroupBody < Base
47
47
  MSG = 'Repeated %<group>s block body on line(s) %<loc>s'
48
48
 
49
+ # @!method several_example_groups?(node)
49
50
  def_node_matcher :several_example_groups?, <<-PATTERN
50
51
  (begin <#example_group_with_body? #example_group_with_body? ...>)
51
52
  PATTERN
52
53
 
54
+ # @!method metadata(node)
53
55
  def_node_matcher :metadata, '(block (send _ _ _ $...) ...)'
56
+
57
+ # @!method body(node)
54
58
  def_node_matcher :body, '(block _ args $...)'
59
+
60
+ # @!method const_arg(node)
55
61
  def_node_matcher :const_arg, '(block (send _ _ $const ...) ...)'
56
62
 
63
+ # @!method skip_or_pending?(node)
57
64
  def_node_matcher :skip_or_pending?, <<-PATTERN
58
- (block <(send nil? {:skip :pending}) ...>)
65
+ (block <(send nil? {:skip :pending} ...) ...>)
59
66
  PATTERN
60
67
 
61
68
  def on_begin(node)
@@ -46,18 +46,22 @@ module RuboCop
46
46
  class RepeatedExampleGroupDescription < Base
47
47
  MSG = 'Repeated %<group>s block description on line(s) %<loc>s'
48
48
 
49
+ # @!method several_example_groups?(node)
49
50
  def_node_matcher :several_example_groups?, <<-PATTERN
50
51
  (begin <#example_group? #example_group? ...>)
51
52
  PATTERN
52
53
 
54
+ # @!method doc_string_and_metadata(node)
53
55
  def_node_matcher :doc_string_and_metadata, <<-PATTERN
54
56
  (block (send _ _ $_ $...) ...)
55
57
  PATTERN
56
58
 
59
+ # @!method skip_or_pending?(node)
57
60
  def_node_matcher :skip_or_pending?, <<-PATTERN
58
61
  (block <(send nil? {:skip :pending}) ...>)
59
62
  PATTERN
60
63
 
64
+ # @!method empty_description?(node)
61
65
  def_node_matcher :empty_description?, '(block (send _ _) ...)'
62
66
 
63
67
  def on_begin(node)
@@ -50,14 +50,18 @@ module RuboCop
50
50
  MSG = 'Repeated include of shared_examples %<name>s ' \
51
51
  'on line(s) %<repeat>s'
52
52
 
53
+ # @!method several_include_examples?(node)
53
54
  def_node_matcher :several_include_examples?, <<-PATTERN
54
55
  (begin <#include_examples? #include_examples? ...>)
55
56
  PATTERN
56
57
 
57
- def_node_matcher :include_examples?, Includes::EXAMPLES.send_pattern
58
+ # @!method include_examples?(node)
59
+ def_node_matcher :include_examples?,
60
+ send_pattern('#Includes.examples')
58
61
 
62
+ # @!method shared_examples_name(node)
59
63
  def_node_matcher :shared_examples_name, <<-PATTERN
60
- (send _ #{Includes::EXAMPLES.node_pattern_union} $_ ...)
64
+ (send _ #Includes.examples $_ ...)
61
65
  PATTERN
62
66
 
63
67
  def on_begin(node)
@@ -39,9 +39,15 @@ module RuboCop
39
39
 
40
40
  MSG_AND_RETURN = 'Use `and_return` for static values.'
41
41
  MSG_BLOCK = 'Use block for static values.'
42
+ RESTRICT_ON_SEND = %i[and_return].freeze
42
43
 
44
+ # @!method contains_stub?(node)
43
45
  def_node_search :contains_stub?, '(send nil? :receive (...))'
46
+
47
+ # @!method stub_with_block?(node)
44
48
  def_node_matcher :stub_with_block?, '(block #contains_stub? ...)'
49
+
50
+ # @!method and_return_value(node)
45
51
  def_node_search :and_return_value, <<-PATTERN
46
52
  $(send _ :and_return $(...))
47
53
  PATTERN
@@ -23,7 +23,7 @@ module RuboCop
23
23
  # end
24
24
  #
25
25
  class ScatteredSetup < Base
26
- MSG = 'Do not define multiple `%<hook_name>s` hooks in the same '\
26
+ MSG = 'Do not define multiple `%<hook_name>s` hooks in the same ' \
27
27
  'example group (also defined on %<lines>s).'
28
28
 
29
29
  def on_block(node)
@@ -53,20 +53,31 @@ module RuboCop
53
53
  class SharedContext < Base
54
54
  extend AutoCorrector
55
55
 
56
- MSG_EXAMPLES = "Use `shared_examples` when you don't "\
57
- 'define context.'
56
+ MSG_EXAMPLES = "Use `shared_examples` when you don't define context."
57
+ MSG_CONTEXT = "Use `shared_context` when you don't define examples."
58
58
 
59
- MSG_CONTEXT = "Use `shared_context` when you don't "\
60
- 'define examples.'
59
+ # @!method examples?(node)
60
+ def_node_search :examples?,
61
+ send_pattern('{#Includes.examples #Examples.all}')
61
62
 
62
- examples = (Examples::ALL + Includes::EXAMPLES)
63
- def_node_search :examples?, examples.send_pattern
63
+ # @!method context?(node)
64
+ def_node_search :context?, <<-PATTERN
65
+ (
66
+ send #rspec? {
67
+ #Subjects.all
68
+ #Helpers.all
69
+ #Includes.context
70
+ #Hooks.all
71
+ } ...
72
+ )
73
+ PATTERN
64
74
 
65
- context = (Hooks::ALL + Helpers::ALL + Includes::CONTEXT + Subject::ALL)
66
- def_node_search :context?, context.send_pattern
67
-
68
- def_node_matcher :shared_context, SharedGroups::CONTEXT.block_pattern
69
- def_node_matcher :shared_example, SharedGroups::EXAMPLES.block_pattern
75
+ # @!method shared_context(node)
76
+ def_node_matcher :shared_context,
77
+ block_pattern('#SharedGroups.context')
78
+ # @!method shared_example(node)
79
+ def_node_matcher :shared_example,
80
+ block_pattern('#SharedGroups.examples')
70
81
 
71
82
  def on_block(node)
72
83
  context_with_only_examples(node) do