rubocop-rspec 1.41.0 → 1.42.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +11 -18
  4. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +11 -18
  5. data/lib/rubocop/cop/rspec/be.rb +1 -1
  6. data/lib/rubocop/cop/rspec/be_eql.rb +5 -5
  7. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +18 -16
  8. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +8 -9
  9. data/lib/rubocop/cop/rspec/context_method.rb +5 -7
  10. data/lib/rubocop/cop/rspec/cop.rb +1 -1
  11. data/lib/rubocop/cop/rspec/described_class.rb +10 -7
  12. data/lib/rubocop/cop/rspec/dialect.rb +4 -11
  13. data/lib/rubocop/cop/rspec/empty_hook.rb +5 -9
  14. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +5 -3
  15. data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +5 -5
  16. data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +4 -1
  17. data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +5 -5
  18. data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +4 -1
  19. data/lib/rubocop/cop/rspec/example_wording.rb +6 -7
  20. data/lib/rubocop/cop/rspec/expect_actual.rb +7 -10
  21. data/lib/rubocop/cop/rspec/expect_change.rb +9 -34
  22. data/lib/rubocop/cop/rspec/expect_in_hook.rb +2 -2
  23. data/lib/rubocop/cop/rspec/expect_output.rb +1 -1
  24. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +18 -18
  25. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +10 -16
  26. data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +6 -7
  27. data/lib/rubocop/cop/rspec/hook_argument.rb +11 -17
  28. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +12 -13
  29. data/lib/rubocop/cop/rspec/implicit_expect.rb +6 -14
  30. data/lib/rubocop/cop/rspec/implicit_subject.rb +8 -5
  31. data/lib/rubocop/cop/rspec/instance_spy.rb +17 -11
  32. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +2 -5
  33. data/lib/rubocop/cop/rspec/it_behaves_like.rb +4 -5
  34. data/lib/rubocop/cop/rspec/leading_subject.rb +13 -13
  35. data/lib/rubocop/cop/rspec/let_before_examples.rb +12 -10
  36. data/lib/rubocop/cop/rspec/message_chain.rb +6 -5
  37. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -1
  38. data/lib/rubocop/cop/rspec/message_spies.rb +1 -2
  39. data/lib/rubocop/cop/rspec/multiple_subjects.rb +17 -18
  40. data/lib/rubocop/cop/rspec/named_subject.rb +1 -1
  41. data/lib/rubocop/cop/rspec/not_to_not.rb +4 -5
  42. data/lib/rubocop/cop/rspec/predicate_matcher.rb +23 -53
  43. data/lib/rubocop/cop/rspec/rails/http_status.rb +4 -8
  44. data/lib/rubocop/cop/rspec/receive_counts.rb +14 -16
  45. data/lib/rubocop/cop/rspec/receive_never.rb +10 -10
  46. data/lib/rubocop/cop/rspec/return_from_stub.rb +8 -19
  47. data/lib/rubocop/cop/rspec/scattered_let.rb +7 -10
  48. data/lib/rubocop/cop/rspec/shared_context.rb +7 -20
  49. data/lib/rubocop/cop/rspec/shared_examples.rb +5 -8
  50. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +14 -17
  51. data/lib/rubocop/cop/rspec/yield.rb +13 -10
  52. data/lib/rubocop/rspec/blank_line_separation.rb +0 -8
  53. data/lib/rubocop/rspec/version.rb +1 -1
  54. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4899c717a87d579939d5c1420800d2fe6c150e4421908e75b9d52c57c8362e60
4
- data.tar.gz: 1155018e6595c843c1ade5cdecb748af349c1c2f94dbcd5b17fb4a4fb5d324be
3
+ metadata.gz: c377178f53790f52ca57f4e777d0847d69e673e0ca54139e073d7d5f33ef051b
4
+ data.tar.gz: baf53df9cbd5041398fc1f41d028a521f0d05a566a765a6198d1015dd31ae9f3
5
5
  SHA512:
6
- metadata.gz: 79d9dc291018b0551b52c85dc6b09a1151356e1c85fe4e6a8a7aca5ade036be690f9b5aae7301cf232a0d067b18c0321be44f9915d5485dcaf993786ed07d328
7
- data.tar.gz: cd9db102d912c42b484c7a514f5954b5f35d29bb7f0f3e186ed946898ba1134de5bf452611812465103aa2effc67016e4536bfc6dd365681aa90b2f4c1957f39
6
+ metadata.gz: 676ea9472397bc4cd47b7777a1e690f036011ebeedd90c5953b4958ca120bd5178d3a669a8b5392af5945fed792e2c3b50438bb048a50a6e4de86387c31942e7
7
+ data.tar.gz: d4351174b8a17c0af13be4d4417396450e86e6ef5b4a19b3c87d5b6c7fa582e5ee54b3b4172967907d5202b3ca732b4bf2970bc574652fdfffb760cbf23086ea
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.42.0 (2020-07-09)
6
+
7
+ * Update RuboCop dependency to 0.87.0 because of changes to internal APIs. ([@bquorning][], [@Darhazer][])
8
+
5
9
  ## 1.41.0 (2020-07-03)
6
10
 
7
11
  * Extend the list of Rails spec types for `RSpec/DescribeClass`. ([@pirj][])
@@ -18,35 +18,28 @@ module RuboCop
18
18
  # let(:a) { b }
19
19
  #
20
20
  class AlignLeftLetBrace < Cop
21
+ extend AutoCorrector
22
+
21
23
  MSG = 'Align left let brace'
22
24
 
23
25
  def self.autocorrect_incompatible_with
24
26
  [Layout::ExtraSpacing]
25
27
  end
26
28
 
27
- def investigate(_processed_source)
29
+ def on_new_investigation
28
30
  return if processed_source.blank?
29
31
 
30
- token_aligner.offending_tokens.each do |let|
31
- add_offense(let, location: :begin)
32
- end
33
- end
32
+ token_aligner =
33
+ RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :begin)
34
34
 
35
- def autocorrect(let)
36
- lambda do |corrector|
37
- corrector.insert_before(
38
- let.loc.begin,
39
- token_aligner.indent_for(let)
40
- )
35
+ token_aligner.offending_tokens.each do |let|
36
+ add_offense(let.loc.begin) do |corrector|
37
+ corrector.insert_before(
38
+ let.loc.begin, token_aligner.indent_for(let)
39
+ )
40
+ end
41
41
  end
42
42
  end
43
-
44
- private
45
-
46
- def token_aligner
47
- @token_aligner ||=
48
- RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :begin)
49
- end
50
43
  end
51
44
  end
52
45
  end
@@ -18,35 +18,28 @@ module RuboCop
18
18
  # let(:a) { b }
19
19
  #
20
20
  class AlignRightLetBrace < Cop
21
+ extend AutoCorrector
22
+
21
23
  MSG = 'Align right let brace'
22
24
 
23
25
  def self.autocorrect_incompatible_with
24
26
  [Layout::ExtraSpacing]
25
27
  end
26
28
 
27
- def investigate(_processed_source)
29
+ def on_new_investigation
28
30
  return if processed_source.blank?
29
31
 
30
- token_aligner.offending_tokens.each do |let|
31
- add_offense(let, location: :end)
32
- end
33
- end
32
+ token_aligner =
33
+ RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :end)
34
34
 
35
- def autocorrect(let)
36
- lambda do |corrector|
37
- corrector.insert_before(
38
- let.loc.end,
39
- token_aligner.indent_for(let)
40
- )
35
+ token_aligner.offending_tokens.each do |let|
36
+ add_offense(let.loc.end) do |corrector|
37
+ corrector.insert_before(
38
+ let.loc.end, token_aligner.indent_for(let)
39
+ )
40
+ end
41
41
  end
42
42
  end
43
-
44
- private
45
-
46
- def token_aligner
47
- @token_aligner ||=
48
- RuboCop::RSpec::AlignLetBrace.new(processed_source.ast, :end)
49
- end
50
43
  end
51
44
  end
52
45
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  def on_send(node)
30
30
  be_without_args(node) do |matcher|
31
- add_offense(matcher, location: :selector)
31
+ add_offense(matcher.loc.selector)
32
32
  end
33
33
  end
34
34
  end
@@ -36,6 +36,8 @@ module RuboCop
36
36
  # coerce objects for comparison.
37
37
  #
38
38
  class BeEql < Cop
39
+ extend AutoCorrector
40
+
39
41
  MSG = 'Prefer `be` over `eql`.'
40
42
 
41
43
  def_node_matcher :eql_type_with_identity, <<-PATTERN
@@ -44,13 +46,11 @@ module RuboCop
44
46
 
45
47
  def on_send(node)
46
48
  eql_type_with_identity(node) do |eql|
47
- add_offense(eql, location: :selector)
49
+ add_offense(eql.loc.selector) do |corrector|
50
+ corrector.replace(eql.loc.selector, 'be')
51
+ end
48
52
  end
49
53
  end
50
-
51
- def autocorrect(node)
52
- ->(corrector) { corrector.replace(node.loc.selector, 'be') }
53
- end
54
54
  end
55
55
  end
56
56
  end
@@ -24,6 +24,8 @@ module RuboCop
24
24
  # expect(page).to have_current_path(/widgets/)
25
25
  #
26
26
  class CurrentPathExpectation < Cop
27
+ extend AutoCorrector
28
+
27
29
  MSG = 'Do not set an RSpec expectation on `current_path` in ' \
28
30
  'Capybara feature specs - instead, use the ' \
29
31
  '`have_current_path` matcher on `page`'
@@ -47,30 +49,30 @@ module RuboCop
47
49
 
48
50
  def on_send(node)
49
51
  expectation_set_on_current_path(node) do
50
- add_offense(node, location: :selector)
52
+ add_offense(node.loc.selector) do |corrector|
53
+ next unless node.chained?
54
+
55
+ autocorrect(corrector, node)
56
+ end
51
57
  end
52
58
  end
53
59
 
54
- def autocorrect(node)
55
- lambda do |corrector|
56
- return unless node.chained?
60
+ private
57
61
 
58
- as_is_matcher(node.parent) do |to_sym, matcher_node|
59
- rewrite_expectation(corrector, node, to_sym, matcher_node)
60
- end
62
+ def autocorrect(corrector, node)
63
+ as_is_matcher(node.parent) do |to_sym, matcher_node|
64
+ rewrite_expectation(corrector, node, to_sym, matcher_node)
65
+ end
61
66
 
62
- regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp|
63
- rewrite_expectation(corrector, node, to_sym, matcher_node)
64
- convert_regexp_str_to_literal(corrector, matcher_node, regexp)
65
- end
67
+ regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp|
68
+ rewrite_expectation(corrector, node, to_sym, matcher_node)
69
+ convert_regexp_str_to_literal(corrector, matcher_node, regexp)
66
70
  end
67
71
  end
68
72
 
69
- private
70
-
71
73
  def rewrite_expectation(corrector, node, to_symbol, matcher_node)
72
74
  current_path_node = node.first_argument
73
- corrector.replace(current_path_node.loc.expression, 'page')
75
+ corrector.replace(current_path_node, 'page')
74
76
  corrector.replace(node.parent.loc.selector, 'to')
75
77
  matcher_method = if to_symbol == :to
76
78
  'have_current_path'
@@ -84,7 +86,7 @@ module RuboCop
84
86
  def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str)
85
87
  str_node = matcher_node.first_argument
86
88
  regexp_expr = Regexp.new(regexp_str).inspect
87
- corrector.replace(str_node.loc.expression, regexp_expr)
89
+ corrector.replace(str_node, regexp_expr)
88
90
  end
89
91
 
90
92
  # `have_current_path` with no options will include the querystring
@@ -97,7 +99,7 @@ module RuboCop
97
99
  return if %i[regexp str].include?(expectation_last_child.type)
98
100
 
99
101
  corrector.insert_after(
100
- expectation_last_child.loc.expression,
102
+ expectation_last_child,
101
103
  ', ignore_query: true'
102
104
  )
103
105
  end
@@ -41,6 +41,8 @@ module RuboCop
41
41
  # end
42
42
  # end
43
43
  class FeatureMethods < Cop
44
+ extend AutoCorrector
45
+
44
46
  MSG = 'Use `%<replacement>s` instead of `%<method>s`.'
45
47
 
46
48
  # https://git.io/v7Kwr
@@ -71,18 +73,15 @@ module RuboCop
71
73
  feature_method(node) do |send_node, match|
72
74
  next if enabled?(match)
73
75
 
74
- add_offense(
75
- send_node,
76
- location: :selector,
77
- message: format(MSG, method: match, replacement: MAP[match])
78
- )
76
+ add_offense(send_node.loc.selector) do |corrector|
77
+ corrector.replace(send_node.loc.selector, MAP[match].to_s)
78
+ end
79
79
  end
80
80
  end
81
81
 
82
- def autocorrect(node)
83
- lambda do |corrector|
84
- corrector.replace(node.loc.selector, MAP[node.method_name].to_s)
85
- end
82
+ def message(range)
83
+ name = range.source.to_sym
84
+ format(MSG, method: name, replacement: MAP[name])
86
85
  end
87
86
 
88
87
  private
@@ -24,6 +24,8 @@ module RuboCop
24
24
  # # ...
25
25
  # end
26
26
  class ContextMethod < Cop
27
+ extend AutoCorrector
28
+
27
29
  MSG = 'Use `describe` for testing methods.'
28
30
 
29
31
  def_node_matcher :context_method, <<-PATTERN
@@ -32,13 +34,9 @@ module RuboCop
32
34
 
33
35
  def on_block(node)
34
36
  context_method(node) do |context|
35
- add_offense(context)
36
- end
37
- end
38
-
39
- def autocorrect(node)
40
- lambda do |corrector|
41
- corrector.replace(node.parent.loc.selector, 'describe')
37
+ add_offense(context) do |corrector|
38
+ corrector.replace(node.send_node.loc.selector, 'describe')
39
+ end
42
40
  end
43
41
  end
44
42
 
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # # Patterns:
18
18
  # # - '_test.rb$'
19
19
  # # - '(?:^|/)test/'
20
- class Cop < ::RuboCop::Cop::Cop
20
+ class Cop < ::RuboCop::Cop::Base
21
21
  include RuboCop::RSpec::Language
22
22
  include RuboCop::RSpec::Language::NodePattern
23
23
 
@@ -55,6 +55,7 @@ module RuboCop
55
55
  # end
56
56
  #
57
57
  class DescribedClass < Cop
58
+ extend AutoCorrector
58
59
  include ConfigurableEnforcedStyle
59
60
 
60
61
  DESCRIBED_CLASS = 'described_class'
@@ -85,22 +86,24 @@ module RuboCop
85
86
  return unless body
86
87
 
87
88
  find_usage(body) do |match|
88
- add_offense(match, message: message(match.const_name))
89
+ msg = message(match.const_name)
90
+ add_offense(match, message: msg) do |corrector|
91
+ autocorrect(corrector, match)
92
+ end
89
93
  end
90
94
  end
91
95
 
92
- def autocorrect(node)
96
+ private
97
+
98
+ def autocorrect(corrector, match)
93
99
  replacement = if style == :described_class
94
100
  DESCRIBED_CLASS
95
101
  else
96
102
  @described_class.const_name
97
103
  end
98
- lambda do |corrector|
99
- corrector.replace(node.loc.expression, replacement)
100
- end
101
- end
102
104
 
103
- private
105
+ corrector.replace(match, replacement)
106
+ end
104
107
 
105
108
  def find_usage(node, &block)
106
109
  yield(node) if offensive?(node)
@@ -42,6 +42,7 @@ module RuboCop
42
42
  # # ...
43
43
  # end
44
44
  class Dialect < Cop
45
+ extend AutoCorrector
45
46
  include MethodPreference
46
47
 
47
48
  MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
@@ -52,24 +53,16 @@ module RuboCop
52
53
  return unless rspec_method?(node)
53
54
  return unless preferred_methods[node.method_name]
54
55
 
55
- add_offense(node)
56
- end
56
+ msg = format(MSG, prefer: preferred_method(node.method_name),
57
+ current: node.method_name)
57
58
 
58
- def autocorrect(node)
59
- lambda do |corrector|
59
+ add_offense(node, message: msg) do |corrector|
60
60
  current = node.loc.selector
61
61
  preferred = preferred_method(current.source)
62
62
 
63
63
  corrector.replace(current, preferred)
64
64
  end
65
65
  end
66
-
67
- private
68
-
69
- def message(node)
70
- format(MSG, prefer: preferred_method(node.method_name),
71
- current: node.method_name)
72
- end
73
66
  end
74
67
  end
75
68
  end
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # end
24
24
  # after(:all) { cleanup_feed }
25
25
  class EmptyHook < Cop
26
+ extend AutoCorrector
26
27
  include RuboCop::Cop::RangeHelp
27
28
 
28
29
  MSG = 'Empty hook detected.'
@@ -33,15 +34,10 @@ module RuboCop
33
34
 
34
35
  def on_block(node)
35
36
  empty_hook?(node) do |hook|
36
- add_offense(hook)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- block = node.parent
43
- range = range_with_surrounding_space(range: block.loc.expression)
44
- corrector.remove(range)
37
+ add_offense(hook) do |corrector|
38
+ range = range_with_surrounding_space(range: node.loc.expression)
39
+ corrector.remove(range)
40
+ end
45
41
  end
46
42
  end
47
43
  end
@@ -42,6 +42,7 @@ module RuboCop
42
42
  # end
43
43
  #
44
44
  class EmptyLineAfterExample < Cop
45
+ extend AutoCorrector
45
46
  include RuboCop::RSpec::BlankLineSeparation
46
47
 
47
48
  MSG = 'Add an empty line after `%<example>s`.'
@@ -52,9 +53,10 @@ module RuboCop
52
53
  return if allowed_one_liner?(node)
53
54
 
54
55
  missing_separating_line(node) do |location|
55
- add_offense(node,
56
- location: location,
57
- message: format(MSG, example: node.method_name))
56
+ msg = format(MSG, example: node.method_name)
57
+ add_offense(location, message: msg) do |corrector|
58
+ corrector.insert_after(location.end, "\n")
59
+ end
58
60
  end
59
61
  end
60
62
 
@@ -24,6 +24,7 @@ module RuboCop
24
24
  # end
25
25
  #
26
26
  class EmptyLineAfterExampleGroup < Cop
27
+ extend AutoCorrector
27
28
  include RuboCop::RSpec::BlankLineSeparation
28
29
 
29
30
  MSG = 'Add an empty line after `%<example_group>s`.'
@@ -33,11 +34,10 @@ module RuboCop
33
34
  return if last_child?(node)
34
35
 
35
36
  missing_separating_line(node) do |location|
36
- add_offense(
37
- node,
38
- location: location,
39
- message: format(MSG, example_group: node.method_name)
40
- )
37
+ msg = format(MSG, example_group: node.method_name)
38
+ add_offense(location, message: msg) do |corrector|
39
+ corrector.insert_after(location.end, "\n")
40
+ end
41
41
  end
42
42
  end
43
43
  end
@@ -17,6 +17,7 @@ module RuboCop
17
17
  #
18
18
  # it { does_something }
19
19
  class EmptyLineAfterFinalLet < Cop
20
+ extend AutoCorrector
20
21
  include RuboCop::RSpec::BlankLineSeparation
21
22
 
22
23
  MSG = 'Add an empty line after the last `let` block.'
@@ -30,7 +31,9 @@ module RuboCop
30
31
  return if last_child?(latest_let)
31
32
 
32
33
  missing_separating_line(latest_let) do |location|
33
- add_offense(latest_let, location: location)
34
+ add_offense(location) do |corrector|
35
+ corrector.insert_after(location.end, "\n")
36
+ end
34
37
  end
35
38
  end
36
39
  end