Skip to content

Commit 70242f4

Browse files
committed
Add new cop Grape/RouteParamGrouping
1 parent d9254de commit 70242f4

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## [Unreleased]
22

3+
### New Features
4+
5+
* New cop `Grape/RouteParamGrouping` detects route parameter defined on endpoint ([@kakubin][])
6+
37
## [0.4.2] - 2024-04-05
48

59
### Changes

config/default.yml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Grape/FieldName:
1919
- snake_case
2020
- camelCase
2121

22+
Grape/RouteParamGrouping:
23+
Enabled: true
24+
2225
Grape/RouteParamType:
2326
Enabled: true
2427
Safe: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Grape
6+
#
7+
# @example
8+
#
9+
# # bad
10+
# get ':id' do
11+
# end
12+
#
13+
# # good
14+
# route_param :id, type: Integer do
15+
# get do
16+
# end
17+
# end
18+
#
19+
class RouteParamGrouping < Base
20+
include EndpointHelper
21+
22+
MSG = 'Use route_param for route parameter'
23+
24+
def on_block(node)
25+
return unless (method_send_node = http_method_node?(node))
26+
return unless (first_argument_node = method_send_node.arguments.first)
27+
return unless first_argument_node.value.match?(/:\w/)
28+
29+
add_offense(first_argument_node)
30+
end
31+
end
32+
end
33+
end
34+
end

lib/rubocop/cop/grape_cops.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
require_relative 'grape/ivar'
88
require_relative 'grape/params_position'
99
require_relative 'grape/present_with'
10+
require_relative 'grape/route_param_grouping'
1011
require_relative 'grape/route_param_type'

lib/rubocop/cop/mixin/endpoint_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module EndpointHelper
77
extend NodePattern::Macros
88

99
def_node_matcher :http_method_node?, <<~PATTERN
10-
(block (send _ {:get :post :put :patch :delete} ...) ...)
10+
(block $(send _ {:get :post :put :patch :delete} ...) ...)
1111
PATTERN
1212

1313
def_node_matcher :params_node?, <<~PATTERN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Grape::RouteParamGrouping, :config do
4+
it 'no offense reported' do
5+
expect_no_offenses(<<~RUBY)
6+
route_param :id, type: Integer do
7+
get do
8+
end
9+
end
10+
RUBY
11+
end
12+
13+
context 'without other options' do
14+
it 'offense detected' do
15+
expect_offense(<<~RUBY)
16+
get ':id' do
17+
^^^^^ Use route_param for route parameters
18+
end
19+
RUBY
20+
end
21+
end
22+
23+
context 'with other options' do
24+
it 'offense detected' do
25+
expect_offense(<<~RUBY)
26+
get ':id', requirements: { id: /[0-9]*/ } do
27+
^^^^^ Use route_param for route parameters
28+
end
29+
RUBY
30+
end
31+
end
32+
33+
context 'ss' do
34+
it 'offense detected' do
35+
expect_offense(<<~RUBY)
36+
get 'books/:id' do
37+
^^^^^^^^^^^ Use route_param for route parameters
38+
end
39+
RUBY
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)