rubocop-infinum 0.5.1 → 0.6.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: abf5743e5f9cb860ccccd40aa99cc051b583115693c6b4e69be6d39eb0346c72
4
- data.tar.gz: 9d9c6d46dae69c7f86cf1f8b43d0c475691870ac672354b7ebae9171de4a714a
3
+ metadata.gz: eab4ddaf8d28fe1b4549a6cada698ea7a839c65dedd6ada156b7347273783946
4
+ data.tar.gz: 696b1a2a1c359e2da6c4ccc699a7cf1738be0ec21e63bdcdbca36a2c227628bf
5
5
  SHA512:
6
- metadata.gz: 492842a441bb84e5b77fce3b5041dc7b81a529798f68f8ddd07dca6ca3e127475e20a8a6508deafde9a35d0650a630a4b108d3a977fd1e25096fd50462fd4b58
7
- data.tar.gz: 925d8a1e346acd5dd9d710ed0ab28788f83f50b17e4e1caae8ec1bc9765753657f792578604f6336ba6c0f4f97543ee9eccfd0ae6f64d2157084ece54aa95225
6
+ metadata.gz: cc4248284de8b3665408d46d2251c170d5b1285173ac690f90a6df1acec8d8c38dfbee66762681ade893907d49dd34d467666301543f92940ceec832915f35b6
7
+ data.tar.gz: 951a92324c0b959841771b982558b093039e9f633ce2ec1a00788956aa6a86667d149460d4bd42093165755617f3cf33243f3404bd997c9a5f65f52fa60136b4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # RuboCop Infinum Change Log
2
2
 
3
+ ## 0.6.0 (Feb 18th, 2022)
4
+
5
+ - Create FactoryBotAssociation cop (@Benaaaaa)
6
+ - Fix issue with second optional parameter and refactor attribute_default_block_value cop (@Benaaaaa)
3
7
  ## 0.5.1 (Sep 16th, 2021)
4
8
 
5
9
  - Enforce separated accessor grouping (@d4be4st)
@@ -22,21 +22,21 @@ module RuboCop
22
22
  MSG = 'Pass method in a block to `:default` option.'
23
23
 
24
24
  def_node_matcher :default_attribute, <<~PATTERN
25
- (send nil? :attribute _ _ (hash <$#attribute ...>))
25
+ (send nil? :attribute _ ?_ (hash <$#attribute ...>))
26
26
  PATTERN
27
27
 
28
28
  def_node_matcher :attribute, '(pair (sym :default) $_)'
29
29
 
30
30
  def on_send(node)
31
- default_attribute(node) do |attribute|
32
- value = attribute.children.last
31
+ default_attribute(node) do |hash_pair|
32
+ value = attribute(hash_pair)
33
33
 
34
34
  add_offense(node, location: value) if value.send_type?
35
35
  end
36
36
  end
37
37
 
38
38
  def autocorrect(node)
39
- expression = default_attribute(node).children.last
39
+ expression = attribute(default_attribute(node))
40
40
 
41
41
  lambda do |corrector|
42
42
  corrector.replace(expression, "-> { #{expression.source} }")
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Infinum
8
+ # This cop looks for 'association' in factories that specify a ':factory' option
9
+ # and tells you to use explicit build reference which improves the performance
10
+ # as cascading factories will not be saved unless needed to
11
+ #
12
+ # @example
13
+ # #bad
14
+ # FactoryBot.define do
15
+ # factory :book do
16
+ # title { 'Lord of the Rings' }
17
+ # association :author
18
+ # end
19
+ # end
20
+ #
21
+ # FactoryBoy.define do
22
+ # factory :book do
23
+ # title {'Lord of the Rings'}
24
+ # author { association :author }
25
+ # end
26
+ # end
27
+ #
28
+ # #good
29
+ # FactoryBot.define do
30
+ # factory :book do
31
+ # title { 'Lord of the Rings' }
32
+ # author { build(:author) }
33
+ # end
34
+ # end
35
+ #
36
+ # FactoryBot.define do
37
+ # factory :author do
38
+ # name { 'J. R. R. Tolkien' }
39
+ # end
40
+ # end
41
+ class FactoryBotAssociation < ::RuboCop::Cop::Cop
42
+ MSG = 'Use %<association_name>s { build(:%<factory_name>s) } instead'
43
+
44
+ def_node_matcher :association_definition, <<~PATTERN
45
+ (send nil? :association (:sym $_) (hash (pair (sym :factory) (:sym $_))) ?)
46
+ PATTERN
47
+
48
+ def_node_matcher :inline_association_definition, <<~PATTERN
49
+ (block (send nil? $_) (args) (send nil? :association (sym $_)))
50
+ PATTERN
51
+
52
+ def on_block(node)
53
+ inline_association_definition(node) do |association_name, factory_name|
54
+ message = format(MSG, association_name: association_name.to_s, factory_name: factory_name.to_s)
55
+
56
+ add_offense(node, location: node, message: message)
57
+ end
58
+ end
59
+
60
+ def on_send(node)
61
+ return unless corrections.empty?
62
+
63
+ association_definition(node) do |association_name, factory_name|
64
+ factory_name = [association_name] if factory_name.empty?
65
+
66
+ message = format(MSG, association_name: association_name.to_s, factory_name: factory_name.first.to_s)
67
+
68
+ add_offense(node, location: node, message: message)
69
+ end
70
+ end
71
+
72
+ def autocorrect(node)
73
+ lambda do |corrector|
74
+ if expression(node).size == 1
75
+ corrector.replace(node, "#{expression(node)[0]} { build(:#{expression(node)[0]}) }")
76
+ else
77
+ corrector.replace(node, "#{expression(node)[0]} { build(:#{expression(node)[1]}) }")
78
+ end
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def expression(node)
85
+ @expression = if node.block_type?
86
+ inline_association_definition(node)
87
+ else
88
+ association_definition(node).flatten
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'infinum/attribute_default_block_value'
4
+ require_relative 'infinum/factory_bot_association'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Infinum
5
- VERSION = '0.5.1'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  end
@@ -5,7 +5,7 @@ require_relative 'lib/rubocop/infinum/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'rubocop-infinum'
7
7
  spec.version = RuboCop::Infinum::VERSION
8
- spec.authors = ['Marko Ćilimković', 'Stjepan Hađić']
8
+ spec.authors = ['Marko Ćilimković', 'Stjepan Hađić', 'Tin Benaković']
9
9
  spec.email = ['team.backend@infinum.com']
10
10
 
11
11
  spec.summary = 'Automatic Infinum code style checking tool.'
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-infinum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marko Ćilimković
8
8
  - Stjepan Hađić
9
- autorequire:
9
+ - Tin Benaković
10
+ autorequire:
10
11
  bindir: exe
11
12
  cert_chain: []
12
- date: 2021-09-16 00:00:00.000000000 Z
13
+ date: 2022-02-18 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: pry-byebug
@@ -95,7 +96,7 @@ dependencies:
95
96
  - - ">="
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
- description:
99
+ description:
99
100
  email:
100
101
  - team.backend@infinum.com
101
102
  executables: []
@@ -113,6 +114,7 @@ files:
113
114
  - bin/setup
114
115
  - lib/rubocop-infinum.rb
115
116
  - lib/rubocop/cop/infinum/attribute_default_block_value.rb
117
+ - lib/rubocop/cop/infinum/factory_bot_association.rb
116
118
  - lib/rubocop/cop/infinum_cops.rb
117
119
  - lib/rubocop/infinum.rb
118
120
  - lib/rubocop/infinum/version.rb
@@ -125,7 +127,7 @@ metadata:
125
127
  allowed_push_host: https://rubygems.org
126
128
  homepage_uri: https://github.com/infinum/rubocop-infinum
127
129
  source_code_uri: https://github.com/infinum/rubocop-infinum
128
- post_install_message:
130
+ post_install_message:
129
131
  rdoc_options: []
130
132
  require_paths:
131
133
  - lib
@@ -140,9 +142,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
142
  - !ruby/object:Gem::Version
141
143
  version: '0'
142
144
  requirements: []
143
- rubyforge_project:
145
+ rubyforge_project:
144
146
  rubygems_version: 2.7.3
145
- signing_key:
147
+ signing_key:
146
148
  specification_version: 4
147
149
  summary: Automatic Infinum code style checking tool.
148
150
  test_files: []