betterlint 1.8.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: 65b957bbe3e091232127a01b3803411610b8968c9a2cecd0be839196c4edd66a
4
- data.tar.gz: 66f79246845f20ab4cbb43104de461f14988831d5377ce4d2279e6c739353256
3
+ metadata.gz: 8ae56cca2ec6c902c0eb366462cdc80fafb3895c3ea56454bdc247f528694cfa
4
+ data.tar.gz: d70a646105175ee4c68a1098c7bc31dc8d06249484245f180ad2e2164aa720c3
5
5
  SHA512:
6
- metadata.gz: 7c4e37ddc5f6285ea890fa8a06ff8a46564801fe182ab215e6a43af7977f69166be914f52f379d11a1166d8ab3b0dd1b155a60f9d708ac387016f59c1aa82935
7
- data.tar.gz: 746e680bf2720c96e826fe1d9299169e7334cacafb367b820008cee18cdd1b3626e7cb37a6b2fd356134d549187e37b8e50db969c0510f19720557491a4c2a95
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,85 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterlint
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.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-03-21 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
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: 1.62.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">"
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: 1.62.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubocop-performance
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 1.21.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 1.21.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubocop-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 2.24.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 2.24.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop-rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.6.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.6.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop-rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '2.24'
75
+ version: 2.28.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '2.24'
82
+ version: 2.28.0
83
83
  description: Betterment rubocop configuration
84
84
  email:
85
85
  - development@betterment.com
@@ -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:
@@ -130,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
133
  - !ruby/object:Gem::Version
131
134
  version: '0'
132
135
  requirements: []
133
- rubygems_version: 3.5.6
136
+ rubygems_version: 3.5.7
134
137
  signing_key:
135
138
  specification_version: 4
136
139
  summary: Betterment rubocop configuration