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 +4 -4
- data/lib/momocop/version.rb +1 -1
- data/lib/rubocop/cop/momocop/factory_bot_inline_association.rb +61 -0
- data/lib/rubocop/cop/momocop/factory_bot_missing_associations.rb +1 -1
- data/lib/rubocop/cop/momocop/factory_bot_property_order.rb +9 -0
- data/lib/rubocop/cop/momocop/factory_bot_singular_factory_name.rb +0 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 220069b3119b5c48271dcd6881e4a1530074eabbc697d12dca9ea70cf5dbbbe8
|
4
|
+
data.tar.gz: 02fcf5cad166113116cb7876aa46f1ffa0de65391488901727286711645a4917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ae69bdfff4429dc93e5e0a78fa4d87afb29bdf5d3bec1bffc3aec97b569066d47e2f0ba215593d0599cbc781591ea38b44dcf128cffbda503ba4b03ced79224
|
7
|
+
data.tar.gz: 22b9e10997ec1a2f8f32d33e503285a7f0ca6809d3c7081713b481c8ba245753d8ed944e57d2032f9f3a243894dbd16018f4d8cff7dcb24b55bf3144280d4813
|
data/lib/momocop/version.rb
CHANGED
@@ -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
|
@@ -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.
|
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-
|
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
|