sevencop 0.41.0 → 0.42.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: fb5397adad61614b94d14106b30c1ca48a436ca36fa4154986e0ff37127e5c54
4
- data.tar.gz: ef34c4ac93d16b2396abd8a61947c5fa317e0b0639bbb40ee61d186d3fa90f83
3
+ metadata.gz: 3948e119b6a873e0226f93921268a1120b1c0bd8067c6dd2eedf80cd449db92d
4
+ data.tar.gz: bd864bf33c85003b21ac7a14dd88bfb581f383edc3f1b072d1b59f4b14739e9d
5
5
  SHA512:
6
- metadata.gz: a602e9e8e23e56a86f99b5cededd1fa3e8cdd002fba5a5bb0de3534fd5fc219d5c793dc61024a4d4ab8d45fbf45c645f197b24da9a58a4eb97a97b0c8947921e
7
- data.tar.gz: 3b8868e249ab3e426b691a1ad287e1803f1ce854925d4b195e31bf20f49cb2a135e425579671a374ced892ee653ae0b3e7af156451553ec5d23439cb8cc160e8
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.41.0)
4
+ sevencop (0.42.0)
5
5
  activesupport
6
6
  rubocop
7
7
 
data/README.md CHANGED
@@ -39,6 +39,8 @@ Note that all cops are `Enabled: false` by default.
39
39
  - [Sevencop/RailsDateAndTimeCalculation](lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb)
40
40
  - [Sevencop/RailsOrderFieldArelSql](lib/rubocop/cop/sevencop/rails_order_field_arel_sql.rb)
41
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)
42
44
  - [Sevencop/RailsSpecificActionName](lib/rubocop/cop/sevencop/rails_specific_action_name.rb)
43
45
  - [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
44
46
  - [Sevencop/RailsWhereNot](lib/rubocop/cop/sevencop/rails_where_not.rb)
data/config/default.yml CHANGED
@@ -63,6 +63,17 @@ Sevencop/RailsOrderFieldInOrderOf:
63
63
  Enabled: false
64
64
  Safe: false
65
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
+
66
77
  Sevencop/RailsSpecificActionName:
67
78
  Description: |
68
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.41.0'
4
+ VERSION = '0.42.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -15,6 +15,8 @@ require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
15
15
  require_relative 'rubocop/cop/sevencop/rails_date_and_time_calculation'
16
16
  require_relative 'rubocop/cop/sevencop/rails_order_field_arel_sql'
17
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'
18
20
  require_relative 'rubocop/cop/sevencop/rails_specific_action_name'
19
21
  require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
20
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.41.0
4
+ version: 0.42.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -67,6 +67,8 @@ files:
67
67
  - lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb
68
68
  - lib/rubocop/cop/sevencop/rails_order_field_arel_sql.rb
69
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
70
72
  - lib/rubocop/cop/sevencop/rails_specific_action_name.rb
71
73
  - lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
72
74
  - lib/rubocop/cop/sevencop/rails_where_not.rb