File tree 4 files changed +62
-0
lines changed
4 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -565,6 +565,14 @@ Rails/HttpStatus:
565
565
- numeric
566
566
- symbolic
567
567
568
+ Rails/HttpUrl :
569
+ Description : ' Enforces passing a string literal as a URL to http method calls.'
570
+ Enabled : true
571
+ VersionAdded : ' 2.26'
572
+ Include :
573
+ - ' spec/**/*'
574
+ - ' test/**/*'
575
+
568
576
Rails/I18nLazyLookup :
569
577
Description : ' Checks for places where I18n "lazy" lookup can be used.'
570
578
StyleGuide : ' https://rails.rubystyle.guide/#lazy-lookup'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces passing a string literal as a URL to http method calls.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # get photos_path
11
+ # put photo_path(id)
12
+ # post edit_photo_path(id)
13
+ #
14
+ # # good
15
+ # get "/photos"
16
+ # put "/photos/#{id}"
17
+ # post "/photos/#{id}/edit"
18
+ class HttpUrl < Base
19
+ MSG = 'The first argument to `%<method>s` should be a string.'
20
+ RESTRICT_ON_SEND = %i[ get post put patch delete head ] . freeze
21
+
22
+ def_node_matcher :request_method? , <<-PATTERN
23
+ (send nil? {#{ RESTRICT_ON_SEND . map ( &:inspect ) . join ( ' ' ) } } $!str ...)
24
+ PATTERN
25
+
26
+ def on_send ( node )
27
+ request_method? ( node ) do |arg |
28
+ add_offense ( arg , message : format ( MSG , method : node . method_name ) )
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
Original file line number Diff line number Diff line change 61
61
require_relative 'rails/has_many_or_has_one_dependent'
62
62
require_relative 'rails/helper_instance_variable'
63
63
require_relative 'rails/http_positional_arguments'
64
+ require_relative 'rails/http_url'
64
65
require_relative 'rails/http_status'
65
66
require_relative 'rails/i18n_lazy_lookup'
66
67
require_relative 'rails/i18n_locale_assignment'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe RuboCop ::Cop ::Rails ::HttpUrl , :config do
4
+ %i[ get post put delete patch head ] . each do |http_method |
5
+ it "registers an offense when first argument to `#{ http_method } ` is not a string" do
6
+ padding = " #{ ' ' * http_method . length } "
7
+ expect_offense ( <<~RUBY , http_method : http_method )
8
+ #{ http_method } :resource
9
+ #{ padding } ^^^^^^^^^ The first argument to `#{ http_method } ` should be a string.
10
+ RUBY
11
+ end
12
+
13
+ it "does not register an offense when the first argument to #{ http_method } is a string" do
14
+ expect_no_offenses ( <<~RUBY )
15
+ #{ http_method } '/resource'
16
+ RUBY
17
+ end
18
+ end
19
+ end
You can’t perform that action at this time.
0 commit comments