-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from r7kamura/rails-route-ordered
Add `Sevencop/RailsRouteOrdered` cop
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sevencop | ||
# Sort routes by path and HTTP method. | ||
# | ||
# @example | ||
# # bad | ||
# get "/users/:id" => "users#show" | ||
# get "/users" => "users#index" | ||
# | ||
# # good | ||
# get "/users" => "users#index" | ||
# get "/users/:id" => "users#show" | ||
# | ||
# # bad | ||
# post "/users" => "users#create" | ||
# get "/users" => "users#index" | ||
# | ||
# # good | ||
# get "/users" => "users#index" | ||
# post "/users" => "users#create" | ||
class RailsRouteOrdered < Base | ||
extend AutoCorrector | ||
|
||
include RangeHelp | ||
|
||
MSG = 'Sort routes by path and HTTP method.' | ||
|
||
RESTRICT_ON_SEND = %i[ | ||
delete | ||
get | ||
head | ||
options | ||
patch | ||
post | ||
put | ||
].freeze | ||
|
||
# @param [RuboCop::AST::SendNode] node | ||
# @return [void] | ||
def on_send(node) | ||
previous_older_sibling = find_previous_older_sibling(node) | ||
return unless previous_older_sibling | ||
|
||
add_offense(node) do |corrector| | ||
corrector.swap( | ||
range_with_comments_and_lines(previous_older_sibling), | ||
range_with_comments_and_lines(node) | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @param [RuboCop::AST::SendNode] node | ||
# @return [Array<String>] | ||
def convert_to_comparison_key(node) | ||
[ | ||
find_path_node(node).source.tr(':', '~'), | ||
node.method_name | ||
] | ||
end | ||
|
||
# Find path node from both of the following styles: | ||
# `get "/users" => "users#index` | ||
# `get "/users", to: "users#index` | ||
# @param [RuboCop::AST::SendNode] node | ||
# @return [RuboCop::AST::Node, nil] | ||
def find_path_node(node) | ||
if node.first_argument.hash_type? | ||
node.first_argument.keys.first | ||
else | ||
node.first_argument | ||
end | ||
end | ||
|
||
# @param [RuboCop::AST::SendNode] node | ||
# @return [RuboCop::AST::SendNode, nil] | ||
def find_previous_older_sibling(node) | ||
node.left_siblings.grep(::RuboCop::AST::SendNode).reverse.find do |sibling| | ||
RESTRICT_ON_SEND.include?(sibling.method_name) && | ||
(convert_to_comparison_key(sibling) <=> convert_to_comparison_key(node)).positive? | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe RuboCop::Cop::Sevencop::RailsRouteOrdered, :config do | ||
context 'when routes are ordered' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
get "/users" => "users#index" | ||
get "/users/:id" => "users#show" | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when routes are not ordered by path' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
get "/users/:id" => "users#show" | ||
get "/users" => "users#index" | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort routes by path and HTTP method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
get "/users" => "users#index" | ||
get "/users/:id" => "users#show" | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when routes are not ordered by HTTP method' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
post "/users" => "users#create" | ||
get "/users" => "users#index" | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort routes by path and HTTP method. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
get "/users" => "users#index" | ||
post "/users" => "users#create" | ||
RUBY | ||
end | ||
end | ||
end |