Skip to content

Commit cbd4725

Browse files
committed
Rename cop and general for deprecated statuses
1 parent a4824e5 commit cbd4725

8 files changed

+285
-158
lines changed

changelog/add_unprocessable_content_status_cop.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1520](https://github.com/rubocop/rubocop-rails/pull/1520): New `Rails/DeprecatedHttpStatusNames` cop. ([@tuxagon][])

config/default.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ Rails/DeprecatedActiveModelErrorsMethods:
394394
VersionAdded: '2.14'
395395
VersionChanged: '2.18'
396396

397+
Rails/DeprecatedHttpStatusNames:
398+
Description: 'Use the new HTTP status names instead of deprecated ones.'
399+
Enabled: pending
400+
Severity: warning
401+
VersionAdded: '<<next>>'
402+
Include:
403+
- '**/app/controllers/**/*.rb'
404+
397405
Rails/DotSeparatedKeys:
398406
Description: 'Enforces the use of dot-separated keys instead of `:scope` options in `I18n` translation methods.'
399407
StyleGuide: 'https://rails.rubystyle.guide/#dot-separated-keys'
@@ -1232,14 +1240,6 @@ Rails/UnknownEnv:
12321240
- test
12331241
- production
12341242

1235-
Rails/UnprocessableContentStatus:
1236-
Description: 'Use `:unprocessable_content` instead of `:unprocessable_entity`.'
1237-
Enabled: pending
1238-
Severity: warning
1239-
VersionAdded: '<<next>>'
1240-
Include:
1241-
- '**/app/controllers/**/*.rb'
1242-
12431243
Rails/UnusedIgnoredColumns:
12441244
Description: 'Remove a column that does not exist from `ignored_columns`.'
12451245
Enabled: false
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Rails
6+
# Enforces the use of the current HTTP status names instead of deprecated ones.
7+
#
8+
# @example
9+
# # bad
10+
# render json: { error: "Invalid data" }, status: :unprocessable_entity
11+
# head :payload_too_large
12+
#
13+
# # good
14+
# render json: { error: "Invalid data" }, status: :unprocessable_content
15+
# head :content_too_large
16+
#
17+
class DeprecatedHttpStatusNames < Base
18+
extend AutoCorrector
19+
20+
requires_gem 'rack', '>= 3.1.0'
21+
22+
RESTRICT_ON_SEND = %i[render redirect_to head assert_response assert_redirected_to].freeze
23+
24+
DEPRECATED_STATUSES = {
25+
unprocessable_entity: :unprocessable_content,
26+
payload_too_large: :content_too_large
27+
}.freeze
28+
29+
MSG = 'Use `:%<preferred>s` instead of `:%<deprecated>s`. The `:%<deprecated>s` status is deprecated.'
30+
31+
def_node_matcher :status_method_call, <<~PATTERN
32+
{
33+
(send nil? {:render :redirect_to} _ $hash)
34+
(send nil? {:render :redirect_to} $hash)
35+
(send nil? {:head :assert_response} $_ ...)
36+
(send nil? :assert_redirected_to _ $hash ...)
37+
(send nil? :assert_redirected_to $hash ...)
38+
}
39+
PATTERN
40+
41+
def_node_matcher :status_hash_value, <<~PATTERN
42+
(hash <(pair (sym :status) $_) ...>)
43+
PATTERN
44+
45+
def on_send(node)
46+
status_method_call(node) do |status_node|
47+
if status_node.hash_type?
48+
# Handle hash arguments like { status: :unprocessable_entity }
49+
status_hash_value(status_node) do |status_value|
50+
find_deprecated_status_names(status_value)
51+
end
52+
else
53+
# Handle positional arguments like head :unprocessable_entity
54+
find_deprecated_status_names(status_node)
55+
end
56+
end
57+
end
58+
59+
private
60+
61+
def find_deprecated_status_names(node)
62+
if node.sym_type? && DEPRECATED_STATUSES.key?(node.value)
63+
deprecated_status = node.value
64+
preferred_status = DEPRECATED_STATUSES[deprecated_status]
65+
66+
message = format(MSG, deprecated: deprecated_status, preferred: preferred_status)
67+
68+
add_offense(node, message: message) do |corrector|
69+
corrector.replace(node, ":#{preferred_status}")
70+
end
71+
elsif node.respond_to?(:children)
72+
# Recursively search child nodes (handles ternary expressions, etc.)
73+
node.children.each do |child|
74+
find_deprecated_status_names(child) if child.is_a?(Parser::AST::Node)
75+
end
76+
end
77+
end
78+
end
79+
end
80+
end
81+
end

lib/rubocop/cop/rails/unprocessable_content_status.rb

Lines changed: 0 additions & 82 deletions
This file was deleted.

lib/rubocop/cop/rails_cops.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
require_relative 'rails/delegate'
4242
require_relative 'rails/delegate_allow_blank'
4343
require_relative 'rails/deprecated_active_model_errors_methods'
44+
require_relative 'rails/deprecated_http_status_names'
4445
require_relative 'rails/dot_separated_keys'
4546
require_relative 'rails/duplicate_association'
4647
require_relative 'rails/duplicate_scope'
@@ -136,7 +137,6 @@
136137
require_relative 'rails/uniq_before_pluck'
137138
require_relative 'rails/unique_validation_without_index'
138139
require_relative 'rails/unknown_env'
139-
require_relative 'rails/unprocessable_content_status'
140140
require_relative 'rails/unused_ignored_columns'
141141
require_relative 'rails/unused_render_content'
142142
require_relative 'rails/validation'

0 commit comments

Comments
 (0)