rubocop-factory_bot 2.23.1 → 2.25.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 +18 -1
- data/config/default.yml +53 -21
- data/lib/rubocop/cop/factory_bot/association_style.rb +65 -2
- data/lib/rubocop/cop/factory_bot/attribute_defined_statically.rb +1 -1
- data/lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb +12 -0
- data/lib/rubocop/cop/factory_bot/create_list.rb +22 -23
- data/lib/rubocop/cop/factory_bot/excessive_create_list.rb +52 -0
- data/lib/rubocop/cop/factory_bot/factory_name_style.rb +8 -1
- data/lib/rubocop/cop/factory_bot/id_sequence.rb +41 -0
- data/lib/rubocop/cop/factory_bot_cops.rb +2 -0
- data/lib/rubocop/factory_bot/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f40e3398e9038a03648df8c412610ed3d1ad3d4934a46df77667c6f0d225ddcc
|
4
|
+
data.tar.gz: 68d463bcc67e2f2fd3b38869a7267509db90cda9d7b8e6550367458d73f0f255
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 166d045079cffb99cfcdef9b192e0ad8c90ba6b789f1539de8b5e697625a26d44ca6bafef56eb007b7c9ba9a78a655f29ba9688874a6e0081d43f1c9048b996e
|
7
|
+
data.tar.gz: 891d54b5c41e9ad51eec8a1e1ec2ecda863b9f6b700420af7de8cd26494444df8c31e57f78e2783bd949871d0df2bcab15a2ecfbda13e6188d2c370d7bcc11fd
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,21 @@
|
|
2
2
|
|
3
3
|
## Master (Unreleased)
|
4
4
|
|
5
|
+
## 2.25.0 (2024-01-04)
|
6
|
+
|
7
|
+
- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])
|
8
|
+
- Add new `FactoryBot/ExcessiveCreateList` cop. ([@ddieulivol])
|
9
|
+
- Fix a false positive for `FactoryBot/ConsistentParenthesesStyle` when hash pinning. ([@ydah])
|
10
|
+
|
11
|
+
## 2.24.0 (2023-09-18)
|
12
|
+
|
13
|
+
- Fix `FactoryBot/AssociationStyle` cop to ignore explicit associations with `strategy: :build`. ([@pirj])
|
14
|
+
- Change `FactoryBot/CreateList` so that it is not an offense if not repeated multiple times. ([@ydah])
|
15
|
+
- Fix a false positive for `FactoryBot/AssociationStyle` when `association` is called in trait block and column name is keyword. ([@ydah])
|
16
|
+
- Fix a false positive for `FactoryBot/AssociationStyle` when `EnforcedStyle: Explicit` and using trait within trait. ([@ydah])
|
17
|
+
- Change `FactoryBot/AssociationStyle`, `FactoryBot/AttributeDefinedStatically`, `FactoryBot/CreateList` and `FactoryBot/FactoryClassName` to work with minitest style directory. ([@ydah])
|
18
|
+
- Add `FactoryBot/IdSequence` cop. ([@owst])
|
19
|
+
|
5
20
|
## 2.23.1 (2023-05-15)
|
6
21
|
|
7
22
|
- Fix `FactoryBot/AssociationStyle` cop for a blockless `factory`. ([@pirj])
|
@@ -22,7 +37,7 @@
|
|
22
37
|
|
23
38
|
## Previously (see [rubocop-rspec's changelist](https://github.com/rubocop/rubocop-rspec/blob/70a97b1895ce4b9bcd6ff336d5d343ddc6175fe6/CHANGELOG.md) for details)
|
24
39
|
|
25
|
-
- Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on ([@dmitrytsepelev])
|
40
|
+
- Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on. ([@dmitrytsepelev])
|
26
41
|
- Add new `RSpec/FactoryBot/FactoryNameStyle` cop. ([@ydah])
|
27
42
|
- Fix wrong autocorrection in `n_times` style on `RSpec/FactoryBot/CreateList`. ([@r7kamura])
|
28
43
|
- Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` when using `generate` with multiple arguments. ([@ydah])
|
@@ -65,6 +80,7 @@
|
|
65
80
|
[@bquorning]: https://github.com/bquorning
|
66
81
|
[@composerinteralia]: https://github.com/composerinteralia
|
67
82
|
[@darhazer]: https://github.com/Darhazer
|
83
|
+
[@ddieulivol]: https://github.com/ddieulivol
|
68
84
|
[@dmitrytsepelev]: https://github.com/dmitrytsepelev
|
69
85
|
[@harrylewis]: https://github.com/harrylewis
|
70
86
|
[@jfragoulis]: https://github.com/jfragoulis
|
@@ -73,6 +89,7 @@
|
|
73
89
|
[@liberatys]: https://github.com/Liberatys
|
74
90
|
[@morissetcl]: https://github.com/morissetcl
|
75
91
|
[@ngouy]: https://github.com/ngouy
|
92
|
+
[@owst]: https://github.com/owst
|
76
93
|
[@pirj]: https://github.com/pirj
|
77
94
|
[@r7kamura]: https://github.com/r7kamura
|
78
95
|
[@seanpdoyle]: https://github.com/seanpdoyle
|
data/config/default.yml
CHANGED
@@ -2,9 +2,10 @@
|
|
2
2
|
FactoryBot:
|
3
3
|
Enabled: true
|
4
4
|
Include:
|
5
|
-
- "
|
6
|
-
- "**/spec
|
7
|
-
- "**/test
|
5
|
+
- "**/spec/factories.rb"
|
6
|
+
- "**/spec/factories/**/*.rb"
|
7
|
+
- "**/test/factories.rb"
|
8
|
+
- "**/test/factories/**/*.rb"
|
8
9
|
- "**/features/support/factories/**/*.rb"
|
9
10
|
DocumentationBaseURL: https://docs.rubocop.org/rubocop-factory_bot
|
10
11
|
|
@@ -12,11 +13,8 @@ FactoryBot/AssociationStyle:
|
|
12
13
|
Description: Use a consistent style to define associations.
|
13
14
|
Enabled: pending
|
14
15
|
Safe: false
|
15
|
-
Include:
|
16
|
-
- spec/factories.rb
|
17
|
-
- spec/factories/**/*.rb
|
18
|
-
- features/support/factories/**/*.rb
|
19
16
|
VersionAdded: '2.23'
|
17
|
+
VersionChanged: '2.24'
|
20
18
|
EnforcedStyle: implicit
|
21
19
|
SupportedStyles:
|
22
20
|
- explicit
|
@@ -27,17 +25,18 @@ FactoryBot/AssociationStyle:
|
|
27
25
|
FactoryBot/AttributeDefinedStatically:
|
28
26
|
Description: Always declare attribute values as blocks.
|
29
27
|
Enabled: true
|
30
|
-
Include:
|
31
|
-
- spec/factories.rb
|
32
|
-
- spec/factories/**/*.rb
|
33
|
-
- features/support/factories/**/*.rb
|
34
28
|
VersionAdded: '1.28'
|
35
|
-
VersionChanged: '2.
|
29
|
+
VersionChanged: '2.24'
|
36
30
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/AttributeDefinedStatically
|
37
31
|
|
38
32
|
FactoryBot/ConsistentParenthesesStyle:
|
39
33
|
Description: Use a consistent style for parentheses in factory_bot calls.
|
40
34
|
Enabled: pending
|
35
|
+
Include:
|
36
|
+
- "**/*_spec.rb"
|
37
|
+
- "**/spec/**/*"
|
38
|
+
- "**/test/**/*"
|
39
|
+
- "**/features/support/factories/**/*.rb"
|
41
40
|
EnforcedStyle: require_parentheses
|
42
41
|
SupportedStyles:
|
43
42
|
- require_parentheses
|
@@ -53,9 +52,8 @@ FactoryBot/CreateList:
|
|
53
52
|
Include:
|
54
53
|
- "**/*_spec.rb"
|
55
54
|
- "**/spec/**/*"
|
56
|
-
-
|
57
|
-
-
|
58
|
-
- features/support/factories/**/*.rb
|
55
|
+
- "**/test/**/*"
|
56
|
+
- "**/features/support/factories/**/*.rb"
|
59
57
|
EnforcedStyle: create_list
|
60
58
|
SupportedStyles:
|
61
59
|
- create_list
|
@@ -63,12 +61,29 @@ FactoryBot/CreateList:
|
|
63
61
|
ExplicitOnly: false
|
64
62
|
SafeAutoCorrect: false
|
65
63
|
VersionAdded: '1.25'
|
66
|
-
VersionChanged: '2.
|
64
|
+
VersionChanged: '2.24'
|
67
65
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/CreateList
|
68
66
|
|
67
|
+
FactoryBot/ExcessiveCreateList:
|
68
|
+
Description: Check for excessive model creation in a list.
|
69
|
+
Enabled: pending
|
70
|
+
Include:
|
71
|
+
- "**/*_spec.rb"
|
72
|
+
- "**/spec/**/*"
|
73
|
+
- "**/test/**/*"
|
74
|
+
- "**/features/support/factories/**/*.rb"
|
75
|
+
MaxAmount: 10
|
76
|
+
VersionAdded: '2.25'
|
77
|
+
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/ExcessiveCreateList
|
78
|
+
|
69
79
|
FactoryBot/FactoryAssociationWithStrategy:
|
70
80
|
Description: Use definition in factory association instead of hard coding a strategy.
|
71
81
|
Enabled: pending
|
82
|
+
Include:
|
83
|
+
- "**/*_spec.rb"
|
84
|
+
- "**/spec/**/*"
|
85
|
+
- "**/test/**/*"
|
86
|
+
- "**/features/support/factories/**/*.rb"
|
72
87
|
VersionAdded: '2.23'
|
73
88
|
VersionChanged: '2.23'
|
74
89
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/FactoryAssociationWithStrategy
|
@@ -76,17 +91,18 @@ FactoryBot/FactoryAssociationWithStrategy:
|
|
76
91
|
FactoryBot/FactoryClassName:
|
77
92
|
Description: Use string value when setting the class attribute explicitly.
|
78
93
|
Enabled: true
|
79
|
-
Include:
|
80
|
-
- spec/factories.rb
|
81
|
-
- spec/factories/**/*.rb
|
82
|
-
- features/support/factories/**/*.rb
|
83
94
|
VersionAdded: '1.37'
|
84
|
-
VersionChanged: '2.
|
95
|
+
VersionChanged: '2.24'
|
85
96
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/FactoryClassName
|
86
97
|
|
87
98
|
FactoryBot/FactoryNameStyle:
|
88
99
|
Description: Checks for name style for argument of FactoryBot::Syntax::Methods.
|
89
100
|
Enabled: pending
|
101
|
+
Include:
|
102
|
+
- "**/*_spec.rb"
|
103
|
+
- "**/spec/**/*"
|
104
|
+
- "**/test/**/*"
|
105
|
+
- "**/features/support/factories/**/*.rb"
|
90
106
|
EnforcedStyle: symbol
|
91
107
|
SupportedStyles:
|
92
108
|
- symbol
|
@@ -96,15 +112,31 @@ FactoryBot/FactoryNameStyle:
|
|
96
112
|
VersionChanged: '2.23'
|
97
113
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/FactoryNameStyle
|
98
114
|
|
115
|
+
FactoryBot/IdSequence:
|
116
|
+
Description: Do not create a FactoryBot sequence for an id column.
|
117
|
+
Enabled: pending
|
118
|
+
VersionAdded: '2.24'
|
119
|
+
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/IdSequence
|
120
|
+
|
99
121
|
FactoryBot/RedundantFactoryOption:
|
100
122
|
Description: Checks for redundant `factory` option.
|
101
123
|
Enabled: pending
|
124
|
+
Include:
|
125
|
+
- "**/*_spec.rb"
|
126
|
+
- "**/spec/**/*"
|
127
|
+
- "**/test/**/*"
|
128
|
+
- "**/features/support/factories/**/*.rb"
|
102
129
|
VersionAdded: '2.23'
|
103
130
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/RedundantFactoryOption
|
104
131
|
|
105
132
|
FactoryBot/SyntaxMethods:
|
106
133
|
Description: Use shorthands from `FactoryBot::Syntax::Methods` in your specs.
|
107
134
|
Enabled: pending
|
135
|
+
Include:
|
136
|
+
- "**/*_spec.rb"
|
137
|
+
- "**/spec/**/*"
|
138
|
+
- "**/test/**/*"
|
139
|
+
- "**/features/support/factories/**/*.rb"
|
108
140
|
SafeAutoCorrect: false
|
109
141
|
VersionAdded: '2.7'
|
110
142
|
Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/SyntaxMethods
|
@@ -21,6 +21,16 @@ module RuboCop
|
|
21
21
|
# user
|
22
22
|
# end
|
23
23
|
#
|
24
|
+
# # bad
|
25
|
+
# factory :post do
|
26
|
+
# association :user, :author
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# factory :post do
|
31
|
+
# user factory: %i[user author]
|
32
|
+
# end
|
33
|
+
#
|
24
34
|
# @example EnforcedStyle: explicit
|
25
35
|
# # bad
|
26
36
|
# factory :post do
|
@@ -32,6 +42,16 @@ module RuboCop
|
|
32
42
|
# association :user
|
33
43
|
# end
|
34
44
|
#
|
45
|
+
# # bad
|
46
|
+
# factory :post do
|
47
|
+
# user factory: %i[user author]
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# # good
|
51
|
+
# factory :post do
|
52
|
+
# association :user, :author
|
53
|
+
# end
|
54
|
+
#
|
35
55
|
# # good (NonImplicitAssociationMethodNames: ['email'])
|
36
56
|
# sequence :email do |n|
|
37
57
|
# "person#{n}@example.com"
|
@@ -53,6 +73,11 @@ module RuboCop
|
|
53
73
|
].freeze
|
54
74
|
|
55
75
|
RESTRICT_ON_SEND = %i[factory trait].freeze
|
76
|
+
KEYWORDS = %i[alias and begin break case class def defined? do
|
77
|
+
else elsif end ensure false for if in module
|
78
|
+
next nil not or redo rescue retry return self
|
79
|
+
super then true undef unless until when while
|
80
|
+
yield __FILE__ __LINE__ __ENCODING__].freeze
|
56
81
|
|
57
82
|
def on_send(node)
|
58
83
|
bad_associations_in(node).each do |association|
|
@@ -72,6 +97,13 @@ module RuboCop
|
|
72
97
|
(send nil? :association sym ...)
|
73
98
|
PATTERN
|
74
99
|
|
100
|
+
# @!method with_strategy_build_option?(node)
|
101
|
+
def_node_matcher :with_strategy_build_option?, <<~PATTERN
|
102
|
+
(send nil? :association sym ...
|
103
|
+
(hash <(pair (sym :strategy) (sym :build)) ...>)
|
104
|
+
)
|
105
|
+
PATTERN
|
106
|
+
|
75
107
|
# @!method implicit_association?(node)
|
76
108
|
def_node_matcher :implicit_association?, <<~PATTERN
|
77
109
|
(send nil? !#non_implicit_association_method_name? ...)
|
@@ -103,6 +135,16 @@ module RuboCop
|
|
103
135
|
(send nil? :association _ (sym $_)* ...)
|
104
136
|
PATTERN
|
105
137
|
|
138
|
+
# @!method association_names(node)
|
139
|
+
def_node_search :association_names, <<~PATTERN
|
140
|
+
(send nil? :association $...)
|
141
|
+
PATTERN
|
142
|
+
|
143
|
+
# @!method trait_name(node)
|
144
|
+
def_node_search :trait_name, <<~PATTERN
|
145
|
+
(send nil? :trait (sym $_) )
|
146
|
+
PATTERN
|
147
|
+
|
106
148
|
def autocorrect(corrector, node)
|
107
149
|
if style == :explicit
|
108
150
|
autocorrect_to_explicit_style(corrector, node)
|
@@ -131,9 +173,22 @@ module RuboCop
|
|
131
173
|
|
132
174
|
def bad?(node)
|
133
175
|
if style == :explicit
|
134
|
-
implicit_association?(node)
|
176
|
+
implicit_association?(node) &&
|
177
|
+
!trait_within_trait?(node)
|
135
178
|
else
|
136
|
-
explicit_association?(node)
|
179
|
+
explicit_association?(node) &&
|
180
|
+
!with_strategy_build_option?(node) &&
|
181
|
+
!keyword?(node)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
def keyword?(node)
|
186
|
+
association_names(node).any? do |associations|
|
187
|
+
associations.any? do |association|
|
188
|
+
next unless association.sym_type?
|
189
|
+
|
190
|
+
KEYWORDS.include?(association.value)
|
191
|
+
end
|
137
192
|
end
|
138
193
|
end
|
139
194
|
|
@@ -190,6 +245,14 @@ module RuboCop
|
|
190
245
|
end
|
191
246
|
options
|
192
247
|
end
|
248
|
+
|
249
|
+
def trait_within_trait?(node)
|
250
|
+
factory_node = node.ancestors.reverse.find do |ancestor|
|
251
|
+
ancestor.method?(:factory) if ancestor.block_type?
|
252
|
+
end
|
253
|
+
|
254
|
+
trait_name(factory_node).include?(node.method_name)
|
255
|
+
end
|
193
256
|
end
|
194
257
|
end
|
195
258
|
end
|
@@ -75,6 +75,17 @@ module RuboCop
|
|
75
75
|
)
|
76
76
|
PATTERN
|
77
77
|
|
78
|
+
# @!method omit_hash_value?(node)
|
79
|
+
def_node_matcher :omit_hash_value?, <<~PATTERN
|
80
|
+
(send
|
81
|
+
#factory_call? %FACTORY_CALLS
|
82
|
+
{sym str send lvar}
|
83
|
+
(hash
|
84
|
+
<value_omission? ...>
|
85
|
+
)
|
86
|
+
)
|
87
|
+
PATTERN
|
88
|
+
|
78
89
|
def self.autocorrect_incompatible_with
|
79
90
|
[Style::MethodCallWithArgsParentheses]
|
80
91
|
end
|
@@ -97,6 +108,7 @@ module RuboCop
|
|
97
108
|
def register_offense_with_parentheses(node)
|
98
109
|
return if style == :require_parentheses || !node.parenthesized?
|
99
110
|
return unless same_line?(node, node.first_argument)
|
111
|
+
return if omit_hash_value?(node)
|
100
112
|
|
101
113
|
add_offense(node.loc.selector,
|
102
114
|
message: MSG_OMIT_PARENS) do |corrector|
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
# create_list :user, 3
|
59
59
|
# 3.times { create :user }
|
60
60
|
#
|
61
|
-
class CreateList < ::RuboCop::Cop::Base
|
61
|
+
class CreateList < ::RuboCop::Cop::Base # rubocop:disable Metrics/ClassLength
|
62
62
|
extend AutoCorrector
|
63
63
|
include ConfigurableEnforcedStyle
|
64
64
|
include RuboCop::FactoryBot::Language
|
@@ -68,24 +68,15 @@ module RuboCop
|
|
68
68
|
MSG_N_TIMES = 'Prefer %<number>s.times.map.'
|
69
69
|
RESTRICT_ON_SEND = %i[create_list].freeze
|
70
70
|
|
71
|
-
# @!method
|
72
|
-
def_node_matcher :
|
73
|
-
(block
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# @!method n_times?(node)
|
82
|
-
def_node_matcher :n_times?, <<-PATTERN
|
83
|
-
(send (int _) :times)
|
84
|
-
PATTERN
|
85
|
-
|
86
|
-
# @!method n_times_map?(node)
|
87
|
-
def_node_matcher :n_times_map?, <<-PATTERN
|
88
|
-
(send #n_times? :map)
|
71
|
+
# @!method repeat_count(node)
|
72
|
+
def_node_matcher :repeat_count, <<-PATTERN
|
73
|
+
(block
|
74
|
+
{
|
75
|
+
(send (const {nil? cbase} :Array) :new (int $_)) # Array.new(3) { create(:user) }
|
76
|
+
(send (int $_) :times) # 3.times { create(:user) }
|
77
|
+
(send (send (int $_) :times) :map) # 3.times.map { create(:user) }
|
78
|
+
}
|
79
|
+
...)
|
89
80
|
PATTERN
|
90
81
|
|
91
82
|
# @!method block_with_arg_and_used?(node)
|
@@ -99,7 +90,7 @@ module RuboCop
|
|
99
90
|
|
100
91
|
# @!method arguments_include_method_call?(node)
|
101
92
|
def_node_matcher :arguments_include_method_call?, <<~PATTERN
|
102
|
-
(send #factory_call? :create sym
|
93
|
+
(send #factory_call? :create sym `(send ...))
|
103
94
|
PATTERN
|
104
95
|
|
105
96
|
# @!method factory_call(node)
|
@@ -119,6 +110,7 @@ module RuboCop
|
|
119
110
|
|
120
111
|
def on_array(node)
|
121
112
|
return unless same_factory_calls_in_array?(node)
|
113
|
+
return if node.values.size < 2
|
122
114
|
|
123
115
|
add_offense(
|
124
116
|
node,
|
@@ -128,10 +120,9 @@ module RuboCop
|
|
128
120
|
end
|
129
121
|
end
|
130
122
|
|
131
|
-
def on_block(node) # rubocop:
|
123
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, Metrics/CyclomaticComplexity
|
132
124
|
return unless style == :create_list
|
133
|
-
|
134
|
-
return unless repetition_block?(node)
|
125
|
+
return unless repeat_multiple_time?(node)
|
135
126
|
return if block_with_arg_and_used?(node)
|
136
127
|
return unless node.body
|
137
128
|
return if arguments_include_method_call?(node.body)
|
@@ -146,6 +137,8 @@ module RuboCop
|
|
146
137
|
return unless style == :n_times
|
147
138
|
|
148
139
|
factory_list_call(node) do |count|
|
140
|
+
next if count < 2
|
141
|
+
|
149
142
|
message = format(MSG_N_TIMES, number: count)
|
150
143
|
add_offense(node.loc.selector, message: message) do |corrector|
|
151
144
|
TimesCorrector.new(node).call(corrector)
|
@@ -155,6 +148,12 @@ module RuboCop
|
|
155
148
|
|
156
149
|
private
|
157
150
|
|
151
|
+
def repeat_multiple_time?(node)
|
152
|
+
return false unless (count = repeat_count(node))
|
153
|
+
|
154
|
+
count > 1
|
155
|
+
end
|
156
|
+
|
158
157
|
# For ease of modification, it is replaced with the `n_times` style,
|
159
158
|
# but if it is not appropriate for the configured style,
|
160
159
|
# it will be replaced in the subsequent autocorrection.
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module FactoryBot
|
6
|
+
# Check for excessive model creation in a list.
|
7
|
+
#
|
8
|
+
# @example MaxAmount: 10 (default)
|
9
|
+
# # We do not allow more than 10 items to be created
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# create_list(:merge_request, 1000, state: :opened)
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# create_list(:merge_request, 10, state: :opened)
|
16
|
+
#
|
17
|
+
# @example MaxAmount: 20
|
18
|
+
# # We do not allow more than 20 items to be created
|
19
|
+
#
|
20
|
+
# # bad
|
21
|
+
# create_list(:merge_request, 1000, state: :opened)
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# create_list(:merge_request, 15, state: :opened)
|
25
|
+
#
|
26
|
+
class ExcessiveCreateList < ::RuboCop::Cop::Base
|
27
|
+
include ConfigurableExplicitOnly
|
28
|
+
|
29
|
+
MESSAGE =
|
30
|
+
'Avoid using `create_list` with more than %<max_amount>s items.'
|
31
|
+
|
32
|
+
# @!method create_list?(node)
|
33
|
+
def_node_matcher :create_list?, <<~PATTERN
|
34
|
+
(send #factory_call? :create_list {sym str} $(int _) ...)
|
35
|
+
PATTERN
|
36
|
+
|
37
|
+
RESTRICT_ON_SEND = %i[create_list].freeze
|
38
|
+
|
39
|
+
def on_send(node)
|
40
|
+
number_node = create_list?(node)
|
41
|
+
return unless number_node
|
42
|
+
|
43
|
+
max_amount = cop_config['MaxAmount']
|
44
|
+
return if number_node.value <= max_amount
|
45
|
+
|
46
|
+
add_offense(number_node, message:
|
47
|
+
format(MESSAGE, max_amount: max_amount))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -14,6 +14,9 @@ module RuboCop
|
|
14
14
|
# create(:user)
|
15
15
|
# build :user, username: "NAME"
|
16
16
|
#
|
17
|
+
# # good - namespaced models
|
18
|
+
# create('users/internal')
|
19
|
+
#
|
17
20
|
# @example EnforcedStyle: string
|
18
21
|
# # bad
|
19
22
|
# create(:user)
|
@@ -76,13 +79,17 @@ module RuboCop
|
|
76
79
|
private
|
77
80
|
|
78
81
|
def offense_for_symbol_style?(name)
|
79
|
-
name.str_type? && style == :symbol
|
82
|
+
name.str_type? && style == :symbol && !namespaced?(name)
|
80
83
|
end
|
81
84
|
|
82
85
|
def offense_for_string_style?(name)
|
83
86
|
name.sym_type? && style == :string
|
84
87
|
end
|
85
88
|
|
89
|
+
def namespaced?(name)
|
90
|
+
name.value.include?('/')
|
91
|
+
end
|
92
|
+
|
86
93
|
def register_offense(name, prefer)
|
87
94
|
add_offense(name,
|
88
95
|
message: format(MSG, prefer: style.to_s)) do |corrector|
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module FactoryBot
|
6
|
+
# Do not create a FactoryBot sequence for an id column.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad - can lead to conflicts between FactoryBot and DB sequences
|
10
|
+
# factory :foo do
|
11
|
+
# sequence :id
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# # good - a non-id column
|
15
|
+
# factory :foo do
|
16
|
+
# sequence :some_non_id_column
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
class IdSequence < ::RuboCop::Cop::Base
|
20
|
+
extend AutoCorrector
|
21
|
+
include RangeHelp
|
22
|
+
|
23
|
+
MSG = 'Do not create a sequence for an id attribute'
|
24
|
+
RESTRICT_ON_SEND = %i[sequence].freeze
|
25
|
+
|
26
|
+
def on_send(node)
|
27
|
+
return unless node.first_argument.value == :id
|
28
|
+
|
29
|
+
add_offense(node) do |corrector|
|
30
|
+
range_to_remove = range_by_whole_lines(
|
31
|
+
node.source_range,
|
32
|
+
include_final_newline: true
|
33
|
+
)
|
34
|
+
|
35
|
+
corrector.remove(range_to_remove)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -4,8 +4,10 @@ require_relative 'factory_bot/association_style'
|
|
4
4
|
require_relative 'factory_bot/attribute_defined_statically'
|
5
5
|
require_relative 'factory_bot/consistent_parentheses_style'
|
6
6
|
require_relative 'factory_bot/create_list'
|
7
|
+
require_relative 'factory_bot/excessive_create_list'
|
7
8
|
require_relative 'factory_bot/factory_association_with_strategy'
|
8
9
|
require_relative 'factory_bot/factory_class_name'
|
9
10
|
require_relative 'factory_bot/factory_name_style'
|
11
|
+
require_relative 'factory_bot/id_sequence'
|
10
12
|
require_relative 'factory_bot/redundant_factory_option'
|
11
13
|
require_relative 'factory_bot/syntax_methods'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-factory_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.25.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Backus
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2024-01-04 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rubocop
|
@@ -48,9 +48,11 @@ files:
|
|
48
48
|
- lib/rubocop/cop/factory_bot/attribute_defined_statically.rb
|
49
49
|
- lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb
|
50
50
|
- lib/rubocop/cop/factory_bot/create_list.rb
|
51
|
+
- lib/rubocop/cop/factory_bot/excessive_create_list.rb
|
51
52
|
- lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
|
52
53
|
- lib/rubocop/cop/factory_bot/factory_class_name.rb
|
53
54
|
- lib/rubocop/cop/factory_bot/factory_name_style.rb
|
55
|
+
- lib/rubocop/cop/factory_bot/id_sequence.rb
|
54
56
|
- lib/rubocop/cop/factory_bot/mixin/configurable_explicit_only.rb
|
55
57
|
- lib/rubocop/cop/factory_bot/redundant_factory_option.rb
|
56
58
|
- lib/rubocop/cop/factory_bot/syntax_methods.rb
|
@@ -82,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
84
|
- !ruby/object:Gem::Version
|
83
85
|
version: '0'
|
84
86
|
requirements: []
|
85
|
-
rubygems_version: 3.4.
|
87
|
+
rubygems_version: 3.4.22
|
86
88
|
signing_key:
|
87
89
|
specification_version: 4
|
88
90
|
summary: Code style checking for factory_bot files
|