rubocop-factory_bot 2.24.0 → 2.26.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2bb256bf3c94a3ff0eca302abed9006719524c843b826f194fbb68c29fea483
4
- data.tar.gz: 7b619d4d6732c155369caf1edef5f8c6b1af302ba4114c4ace18eb462e2d14af
3
+ metadata.gz: f13120d5e06606e2f01a7521724acfcb9d79989c5dcd5725cfb9dc509f4b9244
4
+ data.tar.gz: a6dc74ea1bed0ae4b52a7449c2e94d11a6d404803f8eaf57b1ff4bb934a7869e
5
5
  SHA512:
6
- metadata.gz: 79e7569730a8530231ac5b5f584f35a7e0c2d2d23174966cee57ce5b4ea45d32617bff8adf5ba0d2ad8a4fe59a32c9bd6a6bf67d90f587cfec0a9b4a558e1bc7
7
- data.tar.gz: '02965c01b59d3583720b8ca926f0cd2db764557ec41ac4992c582e0622d6d5b364a167ed3933485bd8df7e2d882febdd353878e88e1b175145ea350ffcc1da6b'
6
+ metadata.gz: feb49a6ac85e680b9517a54937e96c0d246723082d92f1f995f90735a2b6af34bff9c8681517e164969aacc84cae22a0f1289723428050787e4096c11ba2d735
7
+ data.tar.gz: 151c71b63326bcd320be0db9a78a92cff0718214e41a03c349f2c178fdf3c3609a57f911658397934848b58d934b48f85ee12032daff500db8b0b67f2f0ace02
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 2.26.0 (2024-06-08)
6
+
7
+ - Fix a false positive for `FactoryBot/AssociationStyle` when using nested factories with traits. ([@jaydorsey])
8
+ - Support `AutoCorrect: contextual` option for LSP. ([@ydah])
9
+
10
+ ## 2.25.1 (2024-01-08)
11
+
12
+ - Fix a false positive for `FactoryBot/CreateList` when create call does have method calls and repeat multiple times with other argument. ([@ydah])
13
+ - Fix an error occurred for `FactoryBot/IdSequence` when `sequence` with non-symbol argument or without argument. ([@ydah])
14
+
15
+ ## 2.25.0 (2024-01-04)
16
+
17
+ - Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])
18
+ - Add new `FactoryBot/ExcessiveCreateList` cop. ([@ddieulivol])
19
+ - Fix a false positive for `FactoryBot/ConsistentParenthesesStyle` when hash pinning. ([@ydah])
20
+
5
21
  ## 2.24.0 (2023-09-18)
6
22
 
7
23
  - Fix `FactoryBot/AssociationStyle` cop to ignore explicit associations with `strategy: :build`. ([@pirj])
@@ -31,7 +47,7 @@
31
47
 
32
48
  ## Previously (see [rubocop-rspec's changelist](https://github.com/rubocop/rubocop-rspec/blob/70a97b1895ce4b9bcd6ff336d5d343ddc6175fe6/CHANGELOG.md) for details)
33
49
 
34
- - Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on ([@dmitrytsepelev])
50
+ - Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` inside `&&`, `||` and `:?` when `omit_parentheses` is on. ([@dmitrytsepelev])
35
51
  - Add new `RSpec/FactoryBot/FactoryNameStyle` cop. ([@ydah])
36
52
  - Fix wrong autocorrection in `n_times` style on `RSpec/FactoryBot/CreateList`. ([@r7kamura])
37
53
  - Fix a false positive for `RSpec/FactoryBot/ConsistentParenthesesStyle` when using `generate` with multiple arguments. ([@ydah])
@@ -74,8 +90,10 @@
74
90
  [@bquorning]: https://github.com/bquorning
75
91
  [@composerinteralia]: https://github.com/composerinteralia
76
92
  [@darhazer]: https://github.com/Darhazer
93
+ [@ddieulivol]: https://github.com/ddieulivol
77
94
  [@dmitrytsepelev]: https://github.com/dmitrytsepelev
78
95
  [@harrylewis]: https://github.com/harrylewis
96
+ [@jaydorsey]: https://github.com/jaydorsey
79
97
  [@jfragoulis]: https://github.com/jfragoulis
80
98
  [@jonatas]: https://github.com/jonatas
81
99
  [@leoarnold]: https://github.com/leoarnold
data/config/default.yml CHANGED
@@ -14,7 +14,7 @@ FactoryBot/AssociationStyle:
14
14
  Enabled: pending
15
15
  Safe: false
16
16
  VersionAdded: '2.23'
17
- VersionChanged: "<<next>>"
17
+ VersionChanged: '2.24'
18
18
  EnforcedStyle: implicit
19
19
  SupportedStyles:
20
20
  - explicit
@@ -26,7 +26,7 @@ FactoryBot/AttributeDefinedStatically:
26
26
  Description: Always declare attribute values as blocks.
27
27
  Enabled: true
28
28
  VersionAdded: '1.28'
29
- VersionChanged: "<<next>>"
29
+ VersionChanged: '2.24'
30
30
  Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/AttributeDefinedStatically
31
31
 
32
32
  FactoryBot/ConsistentParenthesesStyle:
@@ -49,6 +49,7 @@ FactoryBot/ConsistentParenthesesStyle:
49
49
  FactoryBot/CreateList:
50
50
  Description: Checks for create_list usage.
51
51
  Enabled: true
52
+ AutoCorrect: contextual
52
53
  Include:
53
54
  - "**/*_spec.rb"
54
55
  - "**/spec/**/*"
@@ -61,9 +62,21 @@ FactoryBot/CreateList:
61
62
  ExplicitOnly: false
62
63
  SafeAutoCorrect: false
63
64
  VersionAdded: '1.25'
64
- VersionChanged: "<<next>>"
65
+ VersionChanged: '2.26'
65
66
  Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/CreateList
66
67
 
68
+ FactoryBot/ExcessiveCreateList:
69
+ Description: Check for excessive model creation in a list.
70
+ Enabled: pending
71
+ Include:
72
+ - "**/*_spec.rb"
73
+ - "**/spec/**/*"
74
+ - "**/test/**/*"
75
+ - "**/features/support/factories/**/*.rb"
76
+ MaxAmount: 10
77
+ VersionAdded: '2.25'
78
+ Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/ExcessiveCreateList
79
+
67
80
  FactoryBot/FactoryAssociationWithStrategy:
68
81
  Description: Use definition in factory association instead of hard coding a strategy.
69
82
  Enabled: pending
@@ -80,7 +93,7 @@ FactoryBot/FactoryClassName:
80
93
  Description: Use string value when setting the class attribute explicitly.
81
94
  Enabled: true
82
95
  VersionAdded: '1.37'
83
- VersionChanged: "<<next>>"
96
+ VersionChanged: '2.24'
84
97
  Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/FactoryClassName
85
98
 
86
99
  FactoryBot/FactoryNameStyle:
@@ -103,7 +116,7 @@ FactoryBot/FactoryNameStyle:
103
116
  FactoryBot/IdSequence:
104
117
  Description: Do not create a FactoryBot sequence for an id column.
105
118
  Enabled: pending
106
- VersionAdded: "<<next>>"
119
+ VersionAdded: '2.24'
107
120
  Reference: https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/FactoryBot/IdSequence
108
121
 
109
122
  FactoryBot/RedundantFactoryOption:
@@ -67,6 +67,7 @@ module RuboCop
67
67
 
68
68
  DEFAULT_NON_IMPLICIT_ASSOCIATION_METHOD_NAMES = %w[
69
69
  association
70
+ factory
70
71
  sequence
71
72
  skip_create
72
73
  traits_for_enum
@@ -70,7 +70,7 @@ module RuboCop
70
70
  end
71
71
 
72
72
  def receiver_matches_first_block_argument?(receiver, node)
73
- first_block_argument = node.arguments.first
73
+ first_block_argument = node.first_argument
74
74
 
75
75
  !first_block_argument.nil? &&
76
76
  receiver.lvar_type? &&
@@ -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|
@@ -69,7 +69,7 @@ module RuboCop
69
69
  RESTRICT_ON_SEND = %i[create_list].freeze
70
70
 
71
71
  # @!method repeat_count(node)
72
- def_node_matcher :repeat_count, <<-PATTERN
72
+ def_node_matcher :repeat_count, <<~PATTERN
73
73
  (block
74
74
  {
75
75
  (send (const {nil? cbase} :Array) :new (int $_)) # Array.new(3) { create(:user) }
@@ -90,7 +90,7 @@ module RuboCop
90
90
 
91
91
  # @!method arguments_include_method_call?(node)
92
92
  def_node_matcher :arguments_include_method_call?, <<~PATTERN
93
- (send #factory_call? :create sym `(send ...))
93
+ (send #factory_call? :create sym ... `(send ...))
94
94
  PATTERN
95
95
 
96
96
  # @!method factory_call(node)
@@ -104,7 +104,7 @@ module RuboCop
104
104
  PATTERN
105
105
 
106
106
  # @!method factory_calls_in_array?(node)
107
- def_node_search :factory_calls_in_array?, <<-PATTERN
107
+ def_node_search :factory_calls_in_array?, <<~PATTERN
108
108
  (array #factory_call+)
109
109
  PATTERN
110
110
 
@@ -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|
@@ -19,12 +19,15 @@ module RuboCop
19
19
  class IdSequence < ::RuboCop::Cop::Base
20
20
  extend AutoCorrector
21
21
  include RangeHelp
22
+ include RuboCop::FactoryBot::Language
22
23
 
23
24
  MSG = 'Do not create a sequence for an id attribute'
24
25
  RESTRICT_ON_SEND = %i[sequence].freeze
25
26
 
26
27
  def on_send(node)
27
- return unless node.first_argument.value == :id
28
+ return unless node.receiver.nil? || factory_bot?(node.receiver)
29
+ return unless node.first_argument&.sym_type? &&
30
+ node.first_argument.value == :id
28
31
 
29
32
  add_offense(node) do |corrector|
30
33
  range_to_remove = range_by_whole_lines(
@@ -4,6 +4,7 @@ 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'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module FactoryBot
5
+ module Cop
6
+ # Source and spec generator for new cops
7
+ #
8
+ # This generator will take a cop name and generate a source file
9
+ # and spec file when given a valid qualified cop name.
10
+ # @api private
11
+ class Generator < RuboCop::Cop::Generator
12
+ def todo
13
+ <<~TODO
14
+ Do 4 steps:
15
+ 1. Modify the description of #{badge} in config/default.yml
16
+ 2. Implement your new cop in the generated file!
17
+ 3. Add an entry about new cop to CHANGELOG.md
18
+ 4. Commit your new cop with a message such as
19
+ e.g. "Add new `#{badge}` cop"
20
+ TODO
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module FactoryBot
5
5
  # Version information for the factory_bot RuboCop plugin.
6
6
  module Version
7
- STRING = '2.24.0'
7
+ STRING = '2.26.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-factory_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.24.0
4
+ version: 2.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Backus
@@ -9,10 +9,10 @@ authors:
9
9
  - Phil Pirozhkov
10
10
  - Maxim Krizhanovsky
11
11
  - Andrew Bromwich
12
- autorequire:
12
+ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2023-09-18 00:00:00.000000000 Z
15
+ date: 2024-06-08 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rubocop
@@ -20,18 +20,18 @@ dependencies:
20
20
  requirements:
21
21
  - - "~>"
22
22
  - !ruby/object:Gem::Version
23
- version: '1.33'
23
+ version: '1.41'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - "~>"
29
29
  - !ruby/object:Gem::Version
30
- version: '1.33'
30
+ version: '1.41'
31
31
  description: |
32
32
  Code style checking for factory_bot files.
33
33
  A plugin for the RuboCop code style enforcing & linting tool.
34
- email:
34
+ email:
35
35
  executables: []
36
36
  extensions: []
37
37
  extra_rdoc_files:
@@ -48,6 +48,7 @@ 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
@@ -57,6 +58,7 @@ files:
57
58
  - lib/rubocop/cop/factory_bot/syntax_methods.rb
58
59
  - lib/rubocop/cop/factory_bot_cops.rb
59
60
  - lib/rubocop/factory_bot/config_formatter.rb
61
+ - lib/rubocop/factory_bot/cop/generator.rb
60
62
  - lib/rubocop/factory_bot/description_extractor.rb
61
63
  - lib/rubocop/factory_bot/factory_bot.rb
62
64
  - lib/rubocop/factory_bot/language.rb
@@ -68,7 +70,7 @@ metadata:
68
70
  changelog_uri: https://github.com/rubocop/rubocop-factory_bot/blob/master/CHANGELOG.md
69
71
  documentation_uri: https://docs.rubocop.org/rubocop-factory_bot/
70
72
  rubygems_mfa_required: 'true'
71
- post_install_message:
73
+ post_install_message:
72
74
  rdoc_options: []
73
75
  require_paths:
74
76
  - lib
@@ -83,8 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
85
  - !ruby/object:Gem::Version
84
86
  version: '0'
85
87
  requirements: []
86
- rubygems_version: 3.3.7
87
- signing_key:
88
+ rubygems_version: 3.5.9
89
+ signing_key:
88
90
  specification_version: 4
89
91
  summary: Code style checking for factory_bot files
90
92
  test_files: []