betterlint 1.9.0 → 1.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/config/default.yml +12 -0
- data/lib/rubocop/cop/betterment/redirect_status.rb +25 -0
- data/lib/rubocop/cop/betterment/render_status.rb +43 -0
- data/lib/rubocop/cop/betterment/utils/response_status.rb +41 -0
- data/lib/rubocop/cop/betterment.rb +2 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebad0e83ab9fb4d6fd1beb0c28ea03bbf593cd7d9831c5b2546f646b8622a449
|
4
|
+
data.tar.gz: c8c3b31f1b37f54fdd8ae8857ceb9f39f372e130b888541afab8beae78d60ccb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 743afd50991fd213f1ca6a4f311cc96c506c0f2dc76de117887756ec634b0ad1704e953a871c0f0280a25c7dba60194076fe262257b80364903ca56b6541f33d
|
7
|
+
data.tar.gz: c7ba0f48ee2e69d0590de7973922aeef331c48a6a79417bae84b05ff45c33217ffb8bd01f0814123ff22201c8e73a40c9a307879a377826951589beed966523f
|
data/README.md
CHANGED
@@ -210,3 +210,20 @@ This cop is capable of autocorrecting offenses, but it's not entirely safe. If y
|
|
210
210
|
Betterment/HardcodedID:
|
211
211
|
AutoCorrect: true
|
212
212
|
```
|
213
|
+
|
214
|
+
### Betterment/RedirectStatus
|
215
|
+
|
216
|
+
The `redirect_to` method defaults to a `302 Found`, but when redirecting a POST/PUT/PATCH/DELETE
|
217
|
+
to a GET location, the correct response code is `303 See Other`.
|
218
|
+
|
219
|
+
This cop requires you to explictly provide an HTTP status code when redirecting from the create,
|
220
|
+
update, and destory actions. When autocorrecting, this will automatically add `status: :see_other`.
|
221
|
+
|
222
|
+
### Betterment/RenderStatus
|
223
|
+
|
224
|
+
The `render` method defaults to a `200 OK`. Calling `render` in the create, update, and destroy
|
225
|
+
actions often indicates error handling (e.g. `422 Unprocessable Entity`).
|
226
|
+
|
227
|
+
This cop requires you to explicitly provide an HTTP status code when rendering a response in the
|
228
|
+
create, update, and destroy actions. When autocorrecting, this will automatically add
|
229
|
+
`status: :unprocessable_entity` or `status: :ok` depending on what you're rendering.
|
data/config/default.yml
CHANGED
@@ -76,6 +76,18 @@ Betterment/UnsafeJob:
|
|
76
76
|
Betterment/UnscopedFind:
|
77
77
|
StyleGuide: '#bettermentunscopedfind'
|
78
78
|
|
79
|
+
Betterment/RedirectStatus:
|
80
|
+
SafeAutoCorrect: false
|
81
|
+
Description: Detect missing status codes when redirecting POST, PUT, PATCH, or DELETE responses
|
82
|
+
Include:
|
83
|
+
- app/controllers/**/*.rb
|
84
|
+
|
85
|
+
Betterment/RenderStatus:
|
86
|
+
SafeAutoCorrect: false
|
87
|
+
Description: Detect missing status codes when rendering POST, PUT, PATCH, or DELETE responses
|
88
|
+
Include:
|
89
|
+
- app/controllers/**/*.rb
|
90
|
+
|
79
91
|
FactoryBot/ConsistentParenthesesStyle:
|
80
92
|
Enabled: false
|
81
93
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utils/response_status'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module Betterment
|
8
|
+
class RedirectStatus < Base
|
9
|
+
extend AutoCorrector
|
10
|
+
|
11
|
+
include Utils::ResponseStatus
|
12
|
+
|
13
|
+
MSG = <<~MSG.gsub(/\s+/, " ")
|
14
|
+
Did you forget to specify an HTTP status code? The default is `status: :found`, which
|
15
|
+
is usually inappropriate in this situation. Use `status: :see_other` when redirecting a
|
16
|
+
POST, PUT, PATCH, or DELETE request to a GET resource.
|
17
|
+
MSG
|
18
|
+
|
19
|
+
def on_def(node)
|
20
|
+
each_offense(node, :redirect_to) { :see_other }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utils/response_status'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module Betterment
|
8
|
+
class RenderStatus < Base
|
9
|
+
extend AutoCorrector
|
10
|
+
|
11
|
+
include Utils::ResponseStatus
|
12
|
+
|
13
|
+
MSG = <<~MSG.gsub(/\s+/, " ")
|
14
|
+
Did you forget to specify an HTTP status code? The default is `status: :ok`, which might
|
15
|
+
be inappropriate in this situation. Rendering after a POST, PUT, PATCH or DELETE request
|
16
|
+
typically represents an error (e.g. `status: :unprocessable_entity`).
|
17
|
+
MSG
|
18
|
+
|
19
|
+
def on_def(node)
|
20
|
+
each_offense(node, :render) do |responder|
|
21
|
+
infer_status(responder)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def infer_status(responder)
|
28
|
+
case extract_template(responder).to_s
|
29
|
+
when 'new', 'edit'
|
30
|
+
:unprocessable_entity
|
31
|
+
else
|
32
|
+
:ok
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# @!method extract_template(node)
|
37
|
+
def_node_matcher :extract_template, <<~PATTERN
|
38
|
+
(send nil? :render {(sym $_) (str $_)} ...)
|
39
|
+
PATTERN
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Betterment
|
6
|
+
module Utils
|
7
|
+
module ResponseStatus
|
8
|
+
extend RuboCop::NodePattern::Macros
|
9
|
+
|
10
|
+
UNSAFE_ACTIONS = %i(create update destroy).freeze
|
11
|
+
|
12
|
+
private
|
13
|
+
|
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
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @!method on_missing_status(node)
|
31
|
+
def_node_search :on_missing_status, <<~PATTERN
|
32
|
+
{
|
33
|
+
(send nil? %1)
|
34
|
+
(send nil? %1 ... !(hash <(pair (sym :status) _) ...>))
|
35
|
+
}
|
36
|
+
PATTERN
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -20,3 +20,5 @@ require 'rubocop/cop/betterment/server_error_assertion'
|
|
20
20
|
require 'rubocop/cop/betterment/hardcoded_id'
|
21
21
|
require 'rubocop/cop/betterment/vague_serialize'
|
22
22
|
require 'rubocop/cop/betterment/fetch_boolean'
|
23
|
+
require 'rubocop/cop/betterment/render_status'
|
24
|
+
require 'rubocop/cop/betterment/redirect_status'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: betterlint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Development
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -100,6 +100,8 @@ files:
|
|
100
100
|
- lib/rubocop/cop/betterment/implicit_redirect_type.rb
|
101
101
|
- lib/rubocop/cop/betterment/memoization_with_arguments.rb
|
102
102
|
- lib/rubocop/cop/betterment/non_standard_actions.rb
|
103
|
+
- lib/rubocop/cop/betterment/redirect_status.rb
|
104
|
+
- lib/rubocop/cop/betterment/render_status.rb
|
103
105
|
- lib/rubocop/cop/betterment/server_error_assertion.rb
|
104
106
|
- lib/rubocop/cop/betterment/site_prism_loaded.rb
|
105
107
|
- lib/rubocop/cop/betterment/spec_helper_required_outside_spec_dir.rb
|
@@ -109,6 +111,7 @@ files:
|
|
109
111
|
- lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb
|
110
112
|
- lib/rubocop/cop/betterment/utils/method_return_table.rb
|
111
113
|
- lib/rubocop/cop/betterment/utils/parser.rb
|
114
|
+
- lib/rubocop/cop/betterment/utils/response_status.rb
|
112
115
|
- lib/rubocop/cop/betterment/vague_serialize.rb
|
113
116
|
homepage:
|
114
117
|
licenses:
|