File tree 6 files changed +85
-1
lines changed
6 files changed +85
-1
lines changed Original file line number Diff line number Diff line change 1
1
## [ Unreleased]
2
2
3
+ ### New Features
4
+
5
+ * New cop ` Grape/RouteParamGrouping ` detects route parameter defined on endpoint ([ @kakubin ] [ ] )
6
+
3
7
## [ 0.4.2] - 2024-04-05
4
8
5
9
### Changes
Original file line number Diff line number Diff line change @@ -19,6 +19,9 @@ Grape/FieldName:
19
19
- snake_case
20
20
- camelCase
21
21
22
+ Grape/RouteParamGrouping :
23
+ Enabled : true
24
+
22
25
Grape/RouteParamType :
23
26
Enabled : true
24
27
Safe : false
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 7
7
require_relative 'grape/ivar'
8
8
require_relative 'grape/params_position'
9
9
require_relative 'grape/present_with'
10
+ require_relative 'grape/route_param_grouping'
10
11
require_relative 'grape/route_param_type'
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ module EndpointHelper
7
7
extend NodePattern ::Macros
8
8
9
9
def_node_matcher :http_method_node? , <<~PATTERN
10
- (block (send _ {:get :post :put :patch :delete} ...) ...)
10
+ (block $ (send _ {:get :post :put :patch :delete} ...) ...)
11
11
PATTERN
12
12
13
13
def_node_matcher :params_node? , <<~PATTERN
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments