rubocop-rspec 2.23.0 → 2.26.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -12
- data/config/default.yml +69 -3
- data/lib/rubocop/cop/rspec/around_block.rb +3 -3
- data/lib/rubocop/cop/rspec/be.rb +1 -1
- data/lib/rubocop/cop/rspec/be_eq.rb +1 -1
- data/lib/rubocop/cop/rspec/be_eql.rb +1 -1
- data/lib/rubocop/cop/rspec/be_nil.rb +2 -2
- data/lib/rubocop/cop/rspec/before_after_all.rb +7 -13
- data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +2 -2
- data/lib/rubocop/cop/rspec/change_by_zero.rb +3 -3
- data/lib/rubocop/cop/rspec/context_method.rb +2 -2
- data/lib/rubocop/cop/rspec/context_wording.rb +1 -1
- data/lib/rubocop/cop/rspec/describe_symbol.rb +1 -1
- data/lib/rubocop/cop/rspec/described_class.rb +5 -5
- data/lib/rubocop/cop/rspec/duplicated_metadata.rb +1 -1
- data/lib/rubocop/cop/rspec/empty_example_group.rb +2 -2
- data/lib/rubocop/cop/rspec/empty_line_after_example.rb +2 -2
- data/lib/rubocop/cop/rspec/empty_metadata.rb +46 -0
- data/lib/rubocop/cop/rspec/eq.rb +47 -0
- data/lib/rubocop/cop/rspec/example_length.rb +11 -5
- data/lib/rubocop/cop/rspec/example_wording.rb +11 -2
- data/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb +4 -2
- data/lib/rubocop/cop/rspec/expect_change.rb +2 -2
- data/lib/rubocop/cop/rspec/file_path.rb +6 -0
- data/lib/rubocop/cop/rspec/focus.rb +4 -2
- data/lib/rubocop/cop/rspec/hook_argument.rb +2 -2
- data/lib/rubocop/cop/rspec/hooks_before_examples.rb +1 -1
- data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +2 -2
- data/lib/rubocop/cop/rspec/implicit_expect.rb +1 -1
- data/lib/rubocop/cop/rspec/implicit_subject.rb +2 -2
- data/lib/rubocop/cop/rspec/instance_spy.rb +2 -2
- data/lib/rubocop/cop/rspec/instance_variable.rb +2 -2
- data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -3
- data/lib/rubocop/cop/rspec/let_before_examples.rb +1 -1
- data/lib/rubocop/cop/rspec/let_setup.rb +1 -1
- data/lib/rubocop/cop/rspec/message_expectation.rb +1 -1
- data/lib/rubocop/cop/rspec/metadata_style.rb +202 -0
- data/lib/rubocop/cop/rspec/mixin/file_help.rb +14 -0
- data/lib/rubocop/cop/rspec/mixin/metadata.rb +21 -7
- data/lib/rubocop/cop/rspec/mixin/skip_or_pending.rb +2 -2
- data/lib/rubocop/cop/rspec/multiple_expectations.rb +2 -2
- data/lib/rubocop/cop/rspec/pending.rb +1 -1
- data/lib/rubocop/cop/rspec/pending_without_reason.rb +1 -1
- data/lib/rubocop/cop/rspec/predicate_matcher.rb +6 -6
- data/lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb +1 -1
- data/lib/rubocop/cop/rspec/rails/have_http_status.rb +1 -1
- data/lib/rubocop/cop/rspec/rails/http_status.rb +29 -18
- data/lib/rubocop/cop/rspec/rails/minitest_assertions.rb +2 -2
- data/lib/rubocop/cop/rspec/rails/negation_be_valid.rb +10 -0
- data/lib/rubocop/cop/rspec/receive_counts.rb +1 -1
- data/lib/rubocop/cop/rspec/receive_messages.rb +11 -5
- data/lib/rubocop/cop/rspec/redundant_predicate_matcher.rb +65 -0
- data/lib/rubocop/cop/rspec/remove_const.rb +40 -0
- data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +1 -1
- data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +2 -2
- data/lib/rubocop/cop/rspec/repeated_include_example.rb +1 -1
- data/lib/rubocop/cop/rspec/return_from_stub.rb +1 -1
- data/lib/rubocop/cop/rspec/shared_context.rb +1 -1
- data/lib/rubocop/cop/rspec/shared_examples.rb +66 -20
- data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +1 -1
- data/lib/rubocop/cop/rspec/sort_metadata.rb +2 -1
- data/lib/rubocop/cop/rspec/spec_file_path_format.rb +133 -0
- data/lib/rubocop/cop/rspec/spec_file_path_suffix.rb +40 -0
- data/lib/rubocop/cop/rspec/stubbed_mock.rb +1 -1
- data/lib/rubocop/cop/rspec/subject_stub.rb +4 -4
- data/lib/rubocop/cop/rspec/unspecified_exception.rb +2 -2
- data/lib/rubocop/cop/rspec/variable_definition.rb +4 -4
- data/lib/rubocop/cop/rspec/verified_double_reference.rb +5 -5
- data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -1
- data/lib/rubocop/cop/rspec/void_expect.rb +4 -3
- data/lib/rubocop/cop/rspec_cops.rb +7 -0
- data/lib/rubocop/rspec/language.rb +8 -8
- data/lib/rubocop/rspec/version.rb +1 -1
- data/lib/rubocop/rspec/wording.rb +8 -0
- data/lib/rubocop-rspec.rb +1 -0
- metadata +16 -8
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Use `eq` instead of `be ==` to compare objects.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# expect(foo).to be == 42
|
11
|
+
#
|
12
|
+
# # good
|
13
|
+
# expect(foo).to eq 42
|
14
|
+
#
|
15
|
+
class Eq < Base
|
16
|
+
extend AutoCorrector
|
17
|
+
include RangeHelp
|
18
|
+
|
19
|
+
MSG = 'Use `eq` instead of `be ==` to compare objects.'
|
20
|
+
RESTRICT_ON_SEND = Runners.all
|
21
|
+
|
22
|
+
# @!method be_equals(node)
|
23
|
+
def_node_matcher :be_equals, <<~PATTERN
|
24
|
+
(send _ #Runners.all $(send (send nil? :be) :== _))
|
25
|
+
PATTERN
|
26
|
+
|
27
|
+
def on_send(node)
|
28
|
+
be_equals(node) do |matcher|
|
29
|
+
range = offense_range(matcher)
|
30
|
+
add_offense(range) do |corrector|
|
31
|
+
corrector.replace(range, 'eq')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def offense_range(matcher)
|
39
|
+
range_between(
|
40
|
+
matcher.source_range.begin_pos,
|
41
|
+
matcher.loc.selector.end_pos
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -26,11 +26,12 @@ module RuboCop
|
|
26
26
|
# expect(result).to be(true)
|
27
27
|
# end
|
28
28
|
#
|
29
|
-
# You can set
|
30
|
-
# Available are: 'array', 'hash', and '
|
31
|
-
# will be counted as one line regardless of
|
29
|
+
# You can set constructs you want to fold with `CountAsOne`.
|
30
|
+
# Available are: 'array', 'hash', 'heredoc', and 'method_call'.
|
31
|
+
# Each construct will be counted as one line regardless of
|
32
|
+
# its actual size.
|
32
33
|
#
|
33
|
-
# @example CountAsOne: ['array', 'heredoc']
|
34
|
+
# @example CountAsOne: ['array', 'heredoc', 'method_call']
|
34
35
|
#
|
35
36
|
# it do
|
36
37
|
# array = [ # +1
|
@@ -46,7 +47,12 @@ module RuboCop
|
|
46
47
|
# Heredoc
|
47
48
|
# content.
|
48
49
|
# HEREDOC
|
49
|
-
#
|
50
|
+
#
|
51
|
+
# foo( # +1
|
52
|
+
# 1,
|
53
|
+
# 2
|
54
|
+
# )
|
55
|
+
# end # 6 points
|
50
56
|
#
|
51
57
|
class ExampleLength < Base
|
52
58
|
include CodeLength
|
@@ -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,
|
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,
|
32
|
+
def_node_matcher :example_description, <<~PATTERN
|
33
33
|
(send _ {#Examples.all #ExampleGroups.all} ${
|
34
34
|
$str
|
35
35
|
$(dstr ({str dstr `sym} ...) ...)
|
@@ -66,7 +66,9 @@ module RuboCop
|
|
66
66
|
|
67
67
|
# @param text [String]
|
68
68
|
def strip_excessive_whitespace(text)
|
69
|
-
text
|
69
|
+
text
|
70
|
+
.gsub(/[[:blank:]]{2,}/, ' ')
|
71
|
+
.gsub(/\A[[:blank:]]|[[:blank:]]\z/, '')
|
70
72
|
end
|
71
73
|
|
72
74
|
# @param node [RuboCop::AST::Node]
|
@@ -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,
|
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,
|
46
|
+
def_node_matcher :expect_change_with_block, <<~PATTERN
|
47
47
|
(block
|
48
48
|
(send nil? :change)
|
49
49
|
(args)
|
@@ -5,6 +5,12 @@ module RuboCop
|
|
5
5
|
module RSpec
|
6
6
|
# Checks that spec file paths are consistent and well-formed.
|
7
7
|
#
|
8
|
+
# This cop is deprecated.
|
9
|
+
# We plan to remove it in the next major version update to 3.0.
|
10
|
+
# The migration targets are `RSpec/SpecFilePathSuffix`
|
11
|
+
# and `RSpec/SpecFilePathFormat`.
|
12
|
+
# If you are using this cop, please plan for migration.
|
13
|
+
#
|
8
14
|
# By default, this checks that spec file paths are consistent with the
|
9
15
|
# test subject and enforces that it reflects the described
|
10
16
|
# class/module and its optionally called out method.
|
@@ -56,7 +56,7 @@ module RuboCop
|
|
56
56
|
MSG = 'Focused spec found.'
|
57
57
|
|
58
58
|
# @!method focusable_selector?(node)
|
59
|
-
def_node_matcher :focusable_selector?,
|
59
|
+
def_node_matcher :focusable_selector?, <<~PATTERN
|
60
60
|
{
|
61
61
|
#ExampleGroups.regular
|
62
62
|
#ExampleGroups.skipped
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
68
68
|
PATTERN
|
69
69
|
|
70
70
|
# @!method metadata(node)
|
71
|
-
def_node_matcher :metadata,
|
71
|
+
def_node_matcher :metadata, <<~PATTERN
|
72
72
|
{(send #rspec? #focusable_selector? <$(sym :focus) ...>)
|
73
73
|
(send #rspec? #focusable_selector? ... (hash <$(pair (sym :focus) true) ...>))}
|
74
74
|
PATTERN
|
@@ -79,6 +79,8 @@ module RuboCop
|
|
79
79
|
PATTERN
|
80
80
|
|
81
81
|
def on_send(node)
|
82
|
+
return if node.chained? || node.each_ancestor(:def, :defs).any?
|
83
|
+
|
82
84
|
focus_metadata(node) do |focus|
|
83
85
|
add_offense(focus) do |corrector|
|
84
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,
|
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,
|
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?,
|
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?,
|
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,
|
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,
|
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?,
|
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?,
|
86
|
+
def_node_matcher :implicit_subject?, <<~PATTERN
|
87
87
|
(send nil? {:should :should_not :is_expected} ...)
|
88
88
|
PATTERN
|
89
89
|
|
@@ -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,
|
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,
|
36
|
+
def_node_search :have_received_usage, <<~PATTERN
|
37
37
|
(send
|
38
38
|
(send nil? :expect
|
39
39
|
(lvar $_)) :to
|
@@ -52,12 +52,12 @@ module RuboCop
|
|
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?,
|
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?,
|
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)
|
@@ -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?,
|
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?,
|
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?,
|
40
|
+
def_node_matcher :expectation?, <<~PATTERN
|
41
41
|
(send (send nil? :expect (lvar %)) :to ...)
|
42
42
|
PATTERN
|
43
43
|
|
@@ -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?,
|
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 ...)
|
@@ -33,7 +33,7 @@ module RuboCop
|
|
33
33
|
RESTRICT_ON_SEND = %i[to].freeze
|
34
34
|
|
35
35
|
# @!method message_expectation(node)
|
36
|
-
def_node_matcher :message_expectation,
|
36
|
+
def_node_matcher :message_expectation, <<~PATTERN
|
37
37
|
(send $(send nil? {:expect :allow} ...) :to #receive_message?)
|
38
38
|
PATTERN
|
39
39
|
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Use consistent metadata style.
|
7
|
+
#
|
8
|
+
# This cop does not support autocorrection in the case of
|
9
|
+
# `EnforcedStyle: hash` where the trailing metadata type is ambiguous.
|
10
|
+
# (e.g. `describe 'Something', :a, b`)
|
11
|
+
#
|
12
|
+
# @example EnforcedStyle: symbol (default)
|
13
|
+
# # bad
|
14
|
+
# describe 'Something', a: true
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# describe 'Something', :a
|
18
|
+
#
|
19
|
+
# @example EnforcedStyle: hash
|
20
|
+
# # bad
|
21
|
+
# describe 'Something', :a
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# describe 'Something', a: true
|
25
|
+
class MetadataStyle < Base # rubocop:disable Metrics/ClassLength
|
26
|
+
extend AutoCorrector
|
27
|
+
|
28
|
+
include ConfigurableEnforcedStyle
|
29
|
+
include Metadata
|
30
|
+
include RangeHelp
|
31
|
+
|
32
|
+
# @!method extract_metadata_hash(node)
|
33
|
+
def_node_matcher :extract_metadata_hash, <<~PATTERN
|
34
|
+
(send _ _ _ ... $hash)
|
35
|
+
PATTERN
|
36
|
+
|
37
|
+
# @!method match_boolean_metadata_pair?(node)
|
38
|
+
def_node_matcher :match_boolean_metadata_pair?, <<~PATTERN
|
39
|
+
(pair sym true)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
# @!method match_ambiguous_trailing_metadata?(node)
|
43
|
+
def_node_matcher :match_ambiguous_trailing_metadata?, <<~PATTERN
|
44
|
+
(send _ _ _ ... !{hash sym})
|
45
|
+
PATTERN
|
46
|
+
|
47
|
+
def on_metadata(symbols, hash)
|
48
|
+
# RSpec example groups accept two string arguments. In such a case,
|
49
|
+
# the rspec_metadata matcher will interpret the second string
|
50
|
+
# argument as a metadata symbol.
|
51
|
+
symbols.shift if symbols.first&.str_type?
|
52
|
+
|
53
|
+
symbols.each do |symbol|
|
54
|
+
on_metadata_symbol(symbol)
|
55
|
+
end
|
56
|
+
|
57
|
+
return unless hash
|
58
|
+
|
59
|
+
hash.pairs.each do |pair|
|
60
|
+
on_metadata_pair(pair)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def autocorrect_pair(corrector, node)
|
67
|
+
remove_pair(corrector, node)
|
68
|
+
insert_symbol(corrector, node)
|
69
|
+
end
|
70
|
+
|
71
|
+
def autocorrect_symbol(corrector, node)
|
72
|
+
return if match_ambiguous_trailing_metadata?(node.parent)
|
73
|
+
|
74
|
+
remove_symbol(corrector, node)
|
75
|
+
insert_pair(corrector, node)
|
76
|
+
end
|
77
|
+
|
78
|
+
def bad_metadata_pair?(node)
|
79
|
+
style == :symbol && match_boolean_metadata_pair?(node)
|
80
|
+
end
|
81
|
+
|
82
|
+
def bad_metadata_symbol?(_node)
|
83
|
+
style == :hash
|
84
|
+
end
|
85
|
+
|
86
|
+
def format_symbol_to_pair_source(node)
|
87
|
+
"#{node.value}: true"
|
88
|
+
end
|
89
|
+
|
90
|
+
def insert_pair(corrector, node)
|
91
|
+
hash_node = extract_metadata_hash(node.parent)
|
92
|
+
if hash_node.nil?
|
93
|
+
insert_pair_as_last_argument(corrector, node)
|
94
|
+
elsif hash_node.pairs.any?
|
95
|
+
insert_pair_to_non_empty_hash_metadata(corrector, node, hash_node)
|
96
|
+
else
|
97
|
+
insert_pair_to_empty_hash_metadata(corrector, node, hash_node)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def insert_pair_as_last_argument(corrector, node)
|
102
|
+
corrector.insert_before(
|
103
|
+
node.parent.location.end || node.parent.source_range.with(
|
104
|
+
begin_pos: node.parent.source_range.end_pos
|
105
|
+
),
|
106
|
+
", #{format_symbol_to_pair_source(node)}"
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
def insert_pair_to_empty_hash_metadata(corrector, node, hash_node)
|
111
|
+
corrector.insert_after(
|
112
|
+
hash_node.location.begin,
|
113
|
+
" #{format_symbol_to_pair_source(node)} "
|
114
|
+
)
|
115
|
+
end
|
116
|
+
|
117
|
+
def insert_pair_to_non_empty_hash_metadata(corrector, node, hash_node)
|
118
|
+
corrector.insert_after(
|
119
|
+
hash_node.children.last,
|
120
|
+
", #{format_symbol_to_pair_source(node)}"
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
def insert_symbol(corrector, node)
|
125
|
+
corrector.insert_after(
|
126
|
+
node.parent.left_sibling,
|
127
|
+
", #{node.key.value.inspect}"
|
128
|
+
)
|
129
|
+
end
|
130
|
+
|
131
|
+
def message_for_style
|
132
|
+
format(
|
133
|
+
'Use %<style>s style for metadata.',
|
134
|
+
style: style
|
135
|
+
)
|
136
|
+
end
|
137
|
+
|
138
|
+
def on_metadata_pair(node)
|
139
|
+
return unless bad_metadata_pair?(node)
|
140
|
+
|
141
|
+
add_offense(node, message: message_for_style) do |corrector|
|
142
|
+
autocorrect_pair(corrector, node)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def on_metadata_symbol(node)
|
147
|
+
return unless bad_metadata_symbol?(node)
|
148
|
+
|
149
|
+
add_offense(node, message: message_for_style) do |corrector|
|
150
|
+
autocorrect_symbol(corrector, node)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def remove_pair(corrector, node)
|
155
|
+
if !node.parent.braces? || node.left_siblings.any?
|
156
|
+
remove_pair_following(corrector, node)
|
157
|
+
elsif node.right_siblings.any?
|
158
|
+
remove_pair_preceding(corrector, node)
|
159
|
+
else
|
160
|
+
corrector.remove(node)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def remove_pair_following(corrector, node)
|
165
|
+
corrector.remove(
|
166
|
+
range_with_surrounding_comma(
|
167
|
+
range_with_surrounding_space(
|
168
|
+
node.source_range,
|
169
|
+
side: :left
|
170
|
+
),
|
171
|
+
:left
|
172
|
+
)
|
173
|
+
)
|
174
|
+
end
|
175
|
+
|
176
|
+
def remove_pair_preceding(corrector, node)
|
177
|
+
corrector.remove(
|
178
|
+
range_with_surrounding_space(
|
179
|
+
range_with_surrounding_comma(
|
180
|
+
node.source_range,
|
181
|
+
:right
|
182
|
+
),
|
183
|
+
side: :right
|
184
|
+
)
|
185
|
+
)
|
186
|
+
end
|
187
|
+
|
188
|
+
def remove_symbol(corrector, node)
|
189
|
+
corrector.remove(
|
190
|
+
range_with_surrounding_comma(
|
191
|
+
range_with_surrounding_space(
|
192
|
+
node.source_range,
|
193
|
+
side: :left
|
194
|
+
),
|
195
|
+
:left
|
196
|
+
)
|
197
|
+
)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -13,7 +13,7 @@ module RuboCop
|
|
13
13
|
def_node_matcher :rspec_metadata, <<~PATTERN
|
14
14
|
(block
|
15
15
|
(send
|
16
|
-
#rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _
|
16
|
+
#rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _ $...)
|
17
17
|
...)
|
18
18
|
PATTERN
|
19
19
|
|
@@ -24,25 +24,39 @@ module RuboCop
|
|
24
24
|
|
25
25
|
# @!method metadata_in_block(node)
|
26
26
|
def_node_search :metadata_in_block, <<~PATTERN
|
27
|
-
(send (lvar %) #Hooks.all _
|
27
|
+
(send (lvar %) #Hooks.all _ $...)
|
28
28
|
PATTERN
|
29
29
|
|
30
30
|
def on_block(node)
|
31
31
|
rspec_configure(node) do |block_var|
|
32
|
-
metadata_in_block(node, block_var) do |
|
33
|
-
|
32
|
+
metadata_in_block(node, block_var) do |metadata_arguments|
|
33
|
+
on_metadata_arguments(metadata_arguments)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
rspec_metadata(node) do |
|
38
|
-
|
37
|
+
rspec_metadata(node) do |metadata_arguments|
|
38
|
+
on_metadata_arguments(metadata_arguments)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
alias on_numblock on_block
|
42
42
|
|
43
|
-
def on_metadata(_symbols,
|
43
|
+
def on_metadata(_symbols, _hash)
|
44
44
|
raise ::NotImplementedError
|
45
45
|
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def on_metadata_arguments(metadata_arguments)
|
50
|
+
*symbols, last = metadata_arguments
|
51
|
+
hash = nil
|
52
|
+
case last&.type
|
53
|
+
when :hash
|
54
|
+
hash = last
|
55
|
+
when :sym
|
56
|
+
symbols << last
|
57
|
+
end
|
58
|
+
on_metadata(symbols, hash)
|
59
|
+
end
|
46
60
|
end
|
47
61
|
end
|
48
62
|
end
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
extend RuboCop::NodePattern::Macros
|
9
9
|
|
10
10
|
# @!method skipped_in_metadata?(node)
|
11
|
-
def_node_matcher :skipped_in_metadata?,
|
11
|
+
def_node_matcher :skipped_in_metadata?, <<~PATTERN
|
12
12
|
{
|
13
13
|
(send _ _ <(sym {:skip :pending}) ...>)
|
14
14
|
(send _ _ ... (hash <(pair (sym {:skip :pending}) { true str dstr }) ...>))
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
#
|
31
31
|
# @param node [RuboCop::AST::Node]
|
32
32
|
# @return [Array<RuboCop::AST::Node>] matching nodes
|
33
|
-
def_node_matcher :skip_or_pending_inside_block?,
|
33
|
+
def_node_matcher :skip_or_pending_inside_block?, <<~PATTERN
|
34
34
|
(block <(send nil? {:skip :pending} ...) ...>)
|
35
35
|
PATTERN
|
36
36
|
end
|