rubocop-rspec 2.21.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +147 -9
  3. data/README.md +2 -2
  4. data/config/default.yml +159 -244
  5. data/config/obsoletion.yml +24 -0
  6. data/lib/rubocop/cop/rspec/around_block.rb +3 -3
  7. data/lib/rubocop/cop/rspec/base.rb +0 -1
  8. data/lib/rubocop/cop/rspec/be.rb +1 -1
  9. data/lib/rubocop/cop/rspec/be_empty.rb +1 -0
  10. data/lib/rubocop/cop/rspec/be_eq.rb +1 -1
  11. data/lib/rubocop/cop/rspec/be_eql.rb +1 -1
  12. data/lib/rubocop/cop/rspec/be_nil.rb +2 -2
  13. data/lib/rubocop/cop/rspec/before_after_all.rb +7 -13
  14. data/lib/rubocop/cop/rspec/change_by_zero.rb +30 -4
  15. data/lib/rubocop/cop/rspec/context_method.rb +2 -2
  16. data/lib/rubocop/cop/rspec/context_wording.rb +1 -1
  17. data/lib/rubocop/cop/rspec/describe_symbol.rb +1 -1
  18. data/lib/rubocop/cop/rspec/described_class.rb +33 -11
  19. data/lib/rubocop/cop/rspec/dialect.rb +13 -0
  20. data/lib/rubocop/cop/rspec/duplicated_metadata.rb +1 -1
  21. data/lib/rubocop/cop/rspec/empty_example_group.rb +4 -1
  22. data/lib/rubocop/cop/rspec/empty_hook.rb +1 -1
  23. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +2 -2
  24. data/lib/rubocop/cop/rspec/empty_metadata.rb +46 -0
  25. data/lib/rubocop/cop/rspec/empty_output.rb +47 -0
  26. data/lib/rubocop/cop/rspec/eq.rb +47 -0
  27. data/lib/rubocop/cop/rspec/example_length.rb +11 -5
  28. data/lib/rubocop/cop/rspec/example_without_description.rb +11 -2
  29. data/lib/rubocop/cop/rspec/example_wording.rb +11 -2
  30. data/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb +14 -5
  31. data/lib/rubocop/cop/rspec/expect_actual.rb +17 -14
  32. data/lib/rubocop/cop/rspec/expect_change.rb +2 -2
  33. data/lib/rubocop/cop/rspec/expect_in_hook.rb +1 -1
  34. data/lib/rubocop/cop/rspec/expect_in_let.rb +42 -0
  35. data/lib/rubocop/cop/rspec/expect_output.rb +1 -4
  36. data/lib/rubocop/cop/rspec/focus.rb +17 -2
  37. data/lib/rubocop/cop/rspec/hook_argument.rb +2 -2
  38. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +1 -1
  39. data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +2 -2
  40. data/lib/rubocop/cop/rspec/implicit_expect.rb +1 -1
  41. data/lib/rubocop/cop/rspec/implicit_subject.rb +2 -2
  42. data/lib/rubocop/cop/rspec/indexed_let.rb +32 -1
  43. data/lib/rubocop/cop/rspec/instance_spy.rb +2 -2
  44. data/lib/rubocop/cop/rspec/instance_variable.rb +4 -4
  45. data/lib/rubocop/cop/rspec/is_expected_specify.rb +45 -0
  46. data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -3
  47. data/lib/rubocop/cop/rspec/leaky_constant_declaration.rb +2 -2
  48. data/lib/rubocop/cop/rspec/let_before_examples.rb +5 -1
  49. data/lib/rubocop/cop/rspec/let_setup.rb +1 -1
  50. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -2
  51. data/lib/rubocop/cop/rspec/message_spies.rb +0 -2
  52. data/lib/rubocop/cop/rspec/metadata_style.rb +202 -0
  53. data/lib/rubocop/cop/rspec/missing_expectation_target_method.rb +54 -0
  54. data/lib/rubocop/cop/rspec/mixin/file_help.rb +14 -0
  55. data/lib/rubocop/cop/rspec/mixin/metadata.rb +21 -7
  56. data/lib/rubocop/cop/rspec/mixin/skip_or_pending.rb +2 -2
  57. data/lib/rubocop/cop/rspec/multiple_describes.rb +1 -1
  58. data/lib/rubocop/cop/rspec/multiple_expectations.rb +16 -11
  59. data/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +2 -4
  60. data/lib/rubocop/cop/rspec/named_subject.rb +6 -3
  61. data/lib/rubocop/cop/rspec/pending.rb +12 -2
  62. data/lib/rubocop/cop/rspec/pending_without_reason.rb +1 -1
  63. data/lib/rubocop/cop/rspec/predicate_matcher.rb +10 -10
  64. data/lib/rubocop/cop/rspec/receive_counts.rb +1 -1
  65. data/lib/rubocop/cop/rspec/receive_messages.rb +161 -0
  66. data/lib/rubocop/cop/rspec/redundant_predicate_matcher.rb +67 -0
  67. data/lib/rubocop/cop/rspec/remove_const.rb +39 -0
  68. data/lib/rubocop/cop/rspec/repeated_example.rb +6 -6
  69. data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +1 -1
  70. data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +2 -2
  71. data/lib/rubocop/cop/rspec/repeated_include_example.rb +1 -1
  72. data/lib/rubocop/cop/rspec/repeated_subject_call.rb +125 -0
  73. data/lib/rubocop/cop/rspec/return_from_stub.rb +1 -1
  74. data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
  75. data/lib/rubocop/cop/rspec/shared_context.rb +1 -1
  76. data/lib/rubocop/cop/rspec/shared_examples.rb +66 -20
  77. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +2 -3
  78. data/lib/rubocop/cop/rspec/sort_metadata.rb +3 -2
  79. data/lib/rubocop/cop/rspec/spec_file_path_format.rb +133 -0
  80. data/lib/rubocop/cop/rspec/spec_file_path_suffix.rb +40 -0
  81. data/lib/rubocop/cop/rspec/stubbed_mock.rb +4 -2
  82. data/lib/rubocop/cop/rspec/subject_stub.rb +6 -6
  83. data/lib/rubocop/cop/rspec/undescriptive_literals_description.rb +69 -0
  84. data/lib/rubocop/cop/rspec/unspecified_exception.rb +2 -2
  85. data/lib/rubocop/cop/rspec/variable_definition.rb +4 -4
  86. data/lib/rubocop/cop/rspec/verified_double_reference.rb +6 -6
  87. data/lib/rubocop/cop/rspec/verified_doubles.rb +2 -2
  88. data/lib/rubocop/cop/rspec/void_expect.rb +4 -3
  89. data/lib/rubocop/cop/rspec_cops.rb +14 -28
  90. data/lib/rubocop/rspec/concept.rb +0 -1
  91. data/lib/rubocop/rspec/config_formatter.rb +1 -11
  92. data/lib/rubocop/rspec/cop/generator.rb +25 -0
  93. data/lib/rubocop/rspec/language.rb +8 -9
  94. data/lib/rubocop/rspec/node.rb +1 -1
  95. data/lib/rubocop/rspec/shared_contexts/default_rspec_language_config_context.rb +1 -1
  96. data/lib/rubocop/rspec/version.rb +1 -1
  97. data/lib/rubocop/rspec/wording.rb +8 -0
  98. data/lib/rubocop-rspec.rb +2 -16
  99. metadata +27 -49
  100. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +0 -39
  101. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +0 -104
  102. data/lib/rubocop/cop/rspec/capybara/match_style.rb +0 -38
  103. data/lib/rubocop/cop/rspec/capybara/negation_matcher.rb +0 -33
  104. data/lib/rubocop/cop/rspec/capybara/specific_actions.rb +0 -29
  105. data/lib/rubocop/cop/rspec/capybara/specific_finders.rb +0 -24
  106. data/lib/rubocop/cop/rspec/capybara/specific_matcher.rb +0 -35
  107. data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +0 -36
  108. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +0 -128
  109. data/lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb +0 -117
  110. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +0 -260
  111. data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +0 -56
  112. data/lib/rubocop/cop/rspec/factory_bot/factory_name_style.rb +0 -74
  113. data/lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb +0 -89
  114. data/lib/rubocop/cop/rspec/file_path.rb +0 -173
  115. data/lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb +0 -43
  116. data/lib/rubocop/cop/rspec/rails/have_http_status.rb +0 -55
  117. data/lib/rubocop/cop/rspec/rails/http_status.rb +0 -203
  118. data/lib/rubocop/cop/rspec/rails/inferred_spec_type.rb +0 -145
  119. data/lib/rubocop/cop/rspec/rails/minitest_assertions.rb +0 -60
  120. data/lib/rubocop/cop/rspec/rails/travel_around.rb +0 -92
  121. data/lib/rubocop/rspec/factory_bot/language.rb +0 -37
  122. data/lib/rubocop/rspec/factory_bot.rb +0 -64
  123. data/lib/rubocop/rspec/language/node_pattern.rb +0 -48
@@ -22,6 +22,9 @@ module RuboCop
22
22
  # it 'should find nothing' do
23
23
  # end
24
24
  #
25
+ # it 'will find nothing' do
26
+ # end
27
+ #
25
28
  # # good
26
29
  # it 'finds nothing' do
27
30
  # end
@@ -47,25 +50,30 @@ module RuboCop
47
50
  extend AutoCorrector
48
51
 
49
52
  MSG_SHOULD = 'Do not use should when describing your tests.'
53
+ MSG_WILL = 'Do not use the future tense when describing your tests.'
50
54
  MSG_IT = "Do not repeat 'it' when describing your tests."
51
55
  MSG_INSUFFICIENT_DESCRIPTION = 'Your example description is ' \
52
56
  'insufficient.'
53
57
 
54
58
  SHOULD_PREFIX = /\Ashould(?:n't)?\b/i.freeze
59
+ WILL_PREFIX = /\A(?:will|won't)\b/i.freeze
55
60
  IT_PREFIX = /\Ait /i.freeze
56
61
 
57
62
  # @!method it_description(node)
58
- def_node_matcher :it_description, <<-PATTERN
63
+ def_node_matcher :it_description, <<~PATTERN
59
64
  (block (send _ :it ${
60
65
  (str $_)
61
66
  (dstr (str $_ ) ...)
62
67
  } ...) ...)
63
68
  PATTERN
64
69
 
70
+ # rubocop:disable Metrics/MethodLength
65
71
  def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
66
72
  it_description(node) do |description_node, message|
67
73
  if message.match?(SHOULD_PREFIX)
68
74
  add_wording_offense(description_node, MSG_SHOULD)
75
+ elsif message.match?(WILL_PREFIX)
76
+ add_wording_offense(description_node, MSG_WILL)
69
77
  elsif message.match?(IT_PREFIX)
70
78
  add_wording_offense(description_node, MSG_IT)
71
79
  elsif insufficient_docstring?(description_node)
@@ -74,6 +82,7 @@ module RuboCop
74
82
  end
75
83
  end
76
84
  end
85
+ # rubocop:enable Metrics/MethodLength
77
86
 
78
87
  private
79
88
 
@@ -100,7 +109,7 @@ module RuboCop
100
109
  def replacement_text(node)
101
110
  text = text(node)
102
111
 
103
- if text.match?(SHOULD_PREFIX)
112
+ if text.match?(SHOULD_PREFIX) || text.match?(WILL_PREFIX)
104
113
  RuboCop::RSpec::Wording.new(
105
114
  text,
106
115
  ignore: ignored_words,
@@ -29,7 +29,7 @@ module RuboCop
29
29
  MSG = 'Excessive whitespace.'
30
30
 
31
31
  # @!method example_description(node)
32
- def_node_matcher :example_description, <<-PATTERN
32
+ def_node_matcher :example_description, <<~PATTERN
33
33
  (send _ {#Examples.all #ExampleGroups.all} ${
34
34
  $str
35
35
  $(dstr ({str dstr `sym} ...) ...)
@@ -52,14 +52,23 @@ module RuboCop
52
52
 
53
53
  # @param text [String]
54
54
  def excessive_whitespace?(text)
55
- return true if text.start_with?(' ') || text.end_with?(' ')
56
-
57
- text.match?(/[^\n ] +[^ ]/)
55
+ text.match?(/
56
+ # Leading space
57
+ \A[[:blank:]]
58
+ |
59
+ # Trailing space
60
+ [[:blank:]]\z
61
+ |
62
+ # Two or more consecutive spaces, except if they are leading spaces
63
+ [^[[:space:]]][[:blank:]]{2,}[^[[:blank:]]]
64
+ /x)
58
65
  end
59
66
 
60
67
  # @param text [String]
61
68
  def strip_excessive_whitespace(text)
62
- text.strip.gsub(/ +/, ' ')
69
+ text
70
+ .gsub(/[[:blank:]]{2,}/, ' ')
71
+ .gsub(/\A[[:blank:]]|[[:blank:]]\z/, '')
63
72
  end
64
73
 
65
74
  # @param node [RuboCop::AST::Node]
@@ -24,7 +24,7 @@ module RuboCop
24
24
  class ExpectActual < Base
25
25
  extend AutoCorrector
26
26
 
27
- MSG = 'Provide the actual you are testing to `expect(...)`.'
27
+ MSG = 'Provide the actual value you are testing to `expect(...)`.'
28
28
 
29
29
  RESTRICT_ON_SEND = Runners.all
30
30
 
@@ -50,34 +50,42 @@ module RuboCop
50
50
  regexp
51
51
  ].freeze
52
52
 
53
- SUPPORTED_MATCHERS = %i[eq eql equal be].freeze
53
+ SKIPPED_MATCHERS = %i[route_to be_routable].freeze
54
+ CORRECTABLE_MATCHERS = %i[eq eql equal be].freeze
54
55
 
55
56
  # @!method expect_literal(node)
56
57
  def_node_matcher :expect_literal, <<~PATTERN
57
58
  (send
58
59
  (send nil? :expect $#literal?)
59
60
  #Runners.all
60
- {
61
+ ${
61
62
  (send (send nil? $:be) :== $_)
62
63
  (send nil? $_ $_ ...)
63
64
  }
64
65
  )
65
66
  PATTERN
66
67
 
67
- def on_send(node)
68
- expect_literal(node) do |actual, matcher, expected|
68
+ def on_send(node) # rubocop:disable Metrics/MethodLength
69
+ expect_literal(node) do |actual, send_node, matcher, expected|
70
+ next if SKIPPED_MATCHERS.include?(matcher)
71
+
69
72
  add_offense(actual.source_range) do |corrector|
70
- next unless SUPPORTED_MATCHERS.include?(matcher)
73
+ next unless CORRECTABLE_MATCHERS.include?(matcher)
71
74
  next if literal?(expected)
72
75
 
73
- swap(corrector, actual, expected)
76
+ corrector.replace(actual, expected.source)
77
+ if matcher == :be
78
+ corrector.replace(expected, actual.source)
79
+ else
80
+ corrector.replace(send_node, "#{matcher}(#{actual.source})")
81
+ end
74
82
  end
75
83
  end
76
84
  end
77
85
 
78
86
  private
79
87
 
80
- # This is not implement using a NodePattern because it seems
88
+ # This is not implemented using a NodePattern because it seems
81
89
  # to not be able to match against an explicit (nil) sexp
82
90
  def literal?(node)
83
91
  node && (simple_literal?(node) || complex_literal?(node))
@@ -89,12 +97,7 @@ module RuboCop
89
97
 
90
98
  def complex_literal?(node)
91
99
  COMPLEX_LITERALS.include?(node.type) &&
92
- node.each_child_node.all?(&method(:literal?))
93
- end
94
-
95
- def swap(corrector, actual, expected)
96
- corrector.replace(actual, expected.source)
97
- corrector.replace(expected, actual.source)
100
+ node.each_child_node.all? { |child_node| literal?(child_node) }
98
101
  end
99
102
  end
100
103
  end
@@ -38,12 +38,12 @@ module RuboCop
38
38
  RESTRICT_ON_SEND = %i[change].freeze
39
39
 
40
40
  # @!method expect_change_with_arguments(node)
41
- def_node_matcher :expect_change_with_arguments, <<-PATTERN
41
+ def_node_matcher :expect_change_with_arguments, <<~PATTERN
42
42
  (send nil? :change $_ ({sym str} $_))
43
43
  PATTERN
44
44
 
45
45
  # @!method expect_change_with_block(node)
46
- def_node_matcher :expect_change_with_block, <<-PATTERN
46
+ def_node_matcher :expect_change_with_block, <<~PATTERN
47
47
  (block
48
48
  (send nil? :change)
49
49
  (args)
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # @!method expectation(node)
28
28
  def_node_search :expectation, '(send nil? #Expectations.all ...)'
29
29
 
30
- def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
30
+ def on_block(node)
31
31
  return unless hook?(node)
32
32
  return if node.body.nil?
33
33
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Do not use `expect` in let.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # let(:foo) do
11
+ # expect(something).to eq 'foo'
12
+ # end
13
+ #
14
+ # # good
15
+ # it do
16
+ # expect(something).to eq 'foo'
17
+ # end
18
+ #
19
+ class ExpectInLet < Base
20
+ MSG = 'Do not use `%<expect>s` in let'
21
+
22
+ # @!method expectation(node)
23
+ def_node_search :expectation, '(send nil? #Expectations.all ...)'
24
+
25
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
26
+ return unless let?(node)
27
+ return if node.body.nil?
28
+
29
+ expectation(node.body) do |expect|
30
+ add_offense(expect.loc.selector, message: message(expect))
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def message(expect)
37
+ format(MSG, expect: expect.method_name)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -22,10 +22,7 @@ module RuboCop
22
22
  def on_gvasgn(node)
23
23
  return unless inside_example_scope?(node)
24
24
 
25
- # rubocop:disable InternalAffairs/NodeDestructuring
26
- variable_name, _rhs = *node
27
- # rubocop:enable InternalAffairs/NodeDestructuring
28
- name = variable_name[1..]
25
+ name = node.name[1..]
29
26
  return unless name.eql?('stdout') || name.eql?('stderr')
30
27
 
31
28
  add_offense(node.loc.name, message: format(MSG, name: name))
@@ -34,6 +34,18 @@ module RuboCop
34
34
  # # good
35
35
  # describe 'test' do; end
36
36
  #
37
+ # # bad
38
+ # shared_examples 'test', focus: true do; end
39
+ #
40
+ # # good
41
+ # shared_examples 'test' do; end
42
+ #
43
+ # # bad
44
+ # shared_context 'test', focus: true do; end
45
+ #
46
+ # # good
47
+ # shared_context 'test' do; end
48
+ #
37
49
  # # bad (does not support autocorrection)
38
50
  # focus 'test' do; end
39
51
  #
@@ -44,18 +56,19 @@ module RuboCop
44
56
  MSG = 'Focused spec found.'
45
57
 
46
58
  # @!method focusable_selector?(node)
47
- def_node_matcher :focusable_selector?, <<-PATTERN
59
+ def_node_matcher :focusable_selector?, <<~PATTERN
48
60
  {
49
61
  #ExampleGroups.regular
50
62
  #ExampleGroups.skipped
51
63
  #Examples.regular
52
64
  #Examples.skipped
53
65
  #Examples.pending
66
+ #SharedGroups.all
54
67
  }
55
68
  PATTERN
56
69
 
57
70
  # @!method metadata(node)
58
- def_node_matcher :metadata, <<-PATTERN
71
+ def_node_matcher :metadata, <<~PATTERN
59
72
  {(send #rspec? #focusable_selector? <$(sym :focus) ...>)
60
73
  (send #rspec? #focusable_selector? ... (hash <$(pair (sym :focus) true) ...>))}
61
74
  PATTERN
@@ -66,6 +79,8 @@ module RuboCop
66
79
  PATTERN
67
80
 
68
81
  def on_send(node)
82
+ return if node.chained? || node.each_ancestor(:def, :defs).any?
83
+
69
84
  focus_metadata(node) do |focus|
70
85
  add_offense(focus) do |corrector|
71
86
  if focus.pair_type? || focus.str_type? || focus.sym_type?
@@ -66,12 +66,12 @@ module RuboCop
66
66
  EXPLICIT_MSG = 'Use `%<scope>p` for RSpec hooks.'
67
67
 
68
68
  # @!method scoped_hook(node)
69
- def_node_matcher :scoped_hook, <<-PATTERN
69
+ def_node_matcher :scoped_hook, <<~PATTERN
70
70
  ({block numblock} $(send _ #Hooks.all (sym ${:each :example})) ...)
71
71
  PATTERN
72
72
 
73
73
  # @!method unscoped_hook(node)
74
- def_node_matcher :unscoped_hook, <<-PATTERN
74
+ def_node_matcher :unscoped_hook, <<~PATTERN
75
75
  ({block numblock} $(send _ #Hooks.all) ...)
76
76
  PATTERN
77
77
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
  MSG = 'Move `%<hook>s` above the examples in the group.'
29
29
 
30
30
  # @!method example_or_group?(node)
31
- def_node_matcher :example_or_group?, <<-PATTERN
31
+ def_node_matcher :example_or_group?, <<~PATTERN
32
32
  {
33
33
  ({block numblock} {
34
34
  (send #rspec? #ExampleGroups.all ...)
@@ -22,7 +22,7 @@ module RuboCop
22
22
  RESTRICT_ON_SEND = %i[is_expected should should_not].freeze
23
23
 
24
24
  # @!method lambda?(node)
25
- def_node_matcher :lambda?, <<-PATTERN
25
+ def_node_matcher :lambda?, <<~PATTERN
26
26
  {
27
27
  (send (const nil? :Proc) :new)
28
28
  (send nil? {:proc :lambda})
@@ -33,7 +33,7 @@ module RuboCop
33
33
  def_node_matcher :lambda_subject?, '(block #lambda? ...)'
34
34
 
35
35
  # @!method implicit_expect(node)
36
- def_node_matcher :implicit_expect, <<-PATTERN
36
+ def_node_matcher :implicit_expect, <<~PATTERN
37
37
  $(send nil? {:is_expected :should :should_not} ...)
38
38
  PATTERN
39
39
 
@@ -31,7 +31,7 @@ module RuboCop
31
31
  RESTRICT_ON_SEND = Runners.all + %i[should should_not]
32
32
 
33
33
  # @!method implicit_expect(node)
34
- def_node_matcher :implicit_expect, <<-PATTERN
34
+ def_node_matcher :implicit_expect, <<~PATTERN
35
35
  {
36
36
  (send nil? ${:should :should_not} ...)
37
37
  (send (send nil? $:is_expected) #Runners.all ...)
@@ -78,12 +78,12 @@ module RuboCop
78
78
  ].freeze
79
79
 
80
80
  # @!method explicit_unnamed_subject?(node)
81
- def_node_matcher :explicit_unnamed_subject?, <<-PATTERN
81
+ def_node_matcher :explicit_unnamed_subject?, <<~PATTERN
82
82
  (send nil? :expect (send nil? :subject))
83
83
  PATTERN
84
84
 
85
85
  # @!method implicit_subject?(node)
86
- def_node_matcher :implicit_subject?, <<-PATTERN
86
+ def_node_matcher :implicit_subject?, <<~PATTERN
87
87
  (send nil? {:should :should_not :is_expected} ...)
88
88
  PATTERN
89
89
 
@@ -8,6 +8,9 @@ module RuboCop
8
8
  # It makes reading the test harder because it's not clear what exactly
9
9
  # is tested by this particular example.
10
10
  #
11
+ # The configurable options `AllowedIdentifiers` and `AllowedPatterns`
12
+ # will also read those set in `Naming/VariableNumber`.
13
+ #
11
14
  # @example `Max: 1 (default)`
12
15
  # # bad
13
16
  # let(:item_1) { create(:item) }
@@ -31,7 +34,20 @@ module RuboCop
31
34
  # let(:item_1) { create(:item) }
32
35
  # let(:item_2) { create(:item) }
33
36
  #
37
+ # @example `AllowedIdentifiers: ['item_1', 'item_2']`
38
+ # # good
39
+ # let(:item_1) { create(:item) }
40
+ # let(:item_2) { create(:item) }
41
+ #
42
+ # @example `AllowedPatterns: ['item']`
43
+ # # good
44
+ # let(:item_1) { create(:item) }
45
+ # let(:item_2) { create(:item) }
46
+ #
34
47
  class IndexedLet < Base
48
+ include AllowedIdentifiers
49
+ include AllowedPattern
50
+
35
51
  MSG = 'This `let` statement uses index in its name. Please give it ' \
36
52
  'a meaningful name.'
37
53
 
@@ -69,12 +85,27 @@ module RuboCop
69
85
  end
70
86
 
71
87
  def indexed_let?(node)
72
- let?(node) && SUFFIX_INDEX_REGEX.match?(let_name(node))
88
+ let?(node) &&
89
+ SUFFIX_INDEX_REGEX.match?(let_name(node)) &&
90
+ !allowed_identifier?(let_name(node).to_s) &&
91
+ !matches_allowed_pattern?(let_name(node).to_s)
73
92
  end
74
93
 
75
94
  def let_name_stripped_index(node)
76
95
  let_name(node).to_s.gsub(INDEX_REGEX, '')
77
96
  end
97
+
98
+ def cop_config_patterns_values
99
+ Array(config.for_cop('Naming/VariableNumber')
100
+ .fetch('AllowedPatterns', [])) +
101
+ Array(cop_config.fetch('AllowedPatterns', []))
102
+ end
103
+
104
+ def allowed_identifiers
105
+ Array(config.for_cop('Naming/VariableNumber')
106
+ .fetch('AllowedIdentifiers', [])) +
107
+ Array(cop_config.fetch('AllowedIdentifiers', []))
108
+ end
78
109
  end
79
110
  end
80
111
  end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  'with `have_received`.'
26
26
 
27
27
  # @!method null_double(node)
28
- def_node_search :null_double, <<-PATTERN
28
+ def_node_search :null_double, <<~PATTERN
29
29
  (lvasgn $_
30
30
  (send
31
31
  $(send nil? :instance_double
@@ -33,7 +33,7 @@ module RuboCop
33
33
  PATTERN
34
34
 
35
35
  # @!method have_received_usage(node)
36
- def_node_search :have_received_usage, <<-PATTERN
36
+ def_node_search :have_received_usage, <<~PATTERN
37
37
  (send
38
38
  (send nil? :expect
39
39
  (lvar $_)) :to
@@ -26,7 +26,7 @@ module RuboCop
26
26
  # @example with AssignmentOnly configuration
27
27
  # # rubocop.yml
28
28
  # # RSpec/InstanceVariable:
29
- # # AssignmentOnly: false
29
+ # # AssignmentOnly: true
30
30
  #
31
31
  # # bad
32
32
  # describe MyClass do
@@ -48,16 +48,16 @@ module RuboCop
48
48
  class InstanceVariable < Base
49
49
  include TopLevelGroup
50
50
 
51
- MSG = 'Avoid instance variables use let, ' \
51
+ MSG = 'Avoid instance variables - use let, ' \
52
52
  'a method call, or a local variable (if possible).'
53
53
 
54
54
  # @!method dynamic_class?(node)
55
- def_node_matcher :dynamic_class?, <<-PATTERN
55
+ def_node_matcher :dynamic_class?, <<~PATTERN
56
56
  (block (send (const nil? :Class) :new ...) ...)
57
57
  PATTERN
58
58
 
59
59
  # @!method custom_matcher?(node)
60
- def_node_matcher :custom_matcher?, <<-PATTERN
60
+ def_node_matcher :custom_matcher?, <<~PATTERN
61
61
  (block {
62
62
  (send nil? :matcher sym)
63
63
  (send (const (const nil? :RSpec) :Matchers) :define sym)
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Check for `specify` with `is_expected` and one-liner expectations.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # specify { is_expected.to be_truthy }
11
+ #
12
+ # # good
13
+ # it { is_expected.to be_truthy }
14
+ #
15
+ # # good
16
+ # specify do
17
+ # # ...
18
+ # end
19
+ # specify { expect(sqrt(4)).to eq(2) }
20
+ #
21
+ class IsExpectedSpecify < Base
22
+ extend AutoCorrector
23
+
24
+ RESTRICT_ON_SEND = %i[specify].freeze
25
+ IS_EXPECTED_METHODS = ::Set[:is_expected, :are_expected].freeze
26
+ MSG = 'Use `it` instead of `specify`.'
27
+
28
+ # @!method offense?(node)
29
+ def_node_matcher :offense?, <<~PATTERN
30
+ (block (send _ :specify) _ (send (send _ IS_EXPECTED_METHODS) ...))
31
+ PATTERN
32
+
33
+ def on_send(node)
34
+ block_node = node.parent
35
+ return unless block_node&.single_line? && offense?(block_node)
36
+
37
+ selector = node.loc.selector
38
+ add_offense(selector) do |corrector|
39
+ corrector.replace(selector, 'it')
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -21,7 +21,7 @@ module RuboCop
21
21
  'of iterating over an array.'
22
22
 
23
23
  # @!method each?(node)
24
- def_node_matcher :each?, <<-PATTERN
24
+ def_node_matcher :each?, <<~PATTERN
25
25
  (block
26
26
  (send ... :each)
27
27
  (args (arg $_))
@@ -30,14 +30,14 @@ module RuboCop
30
30
  PATTERN
31
31
 
32
32
  # @!method each_numblock?(node)
33
- def_node_matcher :each_numblock?, <<-PATTERN
33
+ def_node_matcher :each_numblock?, <<~PATTERN
34
34
  (numblock
35
35
  (send ... :each) _ $(...)
36
36
  )
37
37
  PATTERN
38
38
 
39
39
  # @!method expectation?(node)
40
- def_node_matcher :expectation?, <<-PATTERN
40
+ def_node_matcher :expectation?, <<~PATTERN
41
41
  (send (send nil? :expect (lvar %)) :to ...)
42
42
  PATTERN
43
43
 
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # Anonymous classes are fine, since they don't result in global
18
18
  # namespace name clashes.
19
19
  #
20
- # @see https://relishapp.com/rspec/rspec-mocks/docs/mutating-constants
20
+ # @see https://rspec.info/features/3-12/rspec-mocks/mutating-constants
21
21
  #
22
22
  # @example Constants leak between examples
23
23
  # # bad
@@ -119,7 +119,7 @@ module RuboCop
119
119
  private
120
120
 
121
121
  def inside_describe_block?(node)
122
- node.each_ancestor(:block).any?(&method(:spec_group?))
122
+ node.each_ancestor(:block).any? { |ancestor| spec_group?(ancestor) }
123
123
  end
124
124
  end
125
125
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  MSG = 'Move `let` before the examples in the group.'
37
37
 
38
38
  # @!method example_or_group?(node)
39
- def_node_matcher :example_or_group?, <<-PATTERN
39
+ def_node_matcher :example_or_group?, <<~PATTERN
40
40
  {
41
41
  (block (send nil? {#ExampleGroups.all #Examples.all} ...) ...)
42
42
  (send nil? #Includes.examples ...)
@@ -51,6 +51,10 @@ module RuboCop
51
51
  }
52
52
  PATTERN
53
53
 
54
+ def self.autocorrect_incompatible_with
55
+ [RSpec::ScatteredLet]
56
+ end
57
+
54
58
  def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
55
59
  return unless example_group_with_body?(node)
56
60
 
@@ -37,7 +37,7 @@ module RuboCop
37
37
  PATTERN
38
38
 
39
39
  # @!method let_bang(node)
40
- def_node_matcher :let_bang, <<-PATTERN
40
+ def_node_matcher :let_bang, <<~PATTERN
41
41
  {
42
42
  (block $(send nil? :let! {(sym $_) (str $_)}) ...)
43
43
  $(send nil? :let! {(sym $_) (str $_)} block_pass)
@@ -29,11 +29,10 @@ module RuboCop
29
29
 
30
30
  MSG = 'Prefer `%<style>s` for setting message expectations.'
31
31
 
32
- SUPPORTED_STYLES = %w[allow expect].freeze
33
32
  RESTRICT_ON_SEND = %i[to].freeze
34
33
 
35
34
  # @!method message_expectation(node)
36
- def_node_matcher :message_expectation, <<-PATTERN
35
+ def_node_matcher :message_expectation, <<~PATTERN
37
36
  (send $(send nil? {:expect :allow} ...) :to #receive_message?)
38
37
  PATTERN
39
38
 
@@ -39,8 +39,6 @@ module RuboCop
39
39
  'expectations. Setup `%<source>s` as a spy using ' \
40
40
  '`allow` or `instance_spy`.'
41
41
 
42
- SUPPORTED_STYLES = %w[have_received receive].freeze
43
-
44
42
  RESTRICT_ON_SEND = Runners.all
45
43
 
46
44
  # @!method message_expectation(node)