momocop 0.1.13 → 0.1.14

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: 220069b3119b5c48271dcd6881e4a1530074eabbc697d12dca9ea70cf5dbbbe8
4
- data.tar.gz: 02fcf5cad166113116cb7876aa46f1ffa0de65391488901727286711645a4917
3
+ metadata.gz: 125920382da157b5f151d8d7de60a05df4943fbbdf521db3b082001891f7af25
4
+ data.tar.gz: 8860b388475b79b0eec94ee28e9635a2c288b662bdcdbe33a5172b8959d969c9
5
5
  SHA512:
6
- metadata.gz: 2ae69bdfff4429dc93e5e0a78fa4d87afb29bdf5d3bec1bffc3aec97b569066d47e2f0ba215593d0599cbc781591ea38b44dcf128cffbda503ba4b03ced79224
7
- data.tar.gz: 22b9e10997ec1a2f8f32d33e503285a7f0ca6809d3c7081713b481c8ba245753d8ed944e57d2032f9f3a243894dbd16018f4d8cff7dcb24b55bf3144280d4813
6
+ metadata.gz: f538d288ad3858eb9c5c6bbd0c6576b6e8bd0811282da99215b295426e83b4d51f5b111fa256d1c6246eb1577209a114d8745c28c0568e8fc93b5f774774b93b
7
+ data.tar.gz: e49a4e8e1ab9ad2ce341566257730d7eab510002319a8c4c48db179f2be5dbdfc6ee7ddecec9552debef73360729546e6eb954eee14c2489f8c8037f950ac891
data/README.ja.md CHANGED
@@ -30,6 +30,8 @@ Momocop/FactoryBotClassExistence:
30
30
  Enabled: true
31
31
  Momocop/FactoryBotConsistentFileName:
32
32
  Enabled: true
33
+ Momocop/FactoryBotInlineAssociation:
34
+ Enabled: true
33
35
  Momocop/FactoryBotMissingAssociations:
34
36
  Enabled: true
35
37
  Momocop/FactoryBotMissingClassOption:
@@ -52,6 +54,7 @@ Momocop/FactoryBotSingularFactoryName:
52
54
  |---|:-:|:-:|
53
55
  |[`Momocop/FactoryBotClassExistence`](lib/rubocop/cop/momocop/factory_bot_class_existence.rb)|:white_check_mark:|:white_check_mark:|
54
56
  |[`Momocop/FactoryBotConsistentFileName`](lib/rubocop/cop/momocop/factory_bot_consistent_file_name.rb)|:white_check_mark:|:white_check_mark:|
57
+ |[`Momocop/FactoryBotInlineAssociation`](lib/rubocop/cop/momocop/factory_bot_inline_association.rb)|:white_check_mark:|:white_check_mark:|
55
58
  |[`Momocop/FactoryBotMissingAssociations`](lib/rubocop/cop/momocop/factory_bot_missing_associations.rb)|:white_check_mark:|:white_check_mark:|
56
59
  |[`Momocop/FactoryBotMissingClassOption`](lib/rubocop/cop/momocop/factory_bot_missing_class_option.rb)|:white_check_mark:|:white_check_mark:|
57
60
  |[`Momocop/FactoryBotMissingProperties`](lib/rubocop/cop/momocop/factory_bot_missing_properties.rb)|:white_check_mark:|:white_check_mark:|
data/README.md CHANGED
@@ -30,6 +30,8 @@ Momocop/FactoryBotClassExistence:
30
30
  Enabled: true
31
31
  Momocop/FactoryBotConsistentFileName:
32
32
  Enabled: true
33
+ Momocop/FactoryBotInlineAssociation:
34
+ Enabled: true
33
35
  Momocop/FactoryBotMissingAssociations:
34
36
  Enabled: true
35
37
  Momocop/FactoryBotMissingClassOption:
@@ -52,6 +54,7 @@ Momocop/FactoryBotSingularFactoryName:
52
54
  |---|:-:|:-:|
53
55
  |[`Momocop/FactoryBotClassExistence`](lib/rubocop/cop/momocop/factory_bot_class_existence.rb)|:white_check_mark:|:white_check_mark:|
54
56
  |[`Momocop/FactoryBotConsistentFileName`](lib/rubocop/cop/momocop/factory_bot_consistent_file_name.rb)|:white_check_mark:|:white_check_mark:|
57
+ |[`Momocop/FactoryBotInlineAssociation`](lib/rubocop/cop/momocop/factory_bot_inline_association.rb)|:white_check_mark:|:white_check_mark:|
55
58
  |[`Momocop/FactoryBotMissingAssociations`](lib/rubocop/cop/momocop/factory_bot_missing_associations.rb)|:white_check_mark:|:white_check_mark:|
56
59
  |[`Momocop/FactoryBotMissingClassOption`](lib/rubocop/cop/momocop/factory_bot_missing_class_option.rb)|:white_check_mark:|:white_check_mark:|
57
60
  |[`Momocop/FactoryBotMissingProperties`](lib/rubocop/cop/momocop/factory_bot_missing_properties.rb)|:white_check_mark:|:white_check_mark:|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Momocop
4
- VERSION = '0.1.13'
4
+ VERSION = '0.1.14'
5
5
  end
@@ -32,18 +32,34 @@ module RuboCop
32
32
 
33
33
  add_offense(node.loc.selector, message: MSG) do |corrector|
34
34
  association_name = node.arguments.first.value
35
- options = node.arguments.drop(1).map(&:source).join(', ')
35
+ options = node.arguments.at(1)
36
36
 
37
+ convert_options(options) in {
38
+ factory_option:, rest_options:
39
+ }
40
+ factory = factory_option&.source || ":#{association_name}"
37
41
  replacement =
38
- if options.empty?
39
- "#{association_name} { association :#{association_name} }"
42
+ if rest_options.empty?
43
+ "#{association_name} { association #{factory} }"
40
44
  else
41
- "#{association_name} { association :#{association_name}, #{options} }"
45
+ rest_options_source = rest_options.map(&:source).join(', ')
46
+ "#{association_name} { association #{factory}, #{rest_options_source} }"
42
47
  end
43
48
  corrector.replace(node, replacement)
44
49
  end
45
50
  end
46
51
 
52
+ # `association :foo, factory: :bar, baz: 1 ...` => `foo { association :bar, baz: 1 ...}`
53
+ private def convert_options(options)
54
+ factory_option = options&.pairs&.find { |pair| pair.key.value == :factory }&.value
55
+ rest_options = options&.pairs&.reject { |pair| pair.key.value == :factory } || []
56
+
57
+ return {
58
+ factory_option:,
59
+ rest_options:
60
+ }
61
+ end
62
+
47
63
  private def inside_factory_bot_factory?(node)
48
64
  context = node.each_ancestor(:block).first
49
65
  send_node = context.block_type? ? context.send_node : context
@@ -112,10 +112,27 @@ module RuboCop
112
112
  return [] unless body_node
113
113
 
114
114
  associations = association_definitions(body_node)
115
- association_names = associations&.map(&:first_argument)&.map(&:value)&.map(&:to_s)
115
+ association_names = associations&.map { |node| get_association_name(node) }
116
116
  return association_names
117
117
  end
118
118
 
119
+ private def get_association_name(node)
120
+ # Explicit definition
121
+ return node.arguments.first.value.to_s if inside_factory_bot_factory?(node)
122
+
123
+ # Inline definition
124
+ context = node.each_ancestor(:block).first
125
+ send_node = context.block_type? ? context.send_node : context
126
+ return send_node.method_name.to_s
127
+ end
128
+
129
+ private def inside_factory_bot_factory?(node)
130
+ context = node.each_ancestor(:block).first
131
+ send_node = context.block_type? ? context.send_node : context
132
+
133
+ return send_node.method_name == :factory
134
+ end
135
+
119
136
  private def generate_association_definition(property)
120
137
  "#{property} { association :#{property} }"
121
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - supermomonga