Skip to content

Commit

Permalink
Merge pull request #203 from r7kamura/rails-route-as
Browse files Browse the repository at this point in the history
Add `Sevencop/RailsRouteAs` cop
  • Loading branch information
r7kamura committed May 13, 2024
2 parents edcde6d + db0a74e commit 7782bcb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Note that all cops are `Enabled: false` by default.
- [Sevencop/RailsDateAndTimeCalculation](lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb)
- [Sevencop/RailsOrderFieldArelSql](lib/rubocop/cop/sevencop/rails_order_field_arel_sql.rb)
- [Sevencop/RailsOrderFieldInOrderOf](lib/rubocop/cop/sevencop/rails_order_field_in_order_of.rb)
- [Sevencop/RailsRouteAs](lib/rubocop/cop/sevencop/rails_route_as.rb)
- [Sevencop/RailsRouteOrdered](lib/rubocop/cop/sevencop/rails_route_ordered.rb)
- [Sevencop/RailsSpecificActionName](lib/rubocop/cop/sevencop/rails_specific_action_name.rb)
- [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
Expand Down
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Sevencop/RailsOrderFieldInOrderOf:
Enabled: false
Safe: false

Sevencop/RailsRouteAs:
Description: |
Always use `as` option on routing methods.
Enabled: false

Sevencop/RailsRouteOrdered:
Description: |
Sort routes by path and HTTP method.
Expand Down
40 changes: 40 additions & 0 deletions lib/rubocop/cop/sevencop/rails_route_as.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Sevencop
# Always use `as` option on routing methods.
#
# @example
# # bad
# delete "/users/:id" => "users#destroy"
# get "/users/:id" => "users#show"
#
# # good
# delete "/users/:id" => "users#destroy", as: "user"
# get "/users/:id" => "users#show", as: nil
class RailsRouteAs < Base
MSG = "Always use `as` option on routing methods. Use `as: nil` if you don't need named routes."

RESTRICT_ON_SEND = %i[
delete
get
head
patch
post
put
].freeze

# @param [RuboCop::AST::SendNode] node
# @return [void]
def on_send(node)
last_argument = node.last_argument
return unless last_argument&.hash_type?
return if last_argument.pairs.any? { |pair| pair.key.value == :as }

add_offense(node)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/sevencop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require_relative 'rubocop/cop/sevencop/rails_date_and_time_calculation'
require_relative 'rubocop/cop/sevencop/rails_order_field_arel_sql'
require_relative 'rubocop/cop/sevencop/rails_order_field_in_order_of'
require_relative 'rubocop/cop/sevencop/rails_route_as'
require_relative 'rubocop/cop/sevencop/rails_route_ordered'
require_relative 'rubocop/cop/sevencop/rails_specific_action_name'
require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/sevencop/rails_route_as_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RuboCop::Cop::Sevencop::RailsRouteAs, :config do
context 'when `as` option is used' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
get "/users" => "users#index", as: "users"
RUBY
end
end

context 'when `as` option is not used' do
it 'registers offense' do
expect_offense(<<~RUBY)
get "/users" => "users#index"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Always use `as` option on routing methods. Use `as: nil` if you don't need named routes.
RUBY
end
end
end

0 comments on commit 7782bcb

Please sign in to comment.