rubocop-rspec 1.42.0 → 1.43.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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/config/default.yml +12 -2
  4. data/lib/rubocop-rspec.rb +2 -1
  5. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +1 -1
  6. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +1 -1
  7. data/lib/rubocop/cop/rspec/any_instance.rb +1 -1
  8. data/lib/rubocop/cop/rspec/around_block.rb +1 -1
  9. data/lib/rubocop/cop/rspec/base.rb +74 -0
  10. data/lib/rubocop/cop/rspec/be.rb +1 -1
  11. data/lib/rubocop/cop/rspec/be_eql.rb +1 -1
  12. data/lib/rubocop/cop/rspec/before_after_all.rb +1 -1
  13. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +1 -1
  14. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +6 -3
  15. data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +1 -1
  16. data/lib/rubocop/cop/rspec/context_method.rb +2 -2
  17. data/lib/rubocop/cop/rspec/context_wording.rb +3 -3
  18. data/lib/rubocop/cop/rspec/cop.rb +2 -66
  19. data/lib/rubocop/cop/rspec/describe_class.rb +21 -30
  20. data/lib/rubocop/cop/rspec/describe_method.rb +14 -6
  21. data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -2
  22. data/lib/rubocop/cop/rspec/described_class.rb +2 -2
  23. data/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +1 -1
  24. data/lib/rubocop/cop/rspec/dialect.rb +1 -1
  25. data/lib/rubocop/cop/rspec/empty_example_group.rb +91 -7
  26. data/lib/rubocop/cop/rspec/empty_hook.rb +1 -1
  27. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +4 -8
  28. data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +4 -8
  29. data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +7 -10
  30. data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +4 -8
  31. data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +5 -8
  32. data/lib/rubocop/cop/rspec/example_length.rb +1 -1
  33. data/lib/rubocop/cop/rspec/example_without_description.rb +1 -1
  34. data/lib/rubocop/cop/rspec/example_wording.rb +4 -4
  35. data/lib/rubocop/cop/rspec/expect_actual.rb +1 -1
  36. data/lib/rubocop/cop/rspec/expect_change.rb +1 -1
  37. data/lib/rubocop/cop/rspec/expect_in_hook.rb +1 -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 -3
  40. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +10 -6
  41. data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +1 -1
  42. data/lib/rubocop/cop/rspec/file_path.rb +25 -17
  43. data/lib/rubocop/cop/rspec/focus.rb +7 -11
  44. data/lib/rubocop/cop/rspec/hook_argument.rb +5 -6
  45. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +1 -1
  46. data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +1 -1
  47. data/lib/rubocop/cop/rspec/implicit_expect.rb +1 -1
  48. data/lib/rubocop/cop/rspec/implicit_subject.rb +8 -6
  49. data/lib/rubocop/cop/rspec/instance_spy.rb +1 -1
  50. data/lib/rubocop/cop/rspec/instance_variable.rb +1 -1
  51. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +1 -1
  52. data/lib/rubocop/cop/rspec/it_behaves_like.rb +1 -1
  53. data/lib/rubocop/cop/rspec/iterated_expectation.rb +1 -1
  54. data/lib/rubocop/cop/rspec/leading_subject.rb +20 -17
  55. data/lib/rubocop/cop/rspec/leaky_constant_declaration.rb +1 -1
  56. data/lib/rubocop/cop/rspec/let_before_examples.rb +1 -1
  57. data/lib/rubocop/cop/rspec/let_setup.rb +6 -3
  58. data/lib/rubocop/cop/rspec/message_chain.rb +1 -1
  59. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -1
  60. data/lib/rubocop/cop/rspec/message_spies.rb +1 -1
  61. data/lib/rubocop/cop/rspec/missing_example_group_argument.rb +1 -1
  62. data/lib/rubocop/cop/rspec/multiple_describes.rb +11 -8
  63. data/lib/rubocop/cop/rspec/multiple_expectations.rb +7 -11
  64. data/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +148 -0
  65. data/lib/rubocop/cop/rspec/multiple_subjects.rb +1 -1
  66. data/lib/rubocop/cop/rspec/named_subject.rb +1 -1
  67. data/lib/rubocop/cop/rspec/nested_groups.rb +4 -4
  68. data/lib/rubocop/cop/rspec/not_to_not.rb +1 -1
  69. data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
  70. data/lib/rubocop/cop/rspec/pending.rb +1 -1
  71. data/lib/rubocop/cop/rspec/predicate_matcher.rb +7 -14
  72. data/lib/rubocop/cop/rspec/rails/http_status.rb +1 -1
  73. data/lib/rubocop/cop/rspec/receive_counts.rb +1 -1
  74. data/lib/rubocop/cop/rspec/receive_never.rb +2 -2
  75. data/lib/rubocop/cop/rspec/repeated_description.rb +1 -1
  76. data/lib/rubocop/cop/rspec/repeated_example.rb +2 -2
  77. data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +1 -1
  78. data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +1 -1
  79. data/lib/rubocop/cop/rspec/return_from_stub.rb +1 -1
  80. data/lib/rubocop/cop/rspec/scattered_let.rb +1 -1
  81. data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
  82. data/lib/rubocop/cop/rspec/shared_context.rb +1 -1
  83. data/lib/rubocop/cop/rspec/shared_examples.rb +1 -1
  84. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +1 -1
  85. data/lib/rubocop/cop/rspec/subject_stub.rb +2 -2
  86. data/lib/rubocop/cop/rspec/unspecified_exception.rb +1 -1
  87. data/lib/rubocop/cop/rspec/variable_definition.rb +6 -6
  88. data/lib/rubocop/cop/rspec/variable_name.rb +28 -9
  89. data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -1
  90. data/lib/rubocop/cop/rspec/void_expect.rb +1 -1
  91. data/lib/rubocop/cop/rspec/yield.rb +1 -1
  92. data/lib/rubocop/cop/rspec_cops.rb +1 -0
  93. data/lib/rubocop/rspec/corrector/move_node.rb +7 -5
  94. data/lib/rubocop/rspec/description_extractor.rb +1 -1
  95. data/lib/rubocop/rspec/{blank_line_separation.rb → empty_line_separation.rb} +13 -2
  96. data/lib/rubocop/rspec/example_group.rb +2 -2
  97. data/lib/rubocop/rspec/language.rb +6 -4
  98. data/lib/rubocop/rspec/language/node_pattern.rb +6 -1
  99. data/lib/rubocop/rspec/top_level_describe.rb +2 -2
  100. data/lib/rubocop/rspec/top_level_group.rb +24 -13
  101. data/lib/rubocop/rspec/variable.rb +1 -1
  102. data/lib/rubocop/rspec/version.rb +1 -1
  103. metadata +23 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c377178f53790f52ca57f4e777d0847d69e673e0ca54139e073d7d5f33ef051b
4
- data.tar.gz: baf53df9cbd5041398fc1f41d028a521f0d05a566a765a6198d1015dd31ae9f3
3
+ metadata.gz: afae94ff975b0caf19f7aa7cad514296a15cb28afc2c4ae18dba8bb8446290d2
4
+ data.tar.gz: 15fa2c9138a72d6c54247ea384793abbf9c3d6f8eaa2d7346803c8a296b7335c
5
5
  SHA512:
6
- metadata.gz: 676ea9472397bc4cd47b7777a1e690f036011ebeedd90c5953b4958ca120bd5178d3a669a8b5392af5945fed792e2c3b50438bb048a50a6e4de86387c31942e7
7
- data.tar.gz: d4351174b8a17c0af13be4d4417396450e86e6ef5b4a19b3c87d5b6c7fa582e5ee54b3b4172967907d5202b3ca732b4bf2970bc574652fdfffb760cbf23086ea
6
+ metadata.gz: 4552fb307a57b49ed209ac12a30744b97cfd5bc57460a2a385a16a176903f439083a634e92a8f4faf308c3cca570a79bc8ba85b8a51ceff295920614f1bfe95b
7
+ data.tar.gz: f6b9ed4c0d486fb41c477f6b9d95b7a8897b2011f80bc91edd1a872dca11fce0e5de05387f5e0a3112a0849f371fa93c1eba220019ff5b81ba9959daf06e089c
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.43.0 (2020-08-17)
6
+
7
+ * Add a new base cop class `::RuboCop::Cop::RSpec::Base`. The old base class `::RuboCop::Cop::RSpec::Cop` is deprecated, and will be removed in the next major release. ([@bquorning][])
8
+ * Add support for subject detection after includes and example groups in `RSpec/LeadingSubject`. ([@pirj][])
9
+ * Ignore trailing punctuation in context description prefix. ([@elliterate][])
10
+ * Relax `RSpec/VariableDefinition` cop so interpolated and multiline strings are accepted even when configured to enforce the `symbol` style. ([@bquorning][])
11
+ * Fix `RSpec/EmptyExampleGroup` to flag example groups with examples in invalid scopes. ([@mlarraz][])
12
+ * Fix `RSpec/EmptyExampleGroup` to ignore examples groups with examples defined inside iterators. ([@pirj][])
13
+ * Improve `RSpec/NestedGroups`, `RSpec/FilePath`, `RSpec/DescribeMethod`, `RSpec/MultipleDescribes`, `RSpec/DescribeClass`'s top-level example group detection. ([@pirj][])
14
+ * Add detection of `let!` with a block-pass or a string literal to `RSpec/LetSetup`. ([@pirj][])
15
+ * Add `IgnoredPatterns` configuration option to `RSpec/VariableName`. ([@jtannas][])
16
+ * Add `RSpec/MultipleMemoizedHelpers` cop. ([@mockdeep][])
17
+
5
18
  ## 1.42.0 (2020-07-09)
6
19
 
7
20
  * Update RuboCop dependency to 0.87.0 because of changes to internal APIs. ([@bquorning][], [@Darhazer][])
@@ -529,3 +542,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
529
542
  [@rolfschmidt]: https://github.com/rolfschmidt
530
543
  [@andrykonchin]: https://github.com/andrykonchin
531
544
  [@harrylewis]: https://github.com/harrylewis
545
+ [@elliterate]: https://github.com/elliterate
546
+ [@mlarraz]: https://github.com/mlarraz
547
+ [@jtannas]: https://github.com/jtannas
548
+ [@mockdeep]: https://github.com/mockdeep
@@ -74,7 +74,7 @@ RSpec/ContextWording:
74
74
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextWording
75
75
 
76
76
  RSpec/DescribeClass:
77
- Description: Check that the first argument to the top level describe is a constant.
77
+ Description: Check that the first argument to the top-level describe is a constant.
78
78
  Enabled: true
79
79
  VersionAdded: '1.0'
80
80
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeClass
@@ -381,7 +381,7 @@ RSpec/MissingExampleGroupArgument:
381
381
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MissingExampleGroupArgument
382
382
 
383
383
  RSpec/MultipleDescribes:
384
- Description: Checks for multiple top level describes.
384
+ Description: Checks for multiple top-level example groups.
385
385
  Enabled: true
386
386
  VersionAdded: '1.0'
387
387
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleDescribes
@@ -394,6 +394,14 @@ RSpec/MultipleExpectations:
394
394
  VersionChanged: '1.21'
395
395
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleExpectations
396
396
 
397
+ RSpec/MultipleMemoizedHelpers:
398
+ Description: Checks if example groups contain too many `let` and `subject` calls.
399
+ Enabled: true
400
+ AllowSubject: true
401
+ Max: 5
402
+ VersionAdded: '1.43'
403
+ StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleMemoizedHelpers
404
+
397
405
  RSpec/MultipleSubjects:
398
406
  Description: Checks if an example group defines `subject` multiple times.
399
407
  Enabled: true
@@ -558,7 +566,9 @@ RSpec/VariableName:
558
566
  SupportedStyles:
559
567
  - snake_case
560
568
  - camelCase
569
+ IgnoredPatterns: []
561
570
  VersionAdded: '1.40'
571
+ VersionChanged: '1.43'
562
572
  StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VariableName
563
573
 
564
574
  RSpec/VerifiedDoubles:
@@ -19,11 +19,12 @@ require_relative 'rubocop/rspec/example_group'
19
19
  require_relative 'rubocop/rspec/example'
20
20
  require_relative 'rubocop/rspec/hook'
21
21
  require_relative 'rubocop/rspec/variable'
22
+ require_relative 'rubocop/cop/rspec/base'
22
23
  require_relative 'rubocop/cop/rspec/cop'
23
24
  require_relative 'rubocop/rspec/align_let_brace'
24
25
  require_relative 'rubocop/rspec/factory_bot'
25
26
  require_relative 'rubocop/rspec/final_end_location'
26
- require_relative 'rubocop/rspec/blank_line_separation'
27
+ require_relative 'rubocop/rspec/empty_line_separation'
27
28
  require_relative 'rubocop/rspec/corrector/move_node'
28
29
 
29
30
  RuboCop::RSpec::Inject.defaults!
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # let(:baz) { bar }
18
18
  # let(:a) { b }
19
19
  #
20
- class AlignLeftLetBrace < Cop
20
+ class AlignLeftLetBrace < Base
21
21
  extend AutoCorrector
22
22
 
23
23
  MSG = 'Align left let brace'
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # let(:baz) { bar }
18
18
  # let(:a) { b }
19
19
  #
20
- class AlignRightLetBrace < Cop
20
+ class AlignRightLetBrace < Base
21
21
  extend AutoCorrector
22
22
 
23
23
  MSG = 'Align right let brace'
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # allow(my_instance).to receive(:foo)
23
23
  # end
24
24
  # end
25
- class AnyInstance < Cop
25
+ class AnyInstance < Base
26
26
  MSG = 'Avoid stubbing using `%<method>s`.'
27
27
 
28
28
  def_node_matcher :disallowed_stub, <<-PATTERN
@@ -25,7 +25,7 @@ module RuboCop
25
25
  # some_method
26
26
  # test.run
27
27
  # end
28
- class AroundBlock < Cop
28
+ class AroundBlock < Base
29
29
  MSG_NO_ARG = 'Test object should be passed to around block.'
30
30
  MSG_UNUSED_ARG = 'You should call `%<arg>s.call` '\
31
31
  'or `%<arg>s.run`.'
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # @abstract parent class to RSpec cops
7
+ #
8
+ # The criteria for whether rubocop-rspec analyzes a certain ruby file
9
+ # is configured via `AllCops/RSpec`. For example, if you want to
10
+ # customize your project to scan all files within a `test/` directory
11
+ # then you could add this to your configuration:
12
+ #
13
+ # @example configuring analyzed paths
14
+ # # .rubocop.yml
15
+ # # AllCops:
16
+ # # RSpec:
17
+ # # Patterns:
18
+ # # - '_test.rb$'
19
+ # # - '(?:^|/)test/'
20
+ class Base < ::RuboCop::Cop::Base
21
+ include RuboCop::RSpec::Language
22
+ include RuboCop::RSpec::Language::NodePattern
23
+
24
+ DEFAULT_CONFIGURATION =
25
+ RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')
26
+
27
+ DEFAULT_PATTERN_RE = Regexp.union(
28
+ DEFAULT_CONFIGURATION.fetch('Patterns')
29
+ .map(&Regexp.public_method(:new))
30
+ )
31
+
32
+ # Invoke the original inherited hook so our cops are recognized
33
+ def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
34
+ RuboCop::Cop::Base.inherited(subclass)
35
+ end
36
+
37
+ def relevant_file?(file)
38
+ relevant_rubocop_rspec_file?(file) && super
39
+ end
40
+
41
+ private
42
+
43
+ def relevant_rubocop_rspec_file?(file)
44
+ rspec_pattern.match?(file)
45
+ end
46
+
47
+ def rspec_pattern
48
+ if rspec_pattern_config?
49
+ Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new)))
50
+ else
51
+ DEFAULT_PATTERN_RE
52
+ end
53
+ end
54
+
55
+ def all_cops_config
56
+ config
57
+ .for_all_cops
58
+ end
59
+
60
+ def rspec_pattern_config?
61
+ return unless all_cops_config.key?('RSpec')
62
+
63
+ all_cops_config.fetch('RSpec').key?('Patterns')
64
+ end
65
+
66
+ def rspec_pattern_config
67
+ all_cops_config
68
+ .fetch('RSpec', DEFAULT_CONFIGURATION)
69
+ .fetch('Patterns')
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -19,7 +19,7 @@ module RuboCop
19
19
  # expect(foo).to be 1.0
20
20
  # expect(foo).to be(true)
21
21
  #
22
- class Be < Cop
22
+ class Be < Base
23
23
  MSG = 'Don\'t use `be` without an argument.'
24
24
 
25
25
  def_node_matcher :be_without_args, <<-PATTERN
@@ -35,7 +35,7 @@ module RuboCop
35
35
  # necessarily the same type as `b` since the `#==` operator can
36
36
  # coerce objects for comparison.
37
37
  #
38
- class BeEql < Cop
38
+ class BeEql < Base
39
39
  extend AutoCorrector
40
40
 
41
41
  MSG = 'Prefer `be` over `eql`.'
@@ -23,7 +23,7 @@ module RuboCop
23
23
  # before(:each) { Widget.create }
24
24
  # after(:each) { Widget.delete_all }
25
25
  # end
26
- class BeforeAfterAll < Cop
26
+ class BeforeAfterAll < Base
27
27
  MSG = 'Beware of using `%<hook>s` as it may cause state to leak '\
28
28
  'between tests. If you are using `rspec-rails`, and '\
29
29
  '`use_transactional_fixtures` is enabled, then records created '\
@@ -23,7 +23,7 @@ module RuboCop
23
23
  # expect(page).to have_current_path("/callback")
24
24
  # expect(page).to have_current_path(/widgets/)
25
25
  #
26
- class CurrentPathExpectation < Cop
26
+ class CurrentPathExpectation < Base
27
27
  extend AutoCorrector
28
28
 
29
29
  MSG = 'Do not set an RSpec expectation on `current_path` in ' \
@@ -40,7 +40,7 @@ module RuboCop
40
40
  # # ...
41
41
  # end
42
42
  # end
43
- class FeatureMethods < Cop
43
+ class FeatureMethods < Base
44
44
  extend AutoCorrector
45
45
 
46
46
  MSG = 'Use `%<replacement>s` instead of `%<method>s`.'
@@ -55,15 +55,18 @@ module RuboCop
55
55
  feature: :describe
56
56
  }.freeze
57
57
 
58
+ def_node_matcher :capybara_speak,
59
+ SelectorSet.new(MAP.keys).node_pattern_union
60
+
58
61
  def_node_matcher :spec?, <<-PATTERN
59
62
  (block
60
- (send #{RSPEC} {:describe :feature} ...)
63
+ (send #rspec? {:describe :feature} ...)
61
64
  ...)
62
65
  PATTERN
63
66
 
64
67
  def_node_matcher :feature_method, <<-PATTERN
65
68
  (block
66
- $(send #{RSPEC} ${#{MAP.keys.map(&:inspect).join(' ')}} ...)
69
+ $(send #rspec? $#capybara_speak ...)
67
70
  ...)
68
71
  PATTERN
69
72
 
@@ -26,7 +26,7 @@ module RuboCop
26
26
  # expect(page).to have_css('.foo', visible: :all)
27
27
  # expect(page).to have_link('my link', visible: :hidden)
28
28
  #
29
- class VisibilityMatcher < Cop
29
+ class VisibilityMatcher < Base
30
30
  MSG_FALSE = 'Use `:all` or `:hidden` instead of `false`.'
31
31
  MSG_TRUE = 'Use `:visible` instead of `true`.'
32
32
  CAPYBARA_MATCHER_METHODS = %i[
@@ -23,13 +23,13 @@ module RuboCop
23
23
  # describe '.foo_bar' do
24
24
  # # ...
25
25
  # end
26
- class ContextMethod < Cop
26
+ class ContextMethod < Base
27
27
  extend AutoCorrector
28
28
 
29
29
  MSG = 'Use `describe` for testing methods.'
30
30
 
31
31
  def_node_matcher :context_method, <<-PATTERN
32
- (block (send #{RSPEC} :context $(str #method_name?) ...) ...)
32
+ (block (send #rspec? :context $(str #method_name?) ...) ...)
33
33
  PATTERN
34
34
 
35
35
  def on_block(node)
@@ -34,11 +34,11 @@ module RuboCop
34
34
  # context 'when the display name is not present' do
35
35
  # # ...
36
36
  # end
37
- class ContextWording < Cop
37
+ class ContextWording < Base
38
38
  MSG = 'Start context description with %<prefixes>s.'
39
39
 
40
40
  def_node_matcher :context_wording, <<-PATTERN
41
- (block (send #{RSPEC} { :context :shared_context } $(str #bad_prefix?) ...) ...)
41
+ (block (send #rspec? { :context :shared_context } $(str #bad_prefix?) ...) ...)
42
42
  PATTERN
43
43
 
44
44
  def on_block(node)
@@ -51,7 +51,7 @@ module RuboCop
51
51
  private
52
52
 
53
53
  def bad_prefix?(description)
54
- !prefixes.include?(description.split.first)
54
+ !prefixes.include?(description.split(/\b/).first)
55
55
  end
56
56
 
57
57
  def joined_prefixes
@@ -3,72 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module RSpec
6
- # @abstract parent class to RSpec cops
7
- #
8
- # The criteria for whether rubocop-rspec analyzes a certain ruby file
9
- # is configured via `AllCops/RSpec`. For example, if you want to
10
- # customize your project to scan all files within a `test/` directory
11
- # then you could add this to your configuration:
12
- #
13
- # @example configuring analyzed paths
14
- # # .rubocop.yml
15
- # # AllCops:
16
- # # RSpec:
17
- # # Patterns:
18
- # # - '_test.rb$'
19
- # # - '(?:^|/)test/'
20
- class Cop < ::RuboCop::Cop::Base
21
- include RuboCop::RSpec::Language
22
- include RuboCop::RSpec::Language::NodePattern
23
-
24
- DEFAULT_CONFIGURATION =
25
- RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')
26
-
27
- DEFAULT_PATTERN_RE = Regexp.union(
28
- DEFAULT_CONFIGURATION.fetch('Patterns')
29
- .map(&Regexp.public_method(:new))
30
- )
31
-
32
- # Invoke the original inherited hook so our cops are recognized
33
- def self.inherited(subclass)
34
- RuboCop::Cop::Cop.inherited(subclass)
35
- end
36
-
37
- def relevant_file?(file)
38
- relevant_rubocop_rspec_file?(file) && super
39
- end
40
-
41
- private
42
-
43
- def relevant_rubocop_rspec_file?(file)
44
- rspec_pattern =~ file
45
- end
46
-
47
- def rspec_pattern
48
- if rspec_pattern_config?
49
- Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new)))
50
- else
51
- DEFAULT_PATTERN_RE
52
- end
53
- end
54
-
55
- def all_cops_config
56
- config
57
- .for_all_cops
58
- end
59
-
60
- def rspec_pattern_config?
61
- return unless all_cops_config.key?('RSpec')
62
-
63
- all_cops_config.fetch('RSpec').key?('Patterns')
64
- end
65
-
66
- def rspec_pattern_config
67
- all_cops_config
68
- .fetch('RSpec', DEFAULT_CONFIGURATION)
69
- .fetch('Patterns')
70
- end
71
- end
6
+ # @deprecated Use ::RuboCop::Cop::RSpec::Base instead
7
+ Cop = Base
72
8
  end
73
9
  end
74
10
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module RSpec
6
- # Check that the first argument to the top level describe is a constant.
6
+ # Check that the first argument to the top-level describe is a constant.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -21,50 +21,41 @@ module RuboCop
21
21
  #
22
22
  # describe "A feature example", type: :feature do
23
23
  # end
24
- class DescribeClass < Cop
25
- include RuboCop::RSpec::TopLevelDescribe
24
+ class DescribeClass < Base
25
+ include RuboCop::RSpec::TopLevelGroup
26
26
 
27
27
  MSG = 'The first argument to describe should be '\
28
28
  'the class or module being tested.'
29
29
 
30
- def_node_matcher :valid_describe?, <<-PATTERN
31
- {
32
- (send #{RSPEC} :describe const ...)
33
- (send #{RSPEC} :describe)
34
- }
35
- PATTERN
36
-
37
- def_node_matcher :describe_with_rails_metadata?, <<-PATTERN
38
- (send #{RSPEC} :describe !const ...
39
- (hash <#rails_metadata? ...>)
40
- )
41
- PATTERN
42
-
43
30
  def_node_matcher :rails_metadata?, <<-PATTERN
44
31
  (pair
45
32
  (sym :type)
46
- (sym {
47
- :channel :controller :helper :job :mailer :model :request
48
- :routing :view :feature :system :mailbox
49
- }
50
- )
33
+ (sym { :channel :controller :helper :job :mailer :model :request
34
+ :routing :view :feature :system :mailbox })
51
35
  )
52
36
  PATTERN
53
37
 
54
- def on_top_level_describe(node, (described_value, _))
55
- return if shared_group?(root_node)
56
- return if valid_describe?(node)
57
- return if describe_with_rails_metadata?(node)
58
- return if string_constant_describe?(described_value)
38
+ def_node_matcher :example_group_with_rails_metadata?, <<~PATTERN
39
+ (send #rspec? :describe ... (hash <#rails_metadata? ...>))
40
+ PATTERN
41
+
42
+ def_node_matcher :not_a_const_described, <<~PATTERN
43
+ (send #rspec? :describe $[!const !#string_constant?] ...)
44
+ PATTERN
45
+
46
+ def on_top_level_group(top_level_node)
47
+ return if example_group_with_rails_metadata?(top_level_node.send_node)
59
48
 
60
- add_offense(described_value)
49
+ not_a_const_described(top_level_node.send_node) do |described|
50
+ add_offense(described)
51
+ end
61
52
  end
62
53
 
63
54
  private
64
55
 
65
- def string_constant_describe?(described_value)
66
- described_value.str_type? &&
67
- described_value.value =~ /^((::)?[A-Z]\w*)+$/
56
+ def string_constant?(described)
57
+ described.str_type? &&
58
+ described.value.match?(/^(?:(?:::)?[A-Z]\w*)+$/)
68
59
  end
69
60
  end
70
61
  end