momocop 0.1.12 → 0.1.13

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: acef3c7f38f87034b619c4b5e9ca7ca77ac880a0a665f46abff3a19455ef2d5f
4
- data.tar.gz: 4e06a90596825134bdad568b41b38ebdb93056d0fab4f58d381c8ffd6d16e6e5
3
+ metadata.gz: 220069b3119b5c48271dcd6881e4a1530074eabbc697d12dca9ea70cf5dbbbe8
4
+ data.tar.gz: 02fcf5cad166113116cb7876aa46f1ffa0de65391488901727286711645a4917
5
5
  SHA512:
6
- metadata.gz: 7c18582cdb50926ee4bbc7074c076d90f06504cdec64d0dc4a1389dadac1ad000864cdcea4e9e4322a7fcc2bc6bbb8aadd381a29cef2e4bdee1dee3f751833a9
7
- data.tar.gz: 6ca756d9793ca03ef4ca8ea88e37d9169bea071641464af88f36e96688a613fa5dc09c3e50b252c56e79e1cf9376f46db1717e0b4dfa7d46be8352fbb656582a
6
+ metadata.gz: 2ae69bdfff4429dc93e5e0a78fa4d87afb29bdf5d3bec1bffc3aec97b569066d47e2f0ba215593d0599cbc781591ea38b44dcf128cffbda503ba4b03ced79224
7
+ data.tar.gz: 22b9e10997ec1a2f8f32d33e503285a7f0ca6809d3c7081713b481c8ba245753d8ed944e57d2032f9f3a243894dbd16018f4d8cff7dcb24b55bf3144280d4813
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Momocop
4
- VERSION = '0.1.12'
4
+ VERSION = '0.1.13'
5
5
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Momocop
6
+ # Enforces inline association definitions in FactoryBot factories.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # factory :blog_post do
11
+ # association(:user)
12
+ # end
13
+ #
14
+ # # good
15
+ # factory :blog_post do
16
+ # user { association :user }
17
+ # end
18
+ class FactoryBotInlineAssociation < RuboCop::Cop::Base
19
+ extend AutoCorrector
20
+
21
+ MSG = 'Use inline association definition instead of separate `association` method call.'
22
+
23
+ RESTRICT_ON_SEND = %i[association].freeze
24
+
25
+ def_node_matcher :association_call?, <<-PATTERN
26
+ (send nil? :association (sym _))
27
+ PATTERN
28
+
29
+ def on_send(node)
30
+ return unless inside_factory_bot_define?(node)
31
+ return unless inside_factory_bot_factory?(node)
32
+
33
+ add_offense(node.loc.selector, message: MSG) do |corrector|
34
+ association_name = node.arguments.first.value
35
+ options = node.arguments.drop(1).map(&:source).join(', ')
36
+
37
+ replacement =
38
+ if options.empty?
39
+ "#{association_name} { association :#{association_name} }"
40
+ else
41
+ "#{association_name} { association :#{association_name}, #{options} }"
42
+ end
43
+ corrector.replace(node, replacement)
44
+ end
45
+ end
46
+
47
+ private def inside_factory_bot_factory?(node)
48
+ context = node.each_ancestor(:block).first
49
+ send_node = context.block_type? ? context.send_node : context
50
+
51
+ return send_node.method_name == :factory
52
+ end
53
+
54
+ private def inside_factory_bot_define?(node)
55
+ ancestors = node.each_ancestor(:block).to_a
56
+ ancestors.any? { |ancestor| ancestor.method_name == :define && ancestor.receiver&.const_name == 'FactoryBot' }
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -117,7 +117,7 @@ module RuboCop
117
117
  end
118
118
 
119
119
  private def generate_association_definition(property)
120
- "association(:#{property})"
120
+ "#{property} { association :#{property} }"
121
121
  end
122
122
 
123
123
  private def inside_factory_bot_define?(node)
@@ -116,8 +116,17 @@ module RuboCop
116
116
 
117
117
  private def definition_type(node)
118
118
  send_node = node.send_type? ? node : node.children.first
119
+ has_association_body =
120
+ send_node
121
+ .block_node
122
+ &.children
123
+ &.last
124
+ &.children
125
+ &.any? { _1.is_a?(Parser::AST::Node) && _1.send_type? && _1.method_name == :association }
119
126
  if %i[association sequence].include? send_node.method_name
120
127
  send_node.method_name
128
+ elsif has_association_body
129
+ :association
121
130
  else
122
131
  :property
123
132
  end
@@ -33,7 +33,6 @@ module RuboCop
33
33
  return if factory_name.singularize == factory_name
34
34
 
35
35
  add_offense(node.first_argument) do |corrector|
36
- p node.first_argument
37
36
  if node.first_argument.type == :sym
38
37
  corrector.replace(node.first_argument.loc.expression, ":#{factory_name.singularize}")
39
38
  elsif node.first_argument.type == :str
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - supermomonga
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-22 00:00:00.000000000 Z
11
+ date: 2024-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -93,6 +93,7 @@ files:
93
93
  - lib/momocop/version.rb
94
94
  - lib/rubocop/cop/momocop/factory_bot_class_existence.rb
95
95
  - lib/rubocop/cop/momocop/factory_bot_consistent_file_name.rb
96
+ - lib/rubocop/cop/momocop/factory_bot_inline_association.rb
96
97
  - lib/rubocop/cop/momocop/factory_bot_missing_associations.rb
97
98
  - lib/rubocop/cop/momocop/factory_bot_missing_class_option.rb
98
99
  - lib/rubocop/cop/momocop/factory_bot_missing_properties.rb