betterlint 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71ba27a1b021ee2c1543aba5603245ae2631a64525c7c7e5338457b1296c66a6
4
- data.tar.gz: e3c21186976d7d503c890cd4e0c8fed99d5c6f6ae20071a17fa697e9c4928ea7
3
+ metadata.gz: 65b957bbe3e091232127a01b3803411610b8968c9a2cecd0be839196c4edd66a
4
+ data.tar.gz: 66f79246845f20ab4cbb43104de461f14988831d5377ce4d2279e6c739353256
5
5
  SHA512:
6
- metadata.gz: '048543b98c55befe6f306446a132f6f561598bf19ab9ffc4f74c6eb90b7ea5aa1d78d56594c34a2c8c6da7d79462329b8dadc8868af620fe53084189e7f6d604'
7
- data.tar.gz: 35388c596e5edc116860dd60796313b9d6569326e77929b5e05b0f48f6f13067534684ec9d37904e140ba4b2dea93f8e243e10ead8196ee1eaba34e6b600d898
6
+ metadata.gz: 7c4e37ddc5f6285ea890fa8a06ff8a46564801fe182ab215e6a43af7977f69166be914f52f379d11a1166d8ab3b0dd1b155a60f9d708ac387016f59c1aa82935
7
+ data.tar.gz: 746e680bf2720c96e826fe1d9299169e7334cacafb367b820008cee18cdd1b3626e7cb37a6b2fd356134d549187e37b8e50db969c0510f19720557491a4c2a95
data/config/default.yml CHANGED
@@ -86,7 +86,7 @@ Layout/CaseIndentation:
86
86
  IndentOneStep: true
87
87
 
88
88
  Layout/ClosingParenthesisIndentation:
89
- Enabled: false
89
+ Enabled: true
90
90
 
91
91
  Layout/FirstArrayElementIndentation:
92
92
  EnforcedStyle: consistent
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Betterment
6
+ class FetchBoolean < Base
7
+ MSG = <<~MSG
8
+ A boolean fetched from query params or ENV will never be false when
9
+ explicitly specified on the request or env var. Please use a model
10
+ with a boolean attribute, or cast the value.
11
+ MSG
12
+
13
+ # @!method fetch_boolean?(node)
14
+ def_node_matcher :fetch_boolean?, <<-PATTERN
15
+ (send _ :fetch _ (boolean))
16
+ PATTERN
17
+
18
+ # @!method fetch_env_boolean?(node)
19
+ def_node_matcher :fetch_env_boolean?, <<-PATTERN
20
+ (send (const nil? :ENV) :fetch _ (boolean))
21
+ PATTERN
22
+
23
+ # @!method boolean_cast?(node)
24
+ def_node_search :boolean_cast?, <<-PATTERN
25
+ (send
26
+ (send
27
+ (const
28
+ (const
29
+ (const nil? :ActiveModel) :Type) :Boolean) :new) :cast
30
+ ...)
31
+ PATTERN
32
+
33
+ # @!method action_controller?(node)
34
+ def_node_search :action_controller?, <<~PATTERN
35
+ {
36
+ (const {nil? cbase} :ApplicationController)
37
+ (const (const {nil? cbase} :ActionController) :Base)
38
+ }
39
+ PATTERN
40
+
41
+ def on_send(node)
42
+ return unless fetch_env_boolean?(node) ||
43
+ (fetch_boolean?(node) && inherit_action_controller_base?(node))
44
+
45
+ return if node.each_ancestor(:send).any? { |ancestor| boolean_cast?(ancestor) }
46
+
47
+ add_offense(node)
48
+ end
49
+
50
+ private
51
+
52
+ def inherit_action_controller_base?(node)
53
+ class_node = node.each_ancestor(:class).first
54
+ return false unless class_node
55
+
56
+ action_controller?(class_node)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -21,6 +21,7 @@ module RuboCop
21
21
  MSG
22
22
  METHOD_PATTERN = /^find_by_(.+?)(!)?$/
23
23
  FINDS = %i(find find_by find_by! where).freeze
24
+ GRAPHQL_PATTERN = /\bGraphQL\b/i
24
25
 
25
26
  def_node_matcher :custom_scope_find?, <<-PATTERN
26
27
  (send (send (const ... _) ...) {#{FINDS.map(&:inspect).join(' ')}} ...)
@@ -30,6 +31,10 @@ module RuboCop
30
31
  (send (const ... _) {#{FINDS.map(&:inspect).join(' ')}} ...)
31
32
  PATTERN
32
33
 
34
+ def_node_search :find_graphql_namespace_nodes, <<~PATTERN, name: GRAPHQL_PATTERN
35
+ (const _ %name)
36
+ PATTERN
37
+
33
38
  def initialize(config = nil, options = nil)
34
39
  super(config, options)
35
40
  config = @config.for_cop(self)
@@ -44,16 +49,26 @@ module RuboCop
44
49
  _, _, *arg_nodes = *node # rubocop:disable InternalAffairs/NodeDestructuring
45
50
  return unless
46
51
  (
47
- find?(node) ||
48
- custom_scope_find?(node) ||
49
- static_method_name(node.method_name)
52
+ find?(node) ||
53
+ custom_scope_find?(node) ||
54
+ static_method_name(node.method_name)
50
55
  ) && !@unauthenticated_models.include?(Utils::Parser.get_root_token(node))
51
56
 
52
- add_offense(node) if find_param_arg(arg_nodes)
57
+ add_offense(node) if find_param_arg(arg_nodes) || graphql_file? || graphql_namespace?(node)
53
58
  end
54
59
 
55
60
  private
56
61
 
62
+ def graphql_file?
63
+ processed_source.path&.match?(GRAPHQL_PATTERN)
64
+ end
65
+
66
+ def graphql_namespace?(node)
67
+ node
68
+ .each_ancestor(:class, :module)
69
+ .any? { |ancestor| find_graphql_namespace_nodes(ancestor).any? }
70
+ end
71
+
57
72
  def find_param_arg(arg_nodes)
58
73
  return unless arg_nodes
59
74
 
@@ -19,3 +19,4 @@ require 'rubocop/cop/betterment/allowlist_blocklist'
19
19
  require 'rubocop/cop/betterment/server_error_assertion'
20
20
  require 'rubocop/cop/betterment/hardcoded_id'
21
21
  require 'rubocop/cop/betterment/vague_serialize'
22
+ require 'rubocop/cop/betterment/fetch_boolean'
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.6.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Development
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-28 00:00:00.000000000 Z
11
+ date: 2024-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -95,6 +95,7 @@ files:
95
95
  - lib/rubocop/cop/betterment/allowlist_blocklist.rb
96
96
  - lib/rubocop/cop/betterment/authorization_in_controller.rb
97
97
  - lib/rubocop/cop/betterment/dynamic_params.rb
98
+ - lib/rubocop/cop/betterment/fetch_boolean.rb
98
99
  - lib/rubocop/cop/betterment/hardcoded_id.rb
99
100
  - lib/rubocop/cop/betterment/implicit_redirect_type.rb
100
101
  - lib/rubocop/cop/betterment/memoization_with_arguments.rb
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  - !ruby/object:Gem::Version
130
131
  version: '0'
131
132
  requirements: []
132
- rubygems_version: 3.4.20
133
+ rubygems_version: 3.5.6
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: Betterment rubocop configuration