betterlint 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1491e20e0716813156b261f5cd5a9cf253455c004fc1bd19b6b76145519a63ac
4
- data.tar.gz: 235b8423ef33fa88e926e6ff174d179649d4a15b737a4dc3757a97e919267223
3
+ metadata.gz: 8ae56cca2ec6c902c0eb366462cdc80fafb3895c3ea56454bdc247f528694cfa
4
+ data.tar.gz: d70a646105175ee4c68a1098c7bc31dc8d06249484245f180ad2e2164aa720c3
5
5
  SHA512:
6
- metadata.gz: 70410f02798546b16ec349134e18397794f52ba01341f7d5023514c9fdc6a6c52b2aa579c811d6cb66a0955e93f08330398c4b5846a89434464c72b9122f55b9
7
- data.tar.gz: 9773586040f235b40766bccd931e7dcc14e0d9d6297f56e5bce6a155aaa0854cdb1f289dc4667cea0459007663c301bc12e00ca11a5682f0f517188e2c838e08
6
+ metadata.gz: cbde32295337b547e4b8a9fe128ebd0406573745a8e8a8acafb8c3bd8f10f6e052442c6ff36e1c915d559fc3a924f8ed7aa40914d78b3560e00c373f1019deaa
7
+ data.tar.gz: 709a0ea18f486e1489da86947951cf73d9d22341178a46c242d282d686eeafbce4986fbef7143305e20698e047478532dc0ce2556d781ad4b6b6fd093721ee05
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,29 @@
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) do |responder|
21
+ add_offense(responder) do |corrector|
22
+ corrector.insert_after(responder, ", status: :see_other")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
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
+ add_offense(responder) do |corrector|
22
+ corrector.insert_after(responder, ", status: #{infer_status(responder).inspect}")
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def infer_status(responder)
30
+ case extract_template(responder).to_s
31
+ when 'new', 'edit'
32
+ :unprocessable_entity
33
+ else
34
+ :ok
35
+ end
36
+ end
37
+
38
+ # @!method extract_template(node)
39
+ def_node_matcher :extract_template, <<~PATTERN
40
+ (send nil? :render {(sym $_) (str $_)} ...)
41
+ PATTERN
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,28 @@
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, &block)
15
+ if UNSAFE_ACTIONS.include?(node.method_name)
16
+ on_missing_status(node, responder_name, &block)
17
+ end
18
+ end
19
+
20
+ # @!method on_missing_status(node)
21
+ def_node_search :on_missing_status, <<~PATTERN
22
+ (send nil? %1 ... !(hash <(pair (sym :status) _) ...>))
23
+ PATTERN
24
+ end
25
+ end
26
+ end
27
+ end
28
+ 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.9.0
4
+ version: 1.10.0
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-04 00:00:00.000000000 Z
11
+ date: 2024-04-09 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: