sevencop 0.20.1 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22e21f2ba7c484fc1fd0d23b9b7d4dcf70d3e0f2f5f764e95e6dc65d10c419e9
4
- data.tar.gz: b689a364af2f7728c3947575d4edcc5f79a9ce3767fa769acf4829587fa9e63c
3
+ metadata.gz: 97218861b57331e3ffaa577f6a7a7a7d9ec05d0b7f6d7a8034e41ab831854a5d
4
+ data.tar.gz: 076a2bb553cd96314b4c10144e943cd67a3c8bee532950d9945d54a8cbad96ad
5
5
  SHA512:
6
- metadata.gz: 9cab7cab01a11e6c9ea1de15a97ae4ea6c8315dc4c4a2304d4ee1178775dd8526cccd2a42cdca9d30069936e816bde32cd58451e157fa85b737bb2bfa5368465
7
- data.tar.gz: 0040a81647501bcb03ee0f0bd0f97722b2483c202751debc7a530d58677a4d822e73039803a6066d1d6085e2dcd7c9a06fead8c06991e2b14afdc927ebd8b07b
6
+ metadata.gz: 7dbc8187110fae79a6c21c61e2292d7f6e6d42a0f51bdf35066d281a2b44fa47e1b6a2fcc920fa192cda30dfd9c77b73836e57a518d61e5de1a7ff6fc97c3576
7
+ data.tar.gz: 0ad3d66a1ecbbec56054a771626e8cb22eb2bc425501fbe0b6f38290be315110de2875a73670bfead59e3cd854d41214f919178622e0807a07c91b2dac8a283e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.20.1)
4
+ sevencop (0.21.0)
5
5
  rubocop
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -29,6 +29,8 @@ Sevencop/MethodDefinitionOrdered:
29
29
  Choose the cops you want to use and enable them on your .rubocop.yml.
30
30
 
31
31
  - [Sevencop/AutoloadOrdered](lib/rubocop/cop/sevencop/autoload_ordered.rb)
32
+ - [Sevencop/FactoryBotAssociationOption](lib/rubocop/cop/sevencop/factory_bot_association_option.rb)
33
+ - [Sevencop/FactoryBotAssociationStyle](lib/rubocop/cop/sevencop/factory_bot_association_style.rb)
32
34
  - [Sevencop/HashElementOrdered](lib/rubocop/cop/sevencop/hash_element_ordered.rb)
33
35
  - [Sevencop/MethodDefinitionArgumentsMultiline](lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb)
34
36
  - [Sevencop/MethodDefinitionInIncluded](lib/rubocop/cop/sevencop/method_definition_in_included.rb)
data/config/default.yml CHANGED
@@ -5,6 +5,35 @@ Sevencop/AutoloadOrdered:
5
5
  Safe: false
6
6
  VersionAdded: '0.12'
7
7
 
8
+ Sevencop/FactoryBotAssociationOptions:
9
+ Description: |
10
+ Remove redundant options from FactoryBot associations.
11
+ Enabled: false
12
+ VersionAdded: '0.21'
13
+
14
+ Sevencop/FactoryBotAssociationStyle:
15
+ Description: |
16
+ Use consistent style in FactoryBot associations.
17
+ Enabled: false
18
+ Safe: false
19
+ VersionAdded: '0.21'
20
+ EnforcedStyle: implicit
21
+ SupportedStyles:
22
+ - explicit
23
+ - implicit
24
+ inherit_mode:
25
+ merge:
26
+ - NonImplicitAssociationMethodNames
27
+ NonImplicitAssociationMethodNames:
28
+ - skip_create
29
+ Include:
30
+ - factories.rb
31
+ - factories/*.rb
32
+ - spec/factories.rb
33
+ - spec/factories/*.rb
34
+ - test/factories.rb
35
+ - test/factories/*.rb
36
+
8
37
  Sevencop/HashElementOrdered:
9
38
  Description: |
10
39
  Sort Hash elements by key.
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Remove redundant options from FactoryBot associations.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # association :user, factory: :user
11
+ #
12
+ # # good
13
+ # association :user
14
+ class FactoryBotAssociationOption < RuboCop::Cop::Base
15
+ extend AutoCorrector
16
+
17
+ include RangeHelp
18
+
19
+ MSG = 'Remove redundant options from FactoryBot associations.'
20
+
21
+ RESTRICT_ON_SEND = %i[association].freeze
22
+
23
+ # @param node [RuboCop::AST::SendNode]
24
+ # @return [void]
25
+ def on_send(node)
26
+ association_name = association_name_from(node)
27
+ factory_option = factory_option_from(node)
28
+ return if !factory_option || association_name != factory_option.value.value
29
+
30
+ add_offense(factory_option) do |corrector|
31
+ autocorrect(corrector, factory_option)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ # @!method association_name_from(node)
38
+ # @param node [RuboCop::AST::SendNode]
39
+ # @return [Symbol, nil]
40
+ def_node_matcher :association_name_from, <<~PATTERN
41
+ (send
42
+ nil?
43
+ _
44
+ (sym $_)
45
+ ...
46
+ )
47
+ PATTERN
48
+
49
+ # @!method factory_option_from(node)
50
+ # @param node [RuboCop::AST::SendNode]
51
+ # @return [RuboCop::AST::PairNode, nil]
52
+ def_node_matcher :factory_option_from, <<~PATTERN
53
+ (send
54
+ nil?
55
+ _
56
+ _
57
+ (hash
58
+ <
59
+ $(pair
60
+ (sym :factory)
61
+ (sym _)
62
+ )
63
+ ...
64
+ >
65
+ )
66
+ )
67
+ PATTERN
68
+
69
+ # @param corrector [RuboCop::Cop::Corrector]
70
+ # @param node [RuboCop::AST::PairNode]
71
+ # @return [void]
72
+ def autocorrect(
73
+ corrector,
74
+ node
75
+ )
76
+ corrector.remove(
77
+ range_with_surrounding_comma(
78
+ range_with_surrounding_space(
79
+ node.location.expression,
80
+ side: :left
81
+ ),
82
+ :left
83
+ )
84
+ )
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Use consistent style in FactoryBot associations.
7
+ #
8
+ # @safety
9
+ # This cop may cause false-positives in `EnforcedStyle: explicit` case.
10
+ # It recognizes any method call that has no arguments as an implicit association
11
+ # but it might be a user-defined trait call.
12
+ #
13
+ # @example EnforcedStyle: implicit (default)
14
+ # # bad
15
+ # association :user
16
+ #
17
+ # # good
18
+ # user
19
+ #
20
+ # # good
21
+ # association :author, factory: user
22
+ #
23
+ # @example EnforcedStyle: explicit
24
+ # # bad
25
+ # user
26
+ #
27
+ # # good
28
+ # association :user
29
+ #
30
+ # # good (defined in NonImplicitAssociationMethodNames)
31
+ # skip_create
32
+ class FactoryBotAssociationStyle < RuboCop::Cop::Base
33
+ extend AutoCorrector
34
+
35
+ include ConfigurableEnforcedStyle
36
+
37
+ MSG = 'Use consistent style in FactoryBot associations.'
38
+
39
+ RESTRICT_ON_SEND = %i[factory].freeze
40
+
41
+ # @param node [RuboCop::AST::SendNode]
42
+ # @return [void]
43
+ def on_send(node)
44
+ wrong_associations_in(node).each do |association|
45
+ add_offense(association) do |corrector|
46
+ autocorrect(corrector, association)
47
+ end
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ # @!method explicit_association?(node)
54
+ # @param node [RuboCop::AST::Node]
55
+ # @return [Boolean]
56
+ def_node_matcher :explicit_association?, <<~PATTERN
57
+ (send
58
+ nil?
59
+ :association
60
+ ...
61
+ )
62
+ PATTERN
63
+
64
+ # @!method implicit_association?(node)
65
+ # @param node [RuboCop::AST::Node]
66
+ # @return [Boolean]
67
+ def_node_matcher :implicit_association?, <<~PATTERN
68
+ (send
69
+ nil?
70
+ !#non_implicit_association_method_name?
71
+ )
72
+ PATTERN
73
+
74
+ # @param corrector [RuboCop::Cop::Corrector]
75
+ # @param node [RuboCop::AST::SendNode]
76
+ def autocorrect(
77
+ corrector,
78
+ node
79
+ )
80
+ case style
81
+ when :explicit
82
+ autocorrect_to_explicit_style(corrector, node)
83
+ when :implicit
84
+ autocorrect_to_implicit_style(corrector, node)
85
+ end
86
+ end
87
+
88
+ # @param corrector [RuboCop::Cop::Corrector]
89
+ # @param node [RuboCop::AST::SendNode]
90
+ # @return [void]
91
+ def autocorrect_to_explicit_style(
92
+ corrector,
93
+ node
94
+ )
95
+ corrector.replace(node, "association :#{node.method_name}")
96
+ end
97
+
98
+ # @param corrector [RuboCop::Cop::Corrector]
99
+ # @param node [RuboCop::AST::SendNode]
100
+ # @return [void]
101
+ def autocorrect_to_implicit_style(
102
+ corrector,
103
+ node
104
+ )
105
+ corrector.replace(node, node.first_argument.value.to_s)
106
+ end
107
+
108
+ # @param node [RuboCop::AST::SendNode]
109
+ # @return [Boolean]
110
+ def autocorrectable_to_implicit_style?(node)
111
+ node.arguments.one?
112
+ end
113
+
114
+ # @param node [RuboCop::AST::SendNode]
115
+ # @return [Array<RuboCop::AST::Node>]
116
+ def children_of_factory_block(node)
117
+ block = node.parent
118
+ return [] unless block
119
+ return [] unless block.block_type?
120
+
121
+ if block.body.begin_type?
122
+ block.body.children
123
+ else
124
+ [block.body]
125
+ end
126
+ end
127
+
128
+ # @param method_name [Symbol]
129
+ # @return [Boolean]
130
+ def non_implicit_association_method_name?(method_name)
131
+ cop_config['NonImplicitAssociationMethodNames'].include?(method_name.to_s)
132
+ end
133
+
134
+ # @param node [RuboCop::AST::SendNode]
135
+ # @return [Boolean]
136
+ def wrong?(node)
137
+ case style
138
+ when :explicit
139
+ wrong_to_explicit_style?(node)
140
+ when :implicit
141
+ wrong_to_implicit_style?(node)
142
+ end
143
+ end
144
+
145
+ # @param node [RuboCop::AST::SendNode]
146
+ # @return [Array<RuboCop::AST::SendNode>]
147
+ def wrong_associations_in(node)
148
+ children_of_factory_block(node).select do |child|
149
+ wrong?(child)
150
+ end
151
+ end
152
+
153
+ # @param node [RuboCop::AST::Node]
154
+ # @return [Boolean]
155
+ def wrong_to_explicit_style?(node)
156
+ implicit_association?(node)
157
+ end
158
+
159
+ # @param node [RuboCop::AST::SendNode]
160
+ # @return [Boolean]
161
+ def wrong_to_implicit_style?(node)
162
+ explicit_association?(node) && autocorrectable_to_implicit_style?(node)
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.20.1'
4
+ VERSION = '0.21.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -5,6 +5,8 @@ require_relative 'sevencop/rubocop_extension'
5
5
  require_relative 'sevencop/version'
6
6
 
7
7
  require_relative 'rubocop/cop/sevencop/autoload_ordered'
8
+ require_relative 'rubocop/cop/sevencop/factory_bot_association_option'
9
+ require_relative 'rubocop/cop/sevencop/factory_bot_association_style'
8
10
  require_relative 'rubocop/cop/sevencop/hash_element_ordered'
9
11
  require_relative 'rubocop/cop/sevencop/method_definition_arguments_multiline'
10
12
  require_relative 'rubocop/cop/sevencop/method_definition_in_included'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.1
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-13 00:00:00.000000000 Z
11
+ date: 2022-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -43,6 +43,8 @@ files:
43
43
  - config/default.yml
44
44
  - data/reserved_words_mysql.txt
45
45
  - lib/rubocop/cop/sevencop/autoload_ordered.rb
46
+ - lib/rubocop/cop/sevencop/factory_bot_association_option.rb
47
+ - lib/rubocop/cop/sevencop/factory_bot_association_style.rb
46
48
  - lib/rubocop/cop/sevencop/hash_element_ordered.rb
47
49
  - lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb
48
50
  - lib/rubocop/cop/sevencop/method_definition_in_included.rb