-
-
Notifications
You must be signed in to change notification settings - Fork 284
Add Rails/UnprocessableContentStatus
cop
#1520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tuxagon
wants to merge
9
commits into
rubocop:master
Choose a base branch
from
tuxagon:unprocessable-content-status-cop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4824e5
Introduce UnprocessableContentStatus cop
tuxagon cbd4725
Rename cop and general for deprecated statuses
tuxagon c5c2d7a
Improve message
tuxagon af2ea77
Improve descriptions
tuxagon f7ec81e
Rename cop
tuxagon c835727
Rename constant
tuxagon 55f1f6c
Rename private method
tuxagon 76b07f6
Remove deprecated references
tuxagon ff94df5
Test variable and method call checks
tuxagon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
* [#1520](https://github.com/rubocop/rubocop-rails/pull/1520): New `Rails/HttpStatusNameConsistency` cop. ([@tuxagon][]) |
This file contains hidden or 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 hidden or 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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Enforces consistency by using the current HTTP status names. | ||
# | ||
# @example | ||
# # bad | ||
# render json: { error: "Invalid data" }, status: :unprocessable_entity | ||
# head :payload_too_large | ||
# | ||
# # good | ||
# render json: { error: "Invalid data" }, status: :unprocessable_content | ||
# head :content_too_large | ||
# | ||
class HttpStatusNameConsistency < Base | ||
extend AutoCorrector | ||
|
||
requires_gem 'rack', '>= 3.1.0' | ||
|
||
RESTRICT_ON_SEND = %i[render redirect_to head assert_response assert_redirected_to].freeze | ||
|
||
PREFERRED_STATUSES = { | ||
unprocessable_entity: :unprocessable_content, | ||
payload_too_large: :content_too_large | ||
}.freeze | ||
|
||
MSG = 'Prefer `:%<preferred>s` over `:%<current>s`.' | ||
|
||
def_node_matcher :status_method_call, <<~PATTERN | ||
{ | ||
(send nil? {:render :redirect_to} _ $hash) | ||
(send nil? {:render :redirect_to} $hash) | ||
(send nil? {:head :assert_response} $_ ...) | ||
(send nil? :assert_redirected_to _ $hash ...) | ||
(send nil? :assert_redirected_to $hash ...) | ||
} | ||
PATTERN | ||
|
||
def_node_matcher :status_hash_value, <<~PATTERN | ||
(hash <(pair (sym :status) $_) ...>) | ||
PATTERN | ||
|
||
def on_send(node) | ||
status_method_call(node) do |status_node| | ||
if status_node.hash_type? | ||
# Handle hash arguments like { status: :unprocessable_entity } | ||
status_hash_value(status_node) do |status_value| | ||
check_status_name_consistency(status_value) | ||
end | ||
else | ||
# Handle positional arguments like head :unprocessable_entity | ||
check_status_name_consistency(status_node) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_status_name_consistency(node) | ||
if node.sym_type? && PREFERRED_STATUSES.key?(node.value) | ||
current_status = node.value | ||
preferred_status = PREFERRED_STATUSES[current_status] | ||
|
||
message = format(MSG, current: current_status, preferred: preferred_status) | ||
|
||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(node, ":#{preferred_status}") | ||
end | ||
else | ||
node.children.each do |child| | ||
check_status_name_consistency(child) if child.is_a?(Parser::AST::Node) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
228 changes: 228 additions & 0 deletions
228
spec/rubocop/cop/rails/http_status_name_consistency_spec.rb
This file contains hidden or 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,228 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::HttpStatusNameConsistency, :config do | ||
context 'when Rack is older than 3.1' do | ||
let(:gem_versions) { { 'rack' => '3.0.0' } } | ||
|
||
it 'does nothing' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_entity | ||
head :payload_too_large | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when Rack is 3.1 or later' do | ||
let(:gem_versions) { { 'rack' => '3.1.0' } } | ||
|
||
context 'with :unprocessable_entity' do | ||
it 'registers an offense when using :unprocessable_entity in render' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in head' do | ||
expect_offense(<<~RUBY) | ||
head :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in redirect_to' do | ||
expect_offense(<<~RUBY) | ||
redirect_to some_path, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
redirect_to some_path, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in assert_response' do | ||
expect_offense(<<~RUBY) | ||
assert_response :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_response :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in assert_redirected_to' do | ||
expect_offense(<<~RUBY) | ||
assert_redirected_to some_path, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_redirected_to some_path, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in ternary expression' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_entity : :ok | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_content : :ok | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :unprocessable_content' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :unprocessable_entity in hash key' do | ||
expect_no_offenses(<<~RUBY) | ||
{ unprocessable_entity: 'Invalid data' } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when status is provided via variable' do | ||
expect_no_offenses(<<~RUBY) | ||
status_var = :unprocessable_entity | ||
head status_var | ||
render json: { error: 'Invalid data' }, status: status_var | ||
redirect_to some_path, status: status_var | ||
assert_response status_var | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when status is provided via method call' do | ||
expect_no_offenses(<<~RUBY) | ||
head get_status_code | ||
render json: { error: 'Invalid data' }, status: calculate_status | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with :payload_too_large' do | ||
it 'registers an offense when using :payload_too_large in render' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in head' do | ||
expect_offense(<<~RUBY) | ||
head :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in redirect_to' do | ||
expect_offense(<<~RUBY) | ||
redirect_to some_path, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
redirect_to some_path, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in assert_response' do | ||
expect_offense(<<~RUBY) | ||
assert_response :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_response :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in assert_redirected_to' do | ||
expect_offense(<<~RUBY) | ||
assert_redirected_to some_path, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_redirected_to some_path, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in ternary expression' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'File too big' }, status: some_condition ? :payload_too_large : :ok | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'File too big' }, status: some_condition ? :content_too_large : :ok | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :content_too_large' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :payload_too_large in hash key' do | ||
expect_no_offenses(<<~RUBY) | ||
{ payload_too_large: 'File too big' } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when status is provided via variable' do | ||
expect_no_offenses(<<~RUBY) | ||
status_var = :payload_too_large | ||
head status_var | ||
render json: { error: 'Invalid data' }, status: status_var | ||
redirect_to some_path, status: status_var | ||
assert_response status_var | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when status is provided via method call' do | ||
expect_no_offenses(<<~RUBY) | ||
head get_status_code | ||
render json: { error: 'Invalid data' }, status: calculate_status | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with partial preferred statuses' do | ||
it 'handles status preference in the same code' do | ||
expect_offense(<<~RUBY) | ||
head :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Prefer `:unprocessable_content` over `:unprocessable_entity`. | ||
head :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Prefer `:content_too_large` over `:payload_too_large`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :unprocessable_content | ||
head :content_too_large | ||
RUBY | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case works fine, but let's add an assertion that
head
orrender
with variable status (e.g.head some_variable
) do not lead to offenses