rubocop-gusto 11.11.0 → 12.0.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 +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +22 -0
- data/config/graphql.yml +3 -0
- data/config/gusto_cops.yml +10 -0
- data/lib/rubocop/cop/gusto/graphql/resource_from_context.rb +97 -0
- data/lib/rubocop/cop/internal_affairs/require_restrict_on_send.rb +2 -1
- data/lib/rubocop/gusto/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 73fa562ea984ae96cedd99d4dd60d9a8d9e6e9183d257bf1cf57605746a614ac
|
|
4
|
+
data.tar.gz: 710166e958d19967805bfd437f0565a303978ffb5b9c4c0fe11b8d2f886bda68
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e80f7ad9656d2c34666913fc8ac05d03aa6415c12368c238dc229fddcc0c96e7d3150c1d0d42e43d3e1bb4f77de6d59fadc4a584e0087833520c41e18d5b5d54
|
|
7
|
+
data.tar.gz: 0a7c2d54ef905e089ac3a5299d1cde7948b25f1d22aea4d7dc472d74b0d109b69bb637f342d82de2934266b940e2c0c49c64d04f0a7221d580d54eeb571c49da
|
data/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,24 @@
|
|
|
3
3
|
- Remove redundant `Rails: Enabled: true` from `config/rails.yml` (already set by rubocop-rails' own defaults)
|
|
4
4
|
- Enable `Rails/DefaultScope` cop (disabled by default in rubocop-rails)
|
|
5
5
|
|
|
6
|
+
## [12.0.0](https://github.com/Gusto/rubocop-gusto/compare/v11.12.0...v12.0.0) (2026-11-24)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### ⚠ BREAKING CHANGES
|
|
10
|
+
|
|
11
|
+
* drop Ruby 3.3 support ([#204](https://github.com/Gusto/rubocop-gusto/issues/204))
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* drop Ruby 3.3 support ([#204](https://github.com/Gusto/rubocop-gusto/issues/204)) ([eaf21a7](https://github.com/Gusto/rubocop-gusto/commit/eaf21a7c29615339fc264f1353568ab3365112d5))
|
|
16
|
+
|
|
17
|
+
## [11.12.0](https://github.com/Gusto/rubocop-gusto/compare/v11.11.0...v11.12.0) (2026-11-12)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* add Gusto/Graphql/ResourceFromContext cop ([#182](https://github.com/Gusto/rubocop-gusto/issues/182)) ([b8dbb7d](https://github.com/Gusto/rubocop-gusto/commit/b8dbb7d36714748acb0e3c39215fcbd495c6fa1c))
|
|
23
|
+
|
|
6
24
|
## [11.11.0](https://github.com/Gusto/rubocop-gusto/compare/v11.10.0...v11.11.0) (2026-10-27)
|
|
7
25
|
|
|
8
26
|
|
data/README.md
CHANGED
|
@@ -61,6 +61,28 @@ By default the `Gusto/Graphql` department is scoped to `**/graphql/**/*`.
|
|
|
61
61
|
classes provide that read the node without naming `object`; leave it empty and the cop reports
|
|
62
62
|
those resolvers as offenses.
|
|
63
63
|
|
|
64
|
+
`Gusto/Graphql/ResourceFromContext` reads `ResourceAccessors`, the context methods that name what a
|
|
65
|
+
field acts on rather than who is asking, and is inert until you fill it in. List only accessors that
|
|
66
|
+
name a target; a read that names the caller belongs on a separate accessor kept off the list. A
|
|
67
|
+
listed name also covers its bang form (`company_id!`), but not its predicate (`company_id?`). The
|
|
68
|
+
only fallback the cop accepts is gusto-graphql's `default_from_context:` (11.14.0 and later), so a
|
|
69
|
+
context read in a hand-written `prepare:` is flagged. A read is reported where it is written: one in
|
|
70
|
+
a base class or helper is reported once at its definition, not in the classes that inherit or call
|
|
71
|
+
it, so fix the base rather than grandfather it. A read that reaches the target through a principal
|
|
72
|
+
object, such as `context.current_user.company_id`, is not seen, and neither is `context.fetch(:x)`;
|
|
73
|
+
`context[:x]` is `Gusto/Graphql/PreventContextHashAccess`'s job. The cop already follows `context`
|
|
74
|
+
through `T.cast`, `T.let` and local variables; list any base class method that returns the context
|
|
75
|
+
under another name in `ContextMethods`:
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
Gusto/Graphql/ResourceFromContext:
|
|
79
|
+
ContextMethods:
|
|
80
|
+
- typed_context
|
|
81
|
+
ResourceAccessors:
|
|
82
|
+
- company_id
|
|
83
|
+
- company_uuid
|
|
84
|
+
```
|
|
85
|
+
|
|
64
86
|
Several of these cops replace a project-specific convention with a generic message. Put the
|
|
65
87
|
convention your project actually follows in the cop's `Details:`, and set
|
|
66
88
|
`AllCops: ExtraDetails: true` so it is appended to the offense message:
|
data/config/graphql.yml
CHANGED
data/config/gusto_cops.yml
CHANGED
|
@@ -91,6 +91,16 @@ Gusto/Graphql/ResolverIgnoresObject:
|
|
|
91
91
|
# whose GraphQL bases offer such helpers must list them here or the cop reports false positives.
|
|
92
92
|
NodeAccessors: []
|
|
93
93
|
|
|
94
|
+
Gusto/Graphql/ResourceFromContext:
|
|
95
|
+
Description: 'Flags a GraphQL resolver that takes its target from a context accessor instead of an argument or `object`.'
|
|
96
|
+
Enabled: false
|
|
97
|
+
# Methods inherited from a base class that return the GraphQL context under another name, such as
|
|
98
|
+
# a typed wrapper. The cop cannot see those definitions, so a project lists them here.
|
|
99
|
+
ContextMethods: []
|
|
100
|
+
# Context methods that name what a field acts on (a company, an employee) rather than who is
|
|
101
|
+
# asking. Nothing is flagged until a project lists its own. A name also covers its bang form.
|
|
102
|
+
ResourceAccessors: []
|
|
103
|
+
|
|
94
104
|
Gusto/Graphql/TypeVariables:
|
|
95
105
|
Description: 'Requires `type_member` and `type_template` in GraphQL objects to name a type rather than `T.untyped`.'
|
|
96
106
|
Enabled: false
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Gusto
|
|
6
|
+
module Graphql
|
|
7
|
+
# Flags a GraphQL resolver that takes its target from the request context rather than from
|
|
8
|
+
# its arguments or its parent `object`. The one sanctioned fallback is gusto-graphql's
|
|
9
|
+
# `default_from_context:`, which defaults an omitted argument with no context read to flag.
|
|
10
|
+
# A context read inside a hand-written `prepare:` is flagged, because it cannot be told apart
|
|
11
|
+
# from an override that ignores the client's value.
|
|
12
|
+
#
|
|
13
|
+
# Flags nothing until `ResourceAccessors` lists the context methods that name a resource. A listed
|
|
14
|
+
# name also covers its bang form (`company_id!`), which returns the same target, but not its
|
|
15
|
+
# predicate form (`company_id?`), which returns a boolean.
|
|
16
|
+
#
|
|
17
|
+
# @example ResourceAccessors: [company_uuid]
|
|
18
|
+
# # bad
|
|
19
|
+
# def resolve
|
|
20
|
+
# Company.find_by!(uuid: context.company_uuid)
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# # good
|
|
24
|
+
# argument :company_id, ID, as: :company_uuid
|
|
25
|
+
#
|
|
26
|
+
# def resolve(company_uuid:)
|
|
27
|
+
# Company.find_by!(uuid: company_uuid)
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# # good, for an existing field whose clients omit the argument
|
|
31
|
+
# argument :company_id, ID, required: false, as: :company_uuid, default_from_context: :company_uuid
|
|
32
|
+
class ResourceFromContext < Base
|
|
33
|
+
MSG = "Take the target from an argument or `object`, not `%{source}`; to default an omitted " \
|
|
34
|
+
"argument, use `default_from_context:`. If it names who is asking, not what is acted on, " \
|
|
35
|
+
"read it through a caller accessor kept off `ResourceAccessors`."
|
|
36
|
+
|
|
37
|
+
LOCAL_SCOPES = %i(any_def any_block).freeze
|
|
38
|
+
|
|
39
|
+
# @!method context_reference?(node)
|
|
40
|
+
def_node_matcher :context_reference?, "{(call _ :context) (lvar {:context :ctx}) (ivar :@context)}"
|
|
41
|
+
|
|
42
|
+
# @!method context_method_call?(node, names)
|
|
43
|
+
def_node_matcher :context_method_call?, "(send nil? %1)"
|
|
44
|
+
|
|
45
|
+
# @!method sorbet_wrapped(node)
|
|
46
|
+
def_node_matcher :sorbet_wrapped, "(send (const nil? :T) {:cast :let} $_ ...)"
|
|
47
|
+
|
|
48
|
+
def on_new_investigation
|
|
49
|
+
@accessors = configured_names("ResourceAccessors").each_with_object(Set.new) do |name, names|
|
|
50
|
+
base = name.to_s.delete_suffix("!")
|
|
51
|
+
names << base.to_sym << :"#{base}!"
|
|
52
|
+
end
|
|
53
|
+
@context_methods = configured_names("ContextMethods")
|
|
54
|
+
@aliases = nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def on_send(read)
|
|
58
|
+
return unless @accessors.include?(read.method_name)
|
|
59
|
+
return unless context?(read.receiver) || context_alias?(read.receiver)
|
|
60
|
+
|
|
61
|
+
add_offense(read, message: format(MSG, source: read.source))
|
|
62
|
+
end
|
|
63
|
+
alias_method :on_csend, :on_send
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def context?(node)
|
|
68
|
+
wrapped = sorbet_wrapped(node)
|
|
69
|
+
return context?(wrapped) if wrapped
|
|
70
|
+
|
|
71
|
+
context_reference?(node) || context_method_call?(node, @context_methods)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def context_alias?(node)
|
|
75
|
+
return false unless node&.lvar_type?
|
|
76
|
+
|
|
77
|
+
aliases.any? { |assignment| assignment.name == node.children.first && visible_from?(assignment, node) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# A local is visible in the method or block that assigns it and in blocks nested inside it.
|
|
81
|
+
def visible_from?(assignment, read)
|
|
82
|
+
scope = assignment.each_ancestor(*LOCAL_SCOPES).first
|
|
83
|
+
scope.nil? || read.each_ancestor(*LOCAL_SCOPES).include?(scope)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def aliases
|
|
87
|
+
@aliases ||= processed_source.ast.each_node(:lvasgn).select { |assignment| context?(assignment.expression) }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def configured_names(key)
|
|
91
|
+
Array(cop_config[key]).to_set(&:to_sym)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -21,7 +21,8 @@ module RuboCop
|
|
|
21
21
|
# end
|
|
22
22
|
# end
|
|
23
23
|
#
|
|
24
|
-
# NOTE:
|
|
24
|
+
# NOTE: `RESTRICT_ON_SEND` is the default here. A cop that must see every send node (for example, because its
|
|
25
|
+
# method names come from config) is excluded in `.rubocop.yml` with its reason.
|
|
25
26
|
# Upstream Rubocop chose not to implement this as there are many cops in Core that investigate every node.
|
|
26
27
|
class RequireRestrictOnSend < Base
|
|
27
28
|
MSG = "Missing `RESTRICT_ON_SEND` declaration when using `on_send` or `after_send`."
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-gusto
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 12.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gusto Engineering
|
|
@@ -173,6 +173,7 @@ files:
|
|
|
173
173
|
- lib/rubocop/cop/gusto/graphql/prevent_context_hash_access.rb
|
|
174
174
|
- lib/rubocop/cop/gusto/graphql/prevent_field_can_can_action.rb
|
|
175
175
|
- lib/rubocop/cop/gusto/graphql/resolver_ignores_object.rb
|
|
176
|
+
- lib/rubocop/cop/gusto/graphql/resource_from_context.rb
|
|
176
177
|
- lib/rubocop/cop/gusto/graphql/type_variables.rb
|
|
177
178
|
- lib/rubocop/cop/gusto/hardcoded_absent_record_id.rb
|
|
178
179
|
- lib/rubocop/cop/gusto/hardcoded_id.rb
|
|
@@ -228,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
228
229
|
requirements:
|
|
229
230
|
- - ">="
|
|
230
231
|
- !ruby/object:Gem::Version
|
|
231
|
-
version: '3.
|
|
232
|
+
version: '3.4'
|
|
232
233
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
233
234
|
requirements:
|
|
234
235
|
- - ">="
|