rubocop-rspec 1.38.1 → 1.43.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +57 -0
- data/CODE_OF_CONDUCT.md +17 -0
- data/README.md +1 -61
- data/config/default.yml +159 -19
- data/lib/rubocop-rspec.rb +5 -2
- data/lib/rubocop/cop/rspec/align_left_let_brace.rb +12 -19
- data/lib/rubocop/cop/rspec/align_right_let_brace.rb +12 -19
- data/lib/rubocop/cop/rspec/any_instance.rb +1 -1
- data/lib/rubocop/cop/rspec/around_block.rb +1 -1
- data/lib/rubocop/cop/rspec/base.rb +74 -0
- data/lib/rubocop/cop/rspec/be.rb +2 -2
- data/lib/rubocop/cop/rspec/be_eql.rb +6 -6
- data/lib/rubocop/cop/rspec/before_after_all.rb +1 -1
- data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +19 -17
- data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +14 -12
- data/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +69 -0
- data/lib/rubocop/cop/rspec/context_method.rb +7 -9
- data/lib/rubocop/cop/rspec/context_wording.rb +3 -3
- data/lib/rubocop/cop/rspec/cop.rb +3 -87
- data/lib/rubocop/cop/rspec/describe_class.rb +29 -23
- data/lib/rubocop/cop/rspec/describe_method.rb +14 -7
- data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -2
- data/lib/rubocop/cop/rspec/described_class.rb +12 -9
- data/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +1 -1
- data/lib/rubocop/cop/rspec/dialect.rb +5 -12
- data/lib/rubocop/cop/rspec/empty_example_group.rb +91 -7
- data/lib/rubocop/cop/rspec/empty_hook.rb +46 -0
- data/lib/rubocop/cop/rspec/empty_line_after_example.rb +5 -7
- data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +5 -9
- data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +8 -8
- data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +5 -9
- data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +6 -6
- data/lib/rubocop/cop/rspec/example_length.rb +1 -1
- data/lib/rubocop/cop/rspec/example_without_description.rb +1 -1
- data/lib/rubocop/cop/rspec/example_wording.rb +10 -11
- data/lib/rubocop/cop/rspec/expect_actual.rb +8 -11
- data/lib/rubocop/cop/rspec/expect_change.rb +10 -35
- data/lib/rubocop/cop/rspec/expect_in_hook.rb +3 -3
- data/lib/rubocop/cop/rspec/expect_output.rb +2 -2
- data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +24 -21
- data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +20 -22
- data/lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb +7 -8
- data/lib/rubocop/cop/rspec/file_path.rb +57 -21
- data/lib/rubocop/cop/rspec/focus.rb +7 -11
- data/lib/rubocop/cop/rspec/hook_argument.rb +16 -23
- data/lib/rubocop/cop/rspec/hooks_before_examples.rb +10 -29
- data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +1 -1
- data/lib/rubocop/cop/rspec/implicit_expect.rb +7 -15
- data/lib/rubocop/cop/rspec/implicit_subject.rb +16 -11
- data/lib/rubocop/cop/rspec/instance_spy.rb +18 -12
- data/lib/rubocop/cop/rspec/instance_variable.rb +4 -8
- data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +3 -6
- data/lib/rubocop/cop/rspec/it_behaves_like.rb +5 -6
- data/lib/rubocop/cop/rspec/iterated_expectation.rb +1 -1
- data/lib/rubocop/cop/rspec/leading_subject.rb +22 -26
- data/lib/rubocop/cop/rspec/leaky_constant_declaration.rb +2 -5
- data/lib/rubocop/cop/rspec/let_before_examples.rb +10 -26
- data/lib/rubocop/cop/rspec/let_setup.rb +21 -6
- data/lib/rubocop/cop/rspec/message_chain.rb +7 -6
- data/lib/rubocop/cop/rspec/message_expectation.rb +2 -2
- data/lib/rubocop/cop/rspec/message_spies.rb +2 -3
- data/lib/rubocop/cop/rspec/missing_example_group_argument.rb +1 -1
- data/lib/rubocop/cop/rspec/multiple_describes.rb +11 -8
- data/lib/rubocop/cop/rspec/multiple_expectations.rb +7 -11
- data/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +148 -0
- data/lib/rubocop/cop/rspec/multiple_subjects.rb +18 -19
- data/lib/rubocop/cop/rspec/named_subject.rb +8 -8
- data/lib/rubocop/cop/rspec/nested_groups.rb +12 -13
- data/lib/rubocop/cop/rspec/not_to_not.rb +5 -6
- data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
- data/lib/rubocop/cop/rspec/pending.rb +1 -1
- data/lib/rubocop/cop/rspec/predicate_matcher.rb +32 -69
- data/lib/rubocop/cop/rspec/rails/http_status.rb +7 -9
- data/lib/rubocop/cop/rspec/receive_counts.rb +15 -17
- data/lib/rubocop/cop/rspec/receive_never.rb +12 -12
- data/lib/rubocop/cop/rspec/repeated_description.rb +1 -1
- data/lib/rubocop/cop/rspec/repeated_example.rb +2 -2
- data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +12 -2
- data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +1 -1
- data/lib/rubocop/cop/rspec/return_from_stub.rb +12 -22
- data/lib/rubocop/cop/rspec/scattered_let.rb +12 -2
- data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
- data/lib/rubocop/cop/rspec/shared_context.rb +8 -21
- data/lib/rubocop/cop/rspec/shared_examples.rb +7 -9
- data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +15 -18
- data/lib/rubocop/cop/rspec/subject_stub.rb +25 -53
- data/lib/rubocop/cop/rspec/unspecified_exception.rb +1 -1
- data/lib/rubocop/cop/rspec/variable_definition.rb +56 -0
- data/lib/rubocop/cop/rspec/variable_name.rb +66 -0
- data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -1
- data/lib/rubocop/cop/rspec/void_expect.rb +1 -1
- data/lib/rubocop/cop/rspec/yield.rb +14 -11
- data/lib/rubocop/cop/rspec_cops.rb +6 -1
- data/lib/rubocop/rspec/corrector/move_node.rb +54 -0
- data/lib/rubocop/rspec/description_extractor.rb +2 -6
- data/lib/rubocop/rspec/{blank_line_separation.rb → empty_line_separation.rb} +13 -10
- data/lib/rubocop/rspec/example_group.rb +21 -49
- data/lib/rubocop/rspec/factory_bot.rb +7 -1
- data/lib/rubocop/rspec/language.rb +13 -3
- data/lib/rubocop/rspec/language/node_pattern.rb +11 -2
- data/lib/rubocop/rspec/top_level_describe.rb +2 -2
- data/lib/rubocop/rspec/top_level_group.rb +55 -0
- data/lib/rubocop/rspec/variable.rb +16 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- metadata +36 -13
- data/lib/rubocop/rspec/util.rb +0 -19
@@ -26,7 +26,9 @@ module RuboCop
|
|
26
26
|
# let!(:baz) { 3 }
|
27
27
|
# end
|
28
28
|
#
|
29
|
-
class ScatteredLet <
|
29
|
+
class ScatteredLet < Base
|
30
|
+
extend AutoCorrector
|
31
|
+
|
30
32
|
MSG = 'Group all let/let! blocks in the example group together.'
|
31
33
|
|
32
34
|
def on_block(node)
|
@@ -44,9 +46,17 @@ module RuboCop
|
|
44
46
|
lets.each_with_index do |node, idx|
|
45
47
|
next if node.sibling_index == first_let.sibling_index + idx
|
46
48
|
|
47
|
-
add_offense(node)
|
49
|
+
add_offense(node) do |corrector|
|
50
|
+
RuboCop::RSpec::Corrector::MoveNode.new(
|
51
|
+
node, corrector, processed_source
|
52
|
+
).move_after(first_let)
|
53
|
+
end
|
48
54
|
end
|
49
55
|
end
|
56
|
+
|
57
|
+
def find_first_let(node)
|
58
|
+
node.children.find { |child| let?(child) }
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
end
|
@@ -50,7 +50,9 @@ module RuboCop
|
|
50
50
|
# end
|
51
51
|
# end
|
52
52
|
#
|
53
|
-
class SharedContext <
|
53
|
+
class SharedContext < Base
|
54
|
+
extend AutoCorrector
|
55
|
+
|
54
56
|
MSG_EXAMPLES = "Use `shared_examples` when you don't "\
|
55
57
|
'define context.'
|
56
58
|
|
@@ -68,22 +70,14 @@ module RuboCop
|
|
68
70
|
|
69
71
|
def on_block(node)
|
70
72
|
context_with_only_examples(node) do
|
71
|
-
|
73
|
+
add_offense(node.send_node, message: MSG_EXAMPLES) do |corrector|
|
74
|
+
corrector.replace(node.send_node.loc.selector, 'shared_examples')
|
75
|
+
end
|
72
76
|
end
|
73
77
|
|
74
78
|
examples_with_only_context(node) do
|
75
|
-
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
def autocorrect(node)
|
80
|
-
lambda do |corrector|
|
81
|
-
context_with_only_examples(node.parent) do
|
82
|
-
corrector.replace(node.loc.selector, 'shared_examples')
|
83
|
-
end
|
84
|
-
|
85
|
-
examples_with_only_context(node.parent) do
|
86
|
-
corrector.replace(node.loc.selector, 'shared_context')
|
79
|
+
add_offense(node.send_node, message: MSG_CONTEXT) do |corrector|
|
80
|
+
corrector.replace(node.send_node.loc.selector, 'shared_context')
|
87
81
|
end
|
88
82
|
end
|
89
83
|
end
|
@@ -97,13 +91,6 @@ module RuboCop
|
|
97
91
|
def examples_with_only_context(node)
|
98
92
|
shared_example(node) { yield if context?(node) && !examples?(node) }
|
99
93
|
end
|
100
|
-
|
101
|
-
def add_shared_item_offense(node, message)
|
102
|
-
add_offense(
|
103
|
-
node,
|
104
|
-
message: message
|
105
|
-
)
|
106
|
-
end
|
107
94
|
end
|
108
95
|
end
|
109
96
|
end
|
@@ -20,7 +20,9 @@ module RuboCop
|
|
20
20
|
# shared_examples_for 'foo bar baz'
|
21
21
|
# include_examples 'foo bar baz'
|
22
22
|
#
|
23
|
-
class SharedExamples <
|
23
|
+
class SharedExamples < Base
|
24
|
+
extend AutoCorrector
|
25
|
+
|
24
26
|
def_node_matcher :shared_examples,
|
25
27
|
(SharedGroups::ALL + Includes::ALL).send_pattern
|
26
28
|
|
@@ -30,14 +32,9 @@ module RuboCop
|
|
30
32
|
next unless ast_node&.sym_type?
|
31
33
|
|
32
34
|
checker = Checker.new(ast_node)
|
33
|
-
add_offense(checker.node, message: checker.message)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
def autocorrect(node)
|
38
|
-
lambda do |corrector|
|
39
|
-
checker = Checker.new(node)
|
40
|
-
corrector.replace(node.loc.expression, checker.preferred_style)
|
35
|
+
add_offense(checker.node, message: checker.message) do |corrector|
|
36
|
+
corrector.replace(checker.node, checker.preferred_style)
|
37
|
+
end
|
41
38
|
end
|
42
39
|
end
|
43
40
|
|
@@ -47,6 +44,7 @@ module RuboCop
|
|
47
44
|
'to titleize shared examples.'
|
48
45
|
|
49
46
|
attr_reader :node
|
47
|
+
|
50
48
|
def initialize(node)
|
51
49
|
@node = node
|
52
50
|
end
|
@@ -16,7 +16,9 @@ module RuboCop
|
|
16
16
|
# allow(foo).to receive(:bar, :baz)
|
17
17
|
# allow(foo).to receive("bar.baz")
|
18
18
|
#
|
19
|
-
class SingleArgumentMessageChain <
|
19
|
+
class SingleArgumentMessageChain < Base
|
20
|
+
extend AutoCorrector
|
21
|
+
|
20
22
|
MSG = 'Use `%<recommended>s` instead of calling '\
|
21
23
|
'`%<called>s` with a single argument.'
|
22
24
|
|
@@ -30,22 +32,23 @@ module RuboCop
|
|
30
32
|
message_chain(node) do |arg|
|
31
33
|
return if valid_usage?(arg)
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
35
|
+
method = node.method_name
|
36
|
+
msg = format(MSG, recommended: replacement(method), called: method)
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
corrector.replace(node.loc.selector, replacement(node.method_name))
|
40
|
-
message_chain(node) do |arg|
|
41
|
-
autocorrect_hash_arg(corrector, arg) if single_key_hash?(arg)
|
42
|
-
autocorrect_array_arg(corrector, arg) if arg.array_type?
|
38
|
+
add_offense(node.loc.selector, message: msg) do |corrector|
|
39
|
+
autocorrect(corrector, node, method, arg)
|
43
40
|
end
|
44
41
|
end
|
45
42
|
end
|
46
43
|
|
47
44
|
private
|
48
45
|
|
46
|
+
def autocorrect(corrector, node, method, arg)
|
47
|
+
corrector.replace(node.loc.selector, replacement(method))
|
48
|
+
autocorrect_hash_arg(corrector, arg) if single_key_hash?(arg)
|
49
|
+
autocorrect_array_arg(corrector, arg) if arg.array_type?
|
50
|
+
end
|
51
|
+
|
49
52
|
def valid_usage?(node)
|
50
53
|
return true unless node.literal? || node.array_type?
|
51
54
|
|
@@ -63,7 +66,7 @@ module RuboCop
|
|
63
66
|
def autocorrect_hash_arg(corrector, arg)
|
64
67
|
key, value = *arg.children.first
|
65
68
|
|
66
|
-
corrector.replace(arg
|
69
|
+
corrector.replace(arg, key_to_arg(key))
|
67
70
|
corrector.insert_after(arg.parent.loc.end,
|
68
71
|
".and_return(#{value.source})")
|
69
72
|
end
|
@@ -71,7 +74,7 @@ module RuboCop
|
|
71
74
|
def autocorrect_array_arg(corrector, arg)
|
72
75
|
value = arg.children.first
|
73
76
|
|
74
|
-
corrector.replace(arg
|
77
|
+
corrector.replace(arg, value.source)
|
75
78
|
end
|
76
79
|
|
77
80
|
def key_to_arg(node)
|
@@ -82,12 +85,6 @@ module RuboCop
|
|
82
85
|
def replacement(method)
|
83
86
|
method.equal?(:receive_message_chain) ? 'receive' : 'stub'
|
84
87
|
end
|
85
|
-
|
86
|
-
def message(node)
|
87
|
-
method = node.method_name
|
88
|
-
|
89
|
-
format(MSG, recommended: replacement(method), called: method)
|
90
|
-
end
|
91
88
|
end
|
92
89
|
end
|
93
90
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'set'
|
4
|
+
|
3
5
|
module RuboCop
|
4
6
|
module Cop
|
5
7
|
module RSpec
|
@@ -19,7 +21,9 @@ module RuboCop
|
|
19
21
|
# end
|
20
22
|
# end
|
21
23
|
#
|
22
|
-
class SubjectStub <
|
24
|
+
class SubjectStub < Base
|
25
|
+
include RuboCop::RSpec::TopLevelGroup
|
26
|
+
|
23
27
|
MSG = 'Do not stub methods of the object under test.'
|
24
28
|
|
25
29
|
# @!method subject(node)
|
@@ -35,7 +39,7 @@ module RuboCop
|
|
35
39
|
# name # => :thing
|
36
40
|
# end
|
37
41
|
#
|
38
|
-
# @param node [RuboCop::Node]
|
42
|
+
# @param node [RuboCop::AST::Node]
|
39
43
|
#
|
40
44
|
# @yield [Symbol] subject name
|
41
45
|
def_node_matcher :subject, <<-PATTERN
|
@@ -73,72 +77,40 @@ module RuboCop
|
|
73
77
|
} ...)
|
74
78
|
PATTERN
|
75
79
|
|
76
|
-
def
|
77
|
-
|
80
|
+
def on_top_level_group(node)
|
81
|
+
@explicit_subjects = find_all_explicit_subjects(node)
|
78
82
|
|
79
|
-
|
83
|
+
find_subject_expectations(node) do |stub|
|
80
84
|
add_offense(stub)
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
84
88
|
private
|
85
89
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
# @yield [RuboCop::Node] message expectations for subject
|
91
|
-
def find_subject_stub(node, &block)
|
92
|
-
find_subject(node) do |subject_name, context|
|
93
|
-
find_subject_expectation(context, subject_name, &block)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
# Find a subject message expectation
|
98
|
-
#
|
99
|
-
# @param node [RuboCop::Node]
|
100
|
-
# @param subject_name [Symbol] name of subject
|
101
|
-
#
|
102
|
-
# @yield [RuboCop::Node] message expectation
|
103
|
-
def find_subject_expectation(node, subject_name, &block)
|
104
|
-
# Do not search node if it is an example group with its own subject.
|
105
|
-
return if example_group?(node) && redefines_subject?(node)
|
106
|
-
|
107
|
-
# Yield the current node if it is a message expectation.
|
108
|
-
yield(node) if message_expectation?(node, subject_name)
|
90
|
+
def find_all_explicit_subjects(node)
|
91
|
+
node.each_descendant(:block).with_object({}) do |child, h|
|
92
|
+
name = subject(child)
|
93
|
+
next unless name
|
109
94
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
end
|
95
|
+
outer_example_group = child.each_ancestor.find do |a|
|
96
|
+
example_group?(a)
|
97
|
+
end
|
115
98
|
|
116
|
-
|
117
|
-
|
118
|
-
# @param node [RuboCop::Node]
|
119
|
-
#
|
120
|
-
# @return [Boolean]
|
121
|
-
def redefines_subject?(node)
|
122
|
-
node.each_child_node.any? do |child|
|
123
|
-
subject(child) || redefines_subject?(child)
|
99
|
+
h[outer_example_group] ||= []
|
100
|
+
h[outer_example_group] << name
|
124
101
|
end
|
125
102
|
end
|
126
103
|
|
127
|
-
|
128
|
-
|
129
|
-
# @param node [RuboCop::Node]
|
130
|
-
# @param parent [RuboCop::Node,nil]
|
131
|
-
#
|
132
|
-
# @yieldparam subject_name [Symbol] name of subject being defined
|
133
|
-
# @yieldparam parent [RuboCop::Node] parent of subject definition
|
134
|
-
def find_subject(node, parent: nil, &block)
|
135
|
-
# An implicit subject is defined by RSpec when no subject is declared
|
136
|
-
subject_name = subject(node) || :subject
|
104
|
+
def find_subject_expectations(node, subject_names = [], &block)
|
105
|
+
subject_names = @explicit_subjects[node] if @explicit_subjects[node]
|
137
106
|
|
138
|
-
|
107
|
+
expectation_detected = (subject_names + [:subject]).any? do |name|
|
108
|
+
message_expectation?(node, name)
|
109
|
+
end
|
110
|
+
return yield(node) if expectation_detected
|
139
111
|
|
140
112
|
node.each_child_node do |child|
|
141
|
-
|
113
|
+
find_subject_expectations(child, subject_names, &block)
|
142
114
|
end
|
143
115
|
end
|
144
116
|
end
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
# }.to raise_error(/err/)
|
31
31
|
#
|
32
32
|
# expect { do_something }.not_to raise_error
|
33
|
-
class UnspecifiedException <
|
33
|
+
class UnspecifiedException < Base
|
34
34
|
MSG = 'Specify the exception being captured'
|
35
35
|
|
36
36
|
def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Checks that memoized helpers names are symbols or strings.
|
7
|
+
#
|
8
|
+
# @example EnforcedStyle: symbols (default)
|
9
|
+
# # bad
|
10
|
+
# subject('user') { create_user }
|
11
|
+
# let('user_name') { 'Adam' }
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# subject(:user) { create_user }
|
15
|
+
# let(:user_name) { 'Adam' }
|
16
|
+
#
|
17
|
+
# @example EnforcedStyle: strings
|
18
|
+
# # bad
|
19
|
+
# subject(:user) { create_user }
|
20
|
+
# let(:user_name) { 'Adam' }
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# subject('user') { create_user }
|
24
|
+
# let('user_name') { 'Adam' }
|
25
|
+
class VariableDefinition < Base
|
26
|
+
include ConfigurableEnforcedStyle
|
27
|
+
include RuboCop::RSpec::Variable
|
28
|
+
|
29
|
+
MSG = 'Use %<style>s for variable names.'
|
30
|
+
|
31
|
+
def on_send(node)
|
32
|
+
variable_definition?(node) do |variable|
|
33
|
+
if style_violation?(variable)
|
34
|
+
add_offense(variable, message: format(MSG, style: style))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def style_violation?(variable)
|
42
|
+
style == :symbols && string?(variable) ||
|
43
|
+
style == :strings && symbol?(variable)
|
44
|
+
end
|
45
|
+
|
46
|
+
def string?(node)
|
47
|
+
node.str_type?
|
48
|
+
end
|
49
|
+
|
50
|
+
def symbol?(node)
|
51
|
+
node.sym_type? || node.dsym_type?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Checks that memoized helper names use the configured style.
|
7
|
+
#
|
8
|
+
# Variables can be excluded from checking using the `IgnoredPatterns`
|
9
|
+
# option.
|
10
|
+
#
|
11
|
+
# @example EnforcedStyle: snake_case (default)
|
12
|
+
# # bad
|
13
|
+
# subject(:userName1) { 'Adam' }
|
14
|
+
# let(:userName2) { 'Adam' }
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# subject(:user_name_1) { 'Adam' }
|
18
|
+
# let(:user_name_2) { 'Adam' }
|
19
|
+
#
|
20
|
+
# @example EnforcedStyle: camelCase
|
21
|
+
# # bad
|
22
|
+
# subject(:user_name_1) { 'Adam' }
|
23
|
+
# let(:user_name_2) { 'Adam' }
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# subject(:userName1) { 'Adam' }
|
27
|
+
# let(:userName2) { 'Adam' }
|
28
|
+
#
|
29
|
+
# @example IgnoredPatterns configuration
|
30
|
+
#
|
31
|
+
# # rubocop.yml
|
32
|
+
# # RSpec/VariableName:
|
33
|
+
# # EnforcedStyle: snake_case
|
34
|
+
# # IgnoredPatterns:
|
35
|
+
# # - ^userFood
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# # okay because it matches the `^userFood` regex in `IgnoredPatterns`
|
39
|
+
# subject(:userFood_1) { 'spaghetti' }
|
40
|
+
# let(:userFood_2) { 'fettuccine' }
|
41
|
+
#
|
42
|
+
class VariableName < Base
|
43
|
+
include ConfigurableNaming
|
44
|
+
include IgnoredPattern
|
45
|
+
include RuboCop::RSpec::Variable
|
46
|
+
|
47
|
+
MSG = 'Use %<style>s for variable names.'
|
48
|
+
|
49
|
+
def on_send(node)
|
50
|
+
variable_definition?(node) do |variable|
|
51
|
+
return if variable.dstr_type? || variable.dsym_type?
|
52
|
+
return if matches_ignored_pattern?(variable.value)
|
53
|
+
|
54
|
+
check_name(node, variable.value, variable.loc.expression)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def message(style)
|
61
|
+
format(MSG, style: style)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|