forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Rails/HttpUrl
cop to enforce passing a string literal as a URL …
…to http method calls For the test cases; to make the actual URL obvious and to facilitate tracking endpoint path changes. Original discussion: rubocop/rails-style-guide#328 Signed-off-by: moznion <[email protected]>
- Loading branch information
Showing
4 changed files
with
62 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
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Enforces passing a string literal as a URL to http method calls. | ||
# | ||
# @example | ||
# # bad | ||
# get photos_path | ||
# put photo_path(id) | ||
# post edit_photo_path(id) | ||
# | ||
# # good | ||
# get "/photos" | ||
# put "/photos/#{id}" | ||
# post "/photos/#{id}/edit" | ||
class HttpUrl < Base | ||
MSG = 'The first argument to `%<method>s` should be a string.' | ||
RESTRICT_ON_SEND = %i[get post put patch delete head].freeze | ||
|
||
def_node_matcher :request_method?, <<-PATTERN | ||
(send nil? {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} $!str ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
request_method?(node) do |arg| | ||
add_offense(arg, message: format(MSG, method: node.method_name)) | ||
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::HttpUrl, :config do | ||
%i[get post put delete patch head].each do |http_method| | ||
it "registers an offense when first argument to `#{http_method}` is not a string" do | ||
padding = " #{' ' * http_method.length}" | ||
expect_offense(<<~RUBY, http_method: http_method) | ||
#{http_method} :resource | ||
#{padding}^^^^^^^^^ The first argument to `#{http_method}` should be a string. | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when the first argument to #{http_method} is a string" do | ||
expect_no_offenses(<<~RUBY) | ||
#{http_method} '/resource' | ||
RUBY | ||
end | ||
end | ||
end |