Skip to content

Commit 0a3bb88

Browse files
authored
Fix edge cases in status code autocorrection (#41)
* Handle `render` or `redirect_to` with parentheses * Handle `render` or `redirect_to` with no arguments
1 parent f169aa0 commit 0a3bb88

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
betterlint (1.10.0)
4+
betterlint (1.10.1)
55
rubocop (~> 1.62.0)
66
rubocop-performance (~> 1.21.0)
77
rubocop-rails (~> 2.24.0)

betterlint.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55

66
Gem::Specification.new do |s|
77
s.name = "betterlint"
8-
s.version = "1.10.0"
8+
s.version = "1.10.1"
99
s.authors = ["Development"]
1010
s.email = ["[email protected]"]
1111
s.summary = "Betterment rubocop configuration"

lib/rubocop/cop/betterment/redirect_status.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ class RedirectStatus < Base
1717
MSG
1818

1919
def on_def(node)
20-
each_offense(node, :redirect_to) do |responder|
21-
add_offense(responder) do |corrector|
22-
corrector.insert_after(responder, ", status: :see_other")
23-
end
24-
end
20+
each_offense(node, :redirect_to) { :see_other }
2521
end
2622
end
2723
end

lib/rubocop/cop/betterment/render_status.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class RenderStatus < Base
1818

1919
def on_def(node)
2020
each_offense(node, :render) do |responder|
21-
add_offense(responder) do |corrector|
22-
corrector.insert_after(responder, ", status: #{infer_status(responder).inspect}")
23-
end
21+
infer_status(responder)
2422
end
2523
end
2624

lib/rubocop/cop/betterment/utils/response_status.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,28 @@ module ResponseStatus
1111

1212
private
1313

14-
def each_offense(node, responder_name, &block)
15-
if UNSAFE_ACTIONS.include?(node.method_name)
16-
on_missing_status(node, responder_name, &block)
14+
def each_offense(node, responder_name)
15+
return unless UNSAFE_ACTIONS.include?(node.method_name)
16+
17+
on_missing_status(node, responder_name) do |responder|
18+
add_offense(responder) do |corrector|
19+
status = yield(responder)
20+
21+
if responder.arguments?
22+
corrector.insert_after(responder.last_argument, ", status: #{status.inspect}")
23+
else
24+
corrector.replace(responder, "#{responder_name} status: #{status.inspect}")
25+
end
26+
end
1727
end
1828
end
1929

2030
# @!method on_missing_status(node)
2131
def_node_search :on_missing_status, <<~PATTERN
22-
(send nil? %1 ... !(hash <(pair (sym :status) _) ...>))
32+
{
33+
(send nil? %1)
34+
(send nil? %1 ... !(hash <(pair (sym :status) _) ...>))
35+
}
2336
PATTERN
2437
end
2538
end

spec/rubocop/cop/betterment/redirect_status_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,27 @@
88
def create
99
redirect_to '/'
1010
^^^^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
11+
redirect_to('/')
12+
^^^^^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
1113
end
1214
1315
def update
1416
redirect_to '/'
1517
^^^^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
18+
redirect_to
19+
^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
1620
end
1721
RUBY
1822

1923
expect_correction(<<~RUBY)
2024
def create
2125
redirect_to '/', status: :see_other
26+
redirect_to('/', status: :see_other)
2227
end
2328
2429
def update
2530
redirect_to '/', status: :see_other
31+
redirect_to status: :see_other
2632
end
2733
RUBY
2834
end
@@ -48,6 +54,7 @@ def edit
4854
def create
4955
redirect_to '/', status: :found
5056
redirect_to '/', status: :see_other
57+
redirect_to status: :found
5158
end
5259
RUBY
5360
end

spec/rubocop/cop/betterment/render_status_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def create
1616
^^^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
1717
render plain: 'OK'
1818
^^^^^^^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
19+
render(:new)
20+
^^^^^^^^^^^^ Did you forget to specify an HTTP status code? [...]
21+
render
22+
^^^^^^ Did you forget to specify an HTTP status code? [...]
23+
render()
24+
^^^^^^^^ Did you forget to specify an HTTP status code? [...]
1925
end
2026
2127
def update
@@ -33,6 +39,9 @@ def create
3339
render :other, status: :ok
3440
render 'other', status: :ok
3541
render plain: 'OK', status: :ok
42+
render(:new, status: :unprocessable_entity)
43+
render status: :ok
44+
render status: :ok
3645
end
3746
3847
def update
@@ -63,6 +72,7 @@ def edit
6372
def create
6473
render plain: "OK", status: :ok
6574
render :new, status: :unprocessable_entity
75+
render status: :ok
6676
end
6777
RUBY
6878
end

0 commit comments

Comments
 (0)