-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1329](https://github.com/rubocop/rubocop-rails/pull/1329): Add `Rails/HashKeyValueRoute` cop. ([@r7kamura][]) |
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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Avoid Hash key-value routing. | ||
# Drawing a route in this style will be removed in Rails 8.1. | ||
# | ||
# @safety | ||
# This cop is unsafe because it is implemented on the assumption that | ||
# the first element of Hash always contains path and endpoint, based on | ||
# the background that it is often so written. | ||
# | ||
# @example | ||
# # bad | ||
# get '/users' => 'users#index' | ||
# | ||
# # good | ||
# get '/users', to: 'users#index' | ||
# | ||
# # bad | ||
# mount MyApp => '/my_app' | ||
# | ||
# # good | ||
# mount MyApp, at: '/my_app' | ||
class HashKeyValueRoute < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Avoid Hash key-value routing.' | ||
|
||
RESTRICT_ON_SEND = %i[delete get match mount options patch post put].freeze | ||
|
||
# @!method hash_key_value_route(node) | ||
def_node_matcher :hash_key_value_route, <<~PATTERN | ||
(send | ||
nil? | ||
_ | ||
(hash | ||
$(pair $_ $_) | ||
... | ||
) | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
hash_key_value_route(node) do |pair, key, value| | ||
add_offense(pair) do |corrector| | ||
corrector.replace(pair, "#{key.source}, #{option_name(node)}: #{value.source}") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def option_name(node) | ||
if node.method?(:mount) | ||
'at' | ||
else | ||
'to' | ||
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::HashKeyValueRoute, :config do | ||
context 'when using hash key-value route' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
get '/foos' => 'foos#index' | ||
^^^^^^^^^^^^^^^^^^^^^^^ Avoid Hash key-value routing. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
get '/foos', to: 'foos#index' | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with other options' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
get '/foos' => 'foos#index', as: 'foos' | ||
^^^^^^^^^^^^^^^^^^^^^^^ Avoid Hash key-value routing. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
get '/foos', to: 'foos#index', as: 'foos' | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `mount`' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
mount MyApp => '/my_app' | ||
^^^^^^^^^^^^^^^^^^ Avoid Hash key-value routing. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
mount MyApp, at: '/my_app' | ||
RUBY | ||
end | ||
end | ||
|
||
context 'without hash key-value route' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
get '/foos', to: 'foos#index' | ||
RUBY | ||
end | ||
end | ||
end |