sevencop 0.41.0 → 0.42.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/config/default.yml +15 -0
- data/lib/rubocop/cop/sevencop/rails_route_as.rb +40 -0
- data/lib/rubocop/cop/sevencop/rails_route_ordered.rb +90 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6458ef2aecd8e03654e70a76fc22ff59d2dd509f1eb377f65977b363dd3d43c
|
4
|
+
data.tar.gz: 8566e27a40c3eb6bd51ae2afa1f592758641a18019492d599cf7c95e811465b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca075075040e44c561fed97593c321375f831a5d034deb7a4c7e1510efbe70d2623196d901546123c453abce3a8c3aa3bc198041cd85d9ff8d671c1ff03dceea
|
7
|
+
data.tar.gz: b38e408140a08a3a173c8700ecc6006950666a9f0d77fd9858e8110fc7676bfa9878c07d6bbfdba40fba039b35d9cf5d001704af77a646e16d91e79ae44dc9a2
|
data/Gemfile.lock
CHANGED
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,21 @@ 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
|
+
Include:
|
71
|
+
- config/routes.rb
|
72
|
+
|
73
|
+
Sevencop/RailsRouteOrdered:
|
74
|
+
Description: |
|
75
|
+
Sort routes by path and HTTP method.
|
76
|
+
Enabled: false
|
77
|
+
Safe: false
|
78
|
+
Include:
|
79
|
+
- config/routes.rb
|
80
|
+
|
66
81
|
Sevencop/RailsSpecificActionName:
|
67
82
|
Description: |
|
68
83
|
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
|
data/lib/sevencop/version.rb
CHANGED
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.
|
4
|
+
version: 0.42.1
|
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
|