rubocop-rspec 2.1.0 → 2.2.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 (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/config/default.yml +1 -0
  4. data/lib/rubocop-rspec.rb +1 -0
  5. data/lib/rubocop/cop/rspec/any_instance.rb +6 -10
  6. data/lib/rubocop/cop/rspec/be_eql.rb +1 -0
  7. data/lib/rubocop/cop/rspec/before_after_all.rb +2 -0
  8. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +2 -0
  9. data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +2 -0
  10. data/lib/rubocop/cop/rspec/describe_symbol.rb +1 -0
  11. data/lib/rubocop/cop/rspec/expect_change.rb +1 -0
  12. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +1 -0
  13. data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +1 -0
  14. data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +1 -0
  15. data/lib/rubocop/cop/rspec/implicit_subject.rb +1 -0
  16. data/lib/rubocop/cop/rspec/it_behaves_like.rb +1 -0
  17. data/lib/rubocop/cop/rspec/message_chain.rb +4 -10
  18. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -0
  19. data/lib/rubocop/cop/rspec/mixin/comments_help.rb +38 -0
  20. data/lib/rubocop/cop/rspec/not_to_not.rb +1 -0
  21. data/lib/rubocop/cop/rspec/rails/http_status.rb +1 -0
  22. data/lib/rubocop/cop/rspec/receive_counts.rb +2 -0
  23. data/lib/rubocop/cop/rspec/receive_never.rb +1 -0
  24. data/lib/rubocop/cop/rspec/return_from_stub.rb +1 -0
  25. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +1 -0
  26. data/lib/rubocop/cop/rspec/unspecified_exception.rb +1 -0
  27. data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -0
  28. data/lib/rubocop/cop/rspec/void_expect.rb +1 -0
  29. data/lib/rubocop/rspec/corrector/move_node.rb +6 -9
  30. data/lib/rubocop/rspec/version.rb +1 -1
  31. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb3dfbd46088f3d66b1af6e41c171aac15d8b28e5aae7f5c1eb43f770b47f423
4
- data.tar.gz: 59ff08781d18888d37c3776209d3f90f0d7eee582a9cc03521048044c2ccf007
3
+ metadata.gz: c1531e973244c7aac66856335d340dee502696e988f9816bf74e7f5555d80d90
4
+ data.tar.gz: b634d15c53e126f497928eca6adfa6fae08b095ccb1a93f3d906826bca56efd4
5
5
  SHA512:
6
- metadata.gz: 1446d96d3c63a526712f611a283bdf4758e39b7be63ae3c49e8aa66f916b55e4e495a5fae7bdd3ec5974189a1f6a5b4db211da58c031c2f5984703dff5f12cc3
7
- data.tar.gz: c3ebd0c0cbc31647165384f04433fd62644d16aebf83ce1036770705900288cc0fda9c64ff6902f2ac36e23700862f420ef38d8693f641a86ee0dbed44733e34
6
+ metadata.gz: e91e8656f9f82f02fa041d5795d01eba023460aad4e3c06b55167b3f92fc0d22324250e1d116eb63c3b60e915240347f0a36cbe14c8b3752f7c6a63ea9949bd4
7
+ data.tar.gz: 8c470782145e23962f3653beb13b4543d461a3de9bbea201d5fb0c66fc8bfebb5695afad7bc545b37dba664362dee8edb0f579441f0bafcd963ab86f503b0415
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 2.2.0 (2021-02-02)
6
+
7
+ * Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])
8
+ * Improve rubocop-rspec performance. ([@Darhazer][], [@bquorning][])
9
+ * Include `Enabled: true` to prevent a mismatched configuration parameter warning when `RSpec` cops are explicitly enabled in the user configuration. ([@pirj][])
10
+
5
11
  ## 2.1.0 (2020-12-17)
6
12
 
7
13
  * Fix `RSpec/FilePath` false positive for relative file path runs with long namespaces. ([@ahukkanen][])
data/config/default.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  RSpec:
3
+ Enabled: true
3
4
  Include:
4
5
  - "**/*_spec.rb"
5
6
  - "**/spec/**/*"
data/lib/rubocop-rspec.rb CHANGED
@@ -15,6 +15,7 @@ require_relative 'rubocop/rspec/language'
15
15
  require_relative 'rubocop/cop/rspec/mixin/top_level_group'
16
16
  require_relative 'rubocop/cop/rspec/mixin/variable'
17
17
  require_relative 'rubocop/cop/rspec/mixin/final_end_location'
18
+ require_relative 'rubocop/cop/rspec/mixin/comments_help'
18
19
  require_relative 'rubocop/cop/rspec/mixin/empty_line_separation'
19
20
 
20
21
  require_relative 'rubocop/rspec/concept'
@@ -24,18 +24,14 @@ module RuboCop
24
24
  # end
25
25
  class AnyInstance < Base
26
26
  MSG = 'Avoid stubbing using `%<method>s`.'
27
-
28
- def_node_matcher :disallowed_stub, <<-PATTERN
29
- (send _ ${:any_instance :allow_any_instance_of :expect_any_instance_of} ...)
30
- PATTERN
27
+ RESTRICT_ON_SEND = %i[
28
+ any_instance
29
+ allow_any_instance_of
30
+ expect_any_instance_of
31
+ ].freeze
31
32
 
32
33
  def on_send(node)
33
- disallowed_stub(node) do |method|
34
- add_offense(
35
- node,
36
- message: format(MSG, method: method)
37
- )
38
- end
34
+ add_offense(node, message: format(MSG, method: node.method_name))
39
35
  end
40
36
  end
41
37
  end
@@ -39,6 +39,7 @@ module RuboCop
39
39
  extend AutoCorrector
40
40
 
41
41
  MSG = 'Prefer `be` over `eql`.'
42
+ RESTRICT_ON_SEND = %i[to].freeze
42
43
 
43
44
  def_node_matcher :eql_type_with_identity, <<-PATTERN
44
45
  (send _ :to $(send nil? :eql {true false int float sym nil_type?}))
@@ -29,6 +29,8 @@ module RuboCop
29
29
  '`use_transactional_fixtures` is enabled, then records created ' \
30
30
  'in `%<hook>s` are not automatically rolled back.'
31
31
 
32
+ RESTRICT_ON_SEND = %i[before after].freeze
33
+
32
34
  def_node_matcher :before_or_after_all, <<-PATTERN
33
35
  $(send _ {:before :after} (sym {:all :context}))
34
36
  PATTERN
@@ -30,6 +30,8 @@ module RuboCop
30
30
  'Capybara feature specs - instead, use the ' \
31
31
  '`have_current_path` matcher on `page`'
32
32
 
33
+ RESTRICT_ON_SEND = %i[expect].freeze
34
+
33
35
  def_node_matcher :expectation_set_on_current_path, <<-PATTERN
34
36
  (send nil? :expect (send {(send nil? :page) nil?} :current_path))
35
37
  PATTERN
@@ -44,6 +44,8 @@ module RuboCop
44
44
  have_content
45
45
  ].freeze
46
46
 
47
+ RESTRICT_ON_SEND = CAPYBARA_MATCHER_METHODS
48
+
47
49
  def_node_matcher :visible_true?, <<~PATTERN
48
50
  (send nil? #capybara_matcher? ... (hash <$(pair (sym :visible) true) ...>))
49
51
  PATTERN
@@ -19,6 +19,7 @@ module RuboCop
19
19
  # @see https://github.com/rspec/rspec-core/issues/1610
20
20
  class DescribeSymbol < Base
21
21
  MSG = 'Avoid describing symbols.'
22
+ RESTRICT_ON_SEND = %i[describe].freeze
22
23
 
23
24
  def_node_matcher :describe_symbol?, <<-PATTERN
24
25
  (send #rspec? :describe $sym ...)
@@ -35,6 +35,7 @@ module RuboCop
35
35
 
36
36
  MSG_BLOCK = 'Prefer `change(%<obj>s, :%<attr>s)`.'
37
37
  MSG_CALL = 'Prefer `change { %<obj>s.%<attr>s }`.'
38
+ RESTRICT_ON_SEND = %i[change].freeze
38
39
 
39
40
  def_node_matcher :expect_change_with_arguments, <<-PATTERN
40
41
  (send nil? :change ({const send} nil? $_) (sym $_))
@@ -30,6 +30,7 @@ module RuboCop
30
30
 
31
31
  MSG_CREATE_LIST = 'Prefer create_list.'
32
32
  MSG_N_TIMES = 'Prefer %<number>s.times.'
33
+ RESTRICT_ON_SEND = %i[create_list].freeze
33
34
 
34
35
  def_node_matcher :n_times_block_without_arg?, <<-PATTERN
35
36
  (block
@@ -25,6 +25,7 @@ module RuboCop
25
25
  MSG = "Pass '%<class_name>s' string instead of `%<class_name>s` " \
26
26
  'constant.'
27
27
  ALLOWED_CONSTANTS = %w[Hash OpenStruct].freeze
28
+ RESTRICT_ON_SEND = %i[factory].freeze
28
29
 
29
30
  def_node_matcher :class_name, <<~PATTERN
30
31
  (send _ :factory _ (hash <(pair (sym :class) $(const ...)) ...>))
@@ -18,6 +18,7 @@ module RuboCop
18
18
  # end
19
19
  class ImplicitBlockExpectation < Base
20
20
  MSG = 'Avoid implicit block expectations.'
21
+ RESTRICT_ON_SEND = %i[is_expected should should_not].freeze
21
22
 
22
23
  def_node_matcher :lambda?, <<-PATTERN
23
24
  {
@@ -31,6 +31,7 @@ module RuboCop
31
31
  include ConfigurableEnforcedStyle
32
32
 
33
33
  MSG = "Don't use implicit subject."
34
+ RESTRICT_ON_SEND = %i[is_expected should should_not].freeze
34
35
 
35
36
  def_node_matcher :implicit_subject?, <<-PATTERN
36
37
  (send nil? {:should :should_not :is_expected} ...)
@@ -24,6 +24,7 @@ module RuboCop
24
24
 
25
25
  MSG = 'Prefer `%<replacement>s` over `%<original>s` when including ' \
26
26
  'examples in a nested context.'
27
+ RESTRICT_ON_SEND = %i[it_behaves_like it_should_behave_like].freeze
27
28
 
28
29
  def_node_matcher :example_inclusion_offense, '(send _ % ...)'
29
30
 
@@ -15,18 +15,12 @@ module RuboCop
15
15
  #
16
16
  class MessageChain < Base
17
17
  MSG = 'Avoid stubbing using `%<method>s`.'
18
-
19
- def_node_matcher :message_chain, <<-PATTERN
20
- (send _ {:receive_message_chain :stub_chain} ...)
21
- PATTERN
18
+ RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze
22
19
 
23
20
  def on_send(node)
24
- message_chain(node) do
25
- add_offense(
26
- node.loc.selector,
27
- message: format(MSG, method: node.method_name)
28
- )
29
- end
21
+ add_offense(
22
+ node.loc.selector, message: format(MSG, method: node.method_name)
23
+ )
30
24
  end
31
25
  end
32
26
  end
@@ -30,6 +30,7 @@ module RuboCop
30
30
  MSG = 'Prefer `%<style>s` for setting message expectations.'
31
31
 
32
32
  SUPPORTED_STYLES = %w[allow expect].freeze
33
+ RESTRICT_ON_SEND = %i[to].freeze
33
34
 
34
35
  def_node_matcher :message_expectation, <<-PATTERN
35
36
  (send $(send nil? {:expect :allow} ...) :to #receive_message?)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Help methods for working with nodes containing comments.
7
+ module CommentsHelp
8
+ include FinalEndLocation
9
+
10
+ def source_range_with_comment(node)
11
+ begin_pos = begin_pos_with_comment(node).begin_pos
12
+ end_pos = end_line_position(node).end_pos
13
+
14
+ Parser::Source::Range.new(buffer, begin_pos, end_pos)
15
+ end
16
+
17
+ def begin_pos_with_comment(node)
18
+ first_comment = processed_source.ast_with_comments[node].first
19
+
20
+ start_line_position(first_comment || node)
21
+ end
22
+
23
+ def start_line_position(node)
24
+ buffer.line_range(node.loc.line)
25
+ end
26
+
27
+ def end_line_position(node)
28
+ end_line = buffer.line_for_position(final_end_location(node).end_pos)
29
+ buffer.line_range(end_line)
30
+ end
31
+
32
+ def buffer
33
+ processed_source.buffer
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -20,6 +20,7 @@ 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
 
24
25
  def_node_matcher :not_to_not_offense, '(send _ % ...)'
25
26
 
@@ -33,6 +33,7 @@ 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
 
37
38
  def_node_matcher :http_status, <<-PATTERN
38
39
  (send nil? :have_http_status ${int sym})
@@ -28,6 +28,8 @@ module RuboCop
28
28
 
29
29
  MSG = 'Use `%<alternative>s` instead of `%<original>s`.'
30
30
 
31
+ RESTRICT_ON_SEND = %i[times].freeze
32
+
31
33
  def_node_matcher :receive_counts, <<-PATTERN
32
34
  (send $(send _ {:exactly :at_least :at_most} (int {1 2})) :times)
33
35
  PATTERN
@@ -16,6 +16,7 @@ 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
 
20
21
  def_node_search :method_on_stub?, '(send nil? :receive ...)'
21
22
 
@@ -39,6 +39,7 @@ 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
 
43
44
  def_node_search :contains_stub?, '(send nil? :receive (...))'
44
45
  def_node_matcher :stub_with_block?, '(block #contains_stub? ...)'
@@ -21,6 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG = 'Use `%<recommended>s` instead of calling ' \
23
23
  '`%<called>s` with a single argument.'
24
+ RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze
24
25
 
25
26
  def_node_matcher :message_chain, <<-PATTERN
26
27
  (send _ {:receive_message_chain :stub_chain} $_)
@@ -32,6 +32,7 @@ module RuboCop
32
32
  # expect { do_something }.not_to raise_error
33
33
  class UnspecifiedException < Base
34
34
  MSG = 'Specify the exception being captured'
35
+ RESTRICT_ON_SEND = %i[to].freeze
35
36
 
36
37
  def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
37
38
  (send
@@ -24,6 +24,7 @@ module RuboCop
24
24
  # end
25
25
  class VerifiedDoubles < Base
26
26
  MSG = 'Prefer using verifying doubles over normal doubles.'
27
+ RESTRICT_ON_SEND = %i[double spy].freeze
27
28
 
28
29
  def_node_matcher :unverified_double, <<-PATTERN
29
30
  {(send nil? {:double :spy} $...)}
@@ -14,6 +14,7 @@ module RuboCop
14
14
  class VoidExpect < Base
15
15
  MSG = 'Do not use `expect()` without `.to` or `.not_to`. ' \
16
16
  'Chain the methods or remove it.'
17
+ RESTRICT_ON_SEND = %i[expect].freeze
17
18
 
18
19
  def_node_matcher :expect?, <<-PATTERN
19
20
  (send nil? :expect ...)
@@ -6,6 +6,7 @@ module RuboCop
6
6
  # Helper methods to move a node
7
7
  class MoveNode
8
8
  include RuboCop::Cop::RangeHelp
9
+ include RuboCop::Cop::RSpec::CommentsHelp
9
10
  include RuboCop::Cop::RSpec::FinalEndLocation
10
11
 
11
12
  attr_reader :original, :corrector, :processed_source
@@ -17,20 +18,16 @@ module RuboCop
17
18
  end
18
19
 
19
20
  def move_before(other)
20
- position = other.loc.expression
21
- indent = ' ' * other.loc.column
22
- newline_indent = "\n#{indent}"
21
+ position = start_line_position(other)
23
22
 
24
- corrector.insert_before(position, source(original) + newline_indent)
23
+ corrector.insert_before(position, "#{source(original)}\n")
25
24
  corrector.remove(node_range_with_surrounding_space(original))
26
25
  end
27
26
 
28
27
  def move_after(other)
29
- position = final_end_location(other)
30
- indent = ' ' * other.loc.column
31
- newline_indent = "\n#{indent}"
28
+ position = end_line_position(other)
32
29
 
33
- corrector.insert_after(position, newline_indent + source(original))
30
+ corrector.insert_after(position, "\n#{source(original)}")
34
31
  corrector.remove(node_range_with_surrounding_space(original))
35
32
  end
36
33
 
@@ -41,7 +38,7 @@ module RuboCop
41
38
  end
42
39
 
43
40
  def node_range(node)
44
- node.loc.expression.with(end_pos: final_end_location(node).end_pos)
41
+ source_range_with_comment(node)
45
42
  end
46
43
 
47
44
  def node_range_with_surrounding_space(node)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module RSpec
5
5
  # Version information for the RSpec RuboCop plugin.
6
6
  module Version
7
- STRING = '2.1.0'
7
+ STRING = '2.2.0'
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Backus
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-12-17 00:00:00.000000000 Z
13
+ date: 2021-02-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -198,6 +198,7 @@ files:
198
198
  - lib/rubocop/cop/rspec/message_expectation.rb
199
199
  - lib/rubocop/cop/rspec/message_spies.rb
200
200
  - lib/rubocop/cop/rspec/missing_example_group_argument.rb
201
+ - lib/rubocop/cop/rspec/mixin/comments_help.rb
201
202
  - lib/rubocop/cop/rspec/mixin/empty_line_separation.rb
202
203
  - lib/rubocop/cop/rspec/mixin/final_end_location.rb
203
204
  - lib/rubocop/cop/rspec/mixin/top_level_group.rb