sevencop 0.40.1 → 0.42.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: b9d291c9bf0ec6bb349a6f7a70ebca387c933ddb9ca4a84bfa34f8906b044d68
4
- data.tar.gz: 3d005e1c0636815cf76667e80866a48f6279bfbbbe251f58920f356728ad96ec
3
+ metadata.gz: 3948e119b6a873e0226f93921268a1120b1c0bd8067c6dd2eedf80cd449db92d
4
+ data.tar.gz: bd864bf33c85003b21ac7a14dd88bfb581f383edc3f1b072d1b59f4b14739e9d
5
5
  SHA512:
6
- metadata.gz: be809d23bb893cbbff03f3bdd6a95f79db7aac41f3920defba5f55393b71f24b44cb0fd178f4e2920e00e94fbc9f724b73d7ee4d76c53bbca43a266cb053e1ca
7
- data.tar.gz: 66342ee1af89275c848a4c8c11ff06337fbfefb53a659c350621cdc5b0bc7fb784a1ee58cf77fcf1556bb2e1b8ca8e7ac896c10a02eee67ab6d3716850004a66
6
+ metadata.gz: c25ad4ee15137b961661fc5ec92a3e930116c53fd19c01d28a5886448668c0307d30448b990367f3890d2bfc2f4c2aaafc4b1e5f9fb8143b8c54822071d5005e
7
+ data.tar.gz: 2815ccf2f38067606967043985d1aa898c3220694a20098fc3e28fdb7ca5faa9456363579a69a5bc7decf4c3e61fe571ea4579f7115bcf71864194636fd5c4e0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.40.1)
4
+ sevencop (0.42.0)
5
5
  activesupport
6
6
  rubocop
7
7
 
data/README.md CHANGED
@@ -29,8 +29,6 @@ Note that all cops are `Enabled: false` by default.
29
29
  ## Cops
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)
34
32
  - [Sevencop/HashElementOrdered](lib/rubocop/cop/sevencop/hash_element_ordered.rb)
35
33
  - [Sevencop/MapMethodChain](lib/rubocop/cop/sevencop/map_method_chain.rb)
36
34
  - [Sevencop/MethodDefinitionArgumentsMultiline](lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb)
@@ -41,6 +39,8 @@ Note that all cops are `Enabled: false` by default.
41
39
  - [Sevencop/RailsDateAndTimeCalculation](lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb)
42
40
  - [Sevencop/RailsOrderFieldArelSql](lib/rubocop/cop/sevencop/rails_order_field_arel_sql.rb)
43
41
  - [Sevencop/RailsOrderFieldInOrderOf](lib/rubocop/cop/sevencop/rails_order_field_in_order_of.rb)
42
+ - [Sevencop/RailsRouteAs](lib/rubocop/cop/sevencop/rails_route_as.rb)
43
+ - [Sevencop/RailsRouteOrdered](lib/rubocop/cop/sevencop/rails_route_ordered.rb)
44
44
  - [Sevencop/RailsSpecificActionName](lib/rubocop/cop/sevencop/rails_specific_action_name.rb)
45
45
  - [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
46
46
  - [Sevencop/RailsWhereNot](lib/rubocop/cop/sevencop/rails_where_not.rb)
data/config/default.yml CHANGED
@@ -4,30 +4,6 @@ Sevencop/AutoloadOrdered:
4
4
  Enabled: false
5
5
  Safe: false
6
6
 
7
- Sevencop/FactoryBotAssociationOption:
8
- Description: |
9
- Avoid redundant options on FactoryBot associations.
10
- Enabled: false
11
-
12
- Sevencop/FactoryBotAssociationStyle:
13
- Description: |
14
- Use consistent style in FactoryBot associations.
15
- Enabled: false
16
- Safe: false
17
- EnforcedStyle: implicit
18
- SupportedStyles:
19
- - explicit
20
- - implicit
21
- inherit_mode:
22
- merge:
23
- - NonImplicitAssociationMethodNames
24
- NonImplicitAssociationMethodNames:
25
- - skip_create
26
- Include:
27
- - "**/factories.rb"
28
- - "**/spec/factories/*.rb"
29
- - "**/test/factories/*.rb"
30
-
31
7
  Sevencop/HashElementOrdered:
32
8
  Description: |
33
9
  Sort Hash elements by key.
@@ -87,6 +63,17 @@ Sevencop/RailsOrderFieldInOrderOf:
87
63
  Enabled: false
88
64
  Safe: false
89
65
 
66
+ Sevencop/RailsRouteAs:
67
+ Description: |
68
+ Always use `as` option on routing methods.
69
+ Enabled: false
70
+
71
+ Sevencop/RailsRouteOrdered:
72
+ Description: |
73
+ Sort routes by path and HTTP method.
74
+ Enabled: false
75
+ Safe: false
76
+
90
77
  Sevencop/RailsSpecificActionName:
91
78
  Description: |
92
79
  Use only specific action names.
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Always use `as` option on routing methods.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # delete "/users/:id" => "users#destroy"
11
+ # get "/users/:id" => "users#show"
12
+ #
13
+ # # good
14
+ # delete "/users/:id" => "users#destroy", as: "user"
15
+ # get "/users/:id" => "users#show", as: nil
16
+ class RailsRouteAs < Base
17
+ MSG = "Always use `as` option on routing methods. Use `as: nil` if you don't need named routes."
18
+
19
+ RESTRICT_ON_SEND = %i[
20
+ delete
21
+ get
22
+ head
23
+ patch
24
+ post
25
+ put
26
+ ].freeze
27
+
28
+ # @param [RuboCop::AST::SendNode] node
29
+ # @return [void]
30
+ def on_send(node)
31
+ last_argument = node.last_argument
32
+ return unless last_argument&.hash_type?
33
+ return if last_argument.pairs.any? { |pair| pair.key.value == :as }
34
+
35
+ add_offense(node)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Sort routes by path and HTTP method.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # get "/users/:id" => "users#show"
11
+ # get "/users" => "users#index"
12
+ #
13
+ # # good
14
+ # get "/users" => "users#index"
15
+ # get "/users/:id" => "users#show"
16
+ #
17
+ # # bad
18
+ # post "/users" => "users#create"
19
+ # get "/users" => "users#index"
20
+ #
21
+ # # good
22
+ # get "/users" => "users#index"
23
+ # post "/users" => "users#create"
24
+ class RailsRouteOrdered < Base
25
+ extend AutoCorrector
26
+
27
+ include RangeHelp
28
+
29
+ MSG = 'Sort routes by path and HTTP method.'
30
+
31
+ RESTRICT_ON_SEND = %i[
32
+ delete
33
+ get
34
+ head
35
+ options
36
+ patch
37
+ post
38
+ put
39
+ ].freeze
40
+
41
+ # @param [RuboCop::AST::SendNode] node
42
+ # @return [void]
43
+ def on_send(node)
44
+ previous_older_sibling = find_previous_older_sibling(node)
45
+ return unless previous_older_sibling
46
+
47
+ add_offense(node) do |corrector|
48
+ corrector.swap(
49
+ range_with_comments_and_lines(previous_older_sibling),
50
+ range_with_comments_and_lines(node)
51
+ )
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # @param [RuboCop::AST::SendNode] node
58
+ # @return [Array<String>]
59
+ def convert_to_comparison_key(node)
60
+ [
61
+ find_path_node(node).source.tr(':', '~'),
62
+ node.method_name
63
+ ]
64
+ end
65
+
66
+ # Find path node from both of the following styles:
67
+ # `get "/users" => "users#index`
68
+ # `get "/users", to: "users#index`
69
+ # @param [RuboCop::AST::SendNode] node
70
+ # @return [RuboCop::AST::Node, nil]
71
+ def find_path_node(node)
72
+ if node.first_argument.hash_type?
73
+ node.first_argument.keys.first
74
+ else
75
+ node.first_argument
76
+ end
77
+ end
78
+
79
+ # @param [RuboCop::AST::SendNode] node
80
+ # @return [RuboCop::AST::SendNode, nil]
81
+ def find_previous_older_sibling(node)
82
+ node.left_siblings.grep(::RuboCop::AST::SendNode).reverse.find do |sibling|
83
+ RESTRICT_ON_SEND.include?(sibling.method_name) &&
84
+ (convert_to_comparison_key(sibling) <=> convert_to_comparison_key(node)).positive?
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.40.1'
4
+ VERSION = '0.42.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -5,8 +5,6 @@ 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'
10
8
  require_relative 'rubocop/cop/sevencop/hash_element_ordered'
11
9
  require_relative 'rubocop/cop/sevencop/map_method_chain'
12
10
  require_relative 'rubocop/cop/sevencop/method_definition_arguments_multiline'
@@ -17,6 +15,8 @@ require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
17
15
  require_relative 'rubocop/cop/sevencop/rails_date_and_time_calculation'
18
16
  require_relative 'rubocop/cop/sevencop/rails_order_field_arel_sql'
19
17
  require_relative 'rubocop/cop/sevencop/rails_order_field_in_order_of'
18
+ require_relative 'rubocop/cop/sevencop/rails_route_as'
19
+ require_relative 'rubocop/cop/sevencop/rails_route_ordered'
20
20
  require_relative 'rubocop/cop/sevencop/rails_specific_action_name'
21
21
  require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
22
22
  require_relative 'rubocop/cop/sevencop/rails_where_not'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.40.1
4
+ version: 0.42.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -57,8 +57,6 @@ files:
57
57
  - config/default.yml
58
58
  - data/reserved_words_mysql.txt
59
59
  - lib/rubocop/cop/sevencop/autoload_ordered.rb
60
- - lib/rubocop/cop/sevencop/factory_bot_association_option.rb
61
- - lib/rubocop/cop/sevencop/factory_bot_association_style.rb
62
60
  - lib/rubocop/cop/sevencop/hash_element_ordered.rb
63
61
  - lib/rubocop/cop/sevencop/map_method_chain.rb
64
62
  - lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb
@@ -69,6 +67,8 @@ files:
69
67
  - lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb
70
68
  - lib/rubocop/cop/sevencop/rails_order_field_arel_sql.rb
71
69
  - lib/rubocop/cop/sevencop/rails_order_field_in_order_of.rb
70
+ - lib/rubocop/cop/sevencop/rails_route_as.rb
71
+ - lib/rubocop/cop/sevencop/rails_route_ordered.rb
72
72
  - lib/rubocop/cop/sevencop/rails_specific_action_name.rb
73
73
  - lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
74
74
  - lib/rubocop/cop/sevencop/rails_where_not.rb
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Sevencop
6
- # Avoid redundant options on 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 = 'Avoid redundant options on 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.source_range,
80
- side: :left
81
- ),
82
- :left
83
- )
84
- )
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,168 +0,0 @@
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
- bad_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::SendNode]
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::SendNode]
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 [Boolean]
116
- def bad?(node)
117
- case style
118
- when :explicit
119
- bad_to_explicit_style?(node)
120
- when :implicit
121
- bad_to_implicit_style?(node)
122
- end
123
- end
124
-
125
- # @param node [RuboCop::AST::SendNode]
126
- # @return [Array<RuboCop::AST::SendNode>]
127
- def bad_associations_in(node)
128
- children_of_factory_block(node).select do |child|
129
- bad?(child)
130
- end
131
- end
132
-
133
- # @param node [RuboCop::AST::SendNode]
134
- # @return [Boolean]
135
- def bad_to_explicit_style?(node)
136
- implicit_association?(node)
137
- end
138
-
139
- # @param node [RuboCop::AST::SendNode]
140
- # @return [Boolean]
141
- def bad_to_implicit_style?(node)
142
- explicit_association?(node) && autocorrectable_to_implicit_style?(node)
143
- end
144
-
145
- # @param node [RuboCop::AST::SendNode]
146
- # @return [Array<RuboCop::AST::Node>]
147
- def children_of_factory_block(node)
148
- block = node.parent
149
- return [] unless block
150
- return [] unless block.block_type?
151
- return [] unless block.body
152
-
153
- if block.body.begin_type?
154
- block.body.children
155
- else
156
- [block.body]
157
- end
158
- end
159
-
160
- # @param method_name [Symbol]
161
- # @return [Boolean]
162
- def non_implicit_association_method_name?(method_name)
163
- cop_config['NonImplicitAssociationMethodNames'].include?(method_name.to_s)
164
- end
165
- end
166
- end
167
- end
168
- end