rubocop-mable 0.1.3 → 0.1.5
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/Gemfile.lock +2 -1
- data/README.md +27 -9
- data/config/default.yml +22 -6
- data/lib/rubocop/cop/mable/no_post_in_graph_ql.rb +97 -0
- data/lib/rubocop/cop/mable/no_warden_in_graph_ql.rb +91 -0
- data/lib/rubocop/cop/mable_cops.rb +2 -0
- data/lib/rubocop/mable/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b59a5fc99bec56362c41b540f37058dabfc1e16b3ac9bb6db72c64f5f16495f
|
4
|
+
data.tar.gz: 60135942aab111fa27f46225f839edbcca834e97e37737cc303774496c1febfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97a1ae86f7a089288e6444b50ac1eccfb5e04457c4155b5bab6f34c0a8388ff36528e9f0dba95130c574525af2eca8d0cf8a72ab09c7b249d7054b05985a2ce4
|
7
|
+
data.tar.gz: f527513322bef9827c28b64100e3eae9c413ec559b77b049579cc4683923b54a4ed2756159cdb289d2370c49957bc144d78efc06c9964f6acc628c28035ca581
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,20 +5,38 @@ Mable's custom Rubocop cops
|
|
5
5
|
## Cops
|
6
6
|
|
7
7
|
```
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
Mable/NoWardenInGraphQL:
|
10
|
+
Description: "No Warden in graphQL use helper method instead see - Mable/NoPostInGraphQL"
|
10
11
|
Enabled: true
|
11
|
-
|
12
|
+
SafeAutoCorrect: false
|
13
|
+
VersionAdded: "0.1.5"
|
14
|
+
|
15
|
+
Mable/NoPostInGraphQL:
|
16
|
+
Description: "Use graphQL helper method instead of use rails/rack request stack"
|
17
|
+
Enabled: true
|
18
|
+
VersionAdded: "0.1.4"
|
19
|
+
ReplacePostWith: "make_graphql_request"
|
20
|
+
SafeAutoCorrect: false
|
21
|
+
AllowedGraphQLPaths:
|
22
|
+
- "/graphql"
|
23
|
+
- " graphql_path"
|
24
|
+
|
25
|
+
Mable/NoSafetyAssured:
|
26
|
+
Description: 'An extra check to ensure that the safety_assured is required'
|
27
|
+
Enabled: true
|
28
|
+
VersionAdded: '0.1.3'
|
12
29
|
|
13
30
|
Mable/GraphQLHelperSpecs:
|
14
|
-
|
15
|
-
|
16
|
-
|
31
|
+
Description: 'Avoid hardcoding GraphQl path use helper instead.'
|
32
|
+
Enabled: true
|
33
|
+
VersionAdded: '0.1.2'
|
17
34
|
|
18
35
|
Mable/HardcodedDatabaseFactoryBotId:
|
19
|
-
|
20
|
-
|
21
|
-
|
36
|
+
Enabled: true
|
37
|
+
Description: 'Avoid hardcoding factory bot database IDs, instead, dynamically test for the ID'
|
38
|
+
VersionAdded: '0.1.1'
|
39
|
+
|
22
40
|
```
|
23
41
|
|
24
42
|
## Installation
|
data/config/default.yml
CHANGED
@@ -1,14 +1,30 @@
|
|
1
1
|
Mable/GraphQLHelperSpecs:
|
2
|
-
Description:
|
2
|
+
Description: "Avoid hardcoding GraphQl path use helper instead."
|
3
3
|
Enabled: true
|
4
|
-
VersionAdded:
|
4
|
+
VersionAdded: "0.1.2"
|
5
5
|
|
6
6
|
Mable/HardcodedDatabaseFactoryBotId:
|
7
7
|
Enabled: true
|
8
|
-
Description:
|
9
|
-
VersionAdded:
|
8
|
+
Description: "Avoid hardcoding factory bot database IDs, instead, dynamically test for the ID"
|
9
|
+
VersionAdded: "0.1.1"
|
10
10
|
|
11
11
|
Mable/NoSafetyAssured:
|
12
|
-
Description:
|
12
|
+
Description: "An extra check to ensure that the safety_assured is required"
|
13
13
|
Enabled: true
|
14
|
-
VersionAdded:
|
14
|
+
VersionAdded: "0.1.3"
|
15
|
+
|
16
|
+
Mable/NoPostInGraphQL:
|
17
|
+
Description: "Use graphQL helper method instead of use rails/rack request stack"
|
18
|
+
Enabled: true
|
19
|
+
VersionAdded: "0.1.4"
|
20
|
+
ReplacePostWith: "make_graphql_request"
|
21
|
+
SafeAutoCorrect: false
|
22
|
+
AllowedGraphQLPaths:
|
23
|
+
- "/graphql"
|
24
|
+
- "graphql_path"
|
25
|
+
|
26
|
+
Mable/NoWardenInGraphQL:
|
27
|
+
Description: "No Warden in graphQL use helper method instead see - Mable/NoPostInGraphQL"
|
28
|
+
Enabled: true
|
29
|
+
SafeAutoCorrect: false
|
30
|
+
VersionAdded: "0.1.5"
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Mable
|
6
|
+
# Enforces using a direct helper method over `post` for executing GraphQL queries,
|
7
|
+
# incorporating user authentication context when applicable.
|
8
|
+
#
|
9
|
+
# @safety
|
10
|
+
# The cop is unsafe as user might not exist or not be called user.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
#
|
14
|
+
# # bad
|
15
|
+
# post graphql_path, params: { query: my_query, variables: my_variables }, as: :json
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# execute_graphql(query: my_query, variables: my_variables, user: current_user)
|
19
|
+
#
|
20
|
+
class NoPostInGraphQL < Base
|
21
|
+
extend AutoCorrector
|
22
|
+
|
23
|
+
MSG = "Use 'ReplacePostWith' default: `make_graphql_request` directly instead of `post` for GraphQL requests, incorporating user context."
|
24
|
+
|
25
|
+
RESTRICT_ON_SEND = %i[post].freeze
|
26
|
+
|
27
|
+
def_node_matcher :post_graphql?, <<~PATTERN
|
28
|
+
(send nil? :post
|
29
|
+
({str | send nil?} $_) # Optional first argument (string path)
|
30
|
+
(hash
|
31
|
+
(pair
|
32
|
+
(sym :params)
|
33
|
+
(hash
|
34
|
+
(pair (sym :query) _)
|
35
|
+
(pair (sym :variables) _)? # Optional :variables pair
|
36
|
+
)
|
37
|
+
)
|
38
|
+
)
|
39
|
+
)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def_node_search :rspec_block?, <<~PATTERN
|
43
|
+
(block (send (const nil? :RSpec) {:describe :context :it} ...) ...)
|
44
|
+
PATTERN
|
45
|
+
|
46
|
+
def on_send(node)
|
47
|
+
post_graphql?(node) do |path|
|
48
|
+
handle_post_graphql(node) if graphql_path_allowed?(path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def graphql_path_allowed?(path)
|
55
|
+
allowed_paths = cop_config['AllowedGraphQLPaths'] || []
|
56
|
+
allowed_paths.include?(path.to_s)
|
57
|
+
end
|
58
|
+
|
59
|
+
def handle_post_graphql(node)
|
60
|
+
return unless within_rspec_block?(node)
|
61
|
+
|
62
|
+
# Check if the method is `post` and it includes `params:`
|
63
|
+
params_pair = node.arguments.find do |arg|
|
64
|
+
arg.hash_type? && arg.pairs.any? { |pair| pair.key.sym_type? && pair.key.value == :params }
|
65
|
+
end
|
66
|
+
|
67
|
+
return unless params_pair
|
68
|
+
|
69
|
+
# Access pairs directly from params_pair
|
70
|
+
params_hash = params_pair.pairs.find { |pair| pair.key.value == :params }.value
|
71
|
+
|
72
|
+
return unless params_hash.hash_type?
|
73
|
+
|
74
|
+
query_pair = params_hash.pairs.find { |p| p.key.value == :query }
|
75
|
+
variables_pair = params_hash.pairs.find { |p| p.key.value == :variables }
|
76
|
+
|
77
|
+
query = query_pair&.value&.source
|
78
|
+
variables = variables_pair&.value&.source
|
79
|
+
|
80
|
+
replacement = cop_config['ReplacePostWith']
|
81
|
+
execute_call = "#{replacement}(query: #{query}"
|
82
|
+
execute_call += ', user: user'
|
83
|
+
execute_call += ", variables: #{variables}" if variables
|
84
|
+
execute_call += ')'
|
85
|
+
|
86
|
+
add_offense(node) do |corrector|
|
87
|
+
corrector.replace(node.loc.expression, execute_call)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def within_rspec_block?(node)
|
92
|
+
node.each_ancestor.any? { |ancestor| rspec_block?(ancestor) }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Mable
|
6
|
+
# This cop checks for the usage of Warden::Test::Helpers and related
|
7
|
+
# methods within GraphQL specs. It disallows the use of Warden.test_mode!,
|
8
|
+
# login_as, and user.reload.
|
9
|
+
#
|
10
|
+
# @safety
|
11
|
+
# Unsafe as requires graphql helper method to replace
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# # bad
|
15
|
+
# Warden.test_mode!
|
16
|
+
# login_as(user)
|
17
|
+
# user.reload
|
18
|
+
# include Warden::Test::Helpers
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# # Use graphql helper method
|
22
|
+
class NoWardenInGraphQL < Base
|
23
|
+
include RangeHelp
|
24
|
+
extend AutoCorrector
|
25
|
+
|
26
|
+
MSG = 'Do not use `%<method>s` in GraphQL specs. Use graphql helper method instead.'
|
27
|
+
|
28
|
+
RESTRICT_ON_SEND = %i[test_mode! login_as reload].freeze
|
29
|
+
RESTRICT_ON_INCLUDE = %i[Warden::Test::Helpers].freeze
|
30
|
+
|
31
|
+
def_node_search :rspec_describe_graphql?, <<~PATTERN
|
32
|
+
(block
|
33
|
+
(send (const nil? :RSpec) :describe
|
34
|
+
(const { (const nil? {:Graphql :Queries :Mutations}) } _)
|
35
|
+
...)
|
36
|
+
...)
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
def_node_matcher :user_reload?, <<~PATTERN
|
40
|
+
(send
|
41
|
+
(send _ :user) :reload)
|
42
|
+
PATTERN
|
43
|
+
|
44
|
+
def on_send(node)
|
45
|
+
return unless in_graphql?(node) && bad_method?(node)
|
46
|
+
|
47
|
+
method_name = node.method_name
|
48
|
+
add_offense(node, message: format(MSG, method: method_name)) do |corrector|
|
49
|
+
correct_offense(corrector, node)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def on_const(node)
|
54
|
+
return unless in_graphql?(node) && bad_include?(node)
|
55
|
+
|
56
|
+
add_offense(node, message: format(MSG, method: 'include Warden::Test::Helpers')) do |corrector|
|
57
|
+
correct_offense(corrector, node)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def in_graphql?(node)
|
64
|
+
node.each_ancestor(:block).any? do |ancestor|
|
65
|
+
rspec_describe_graphql?(ancestor)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def bad_method?(node)
|
70
|
+
if node.method_name == :reload
|
71
|
+
user_reload?(node)
|
72
|
+
else
|
73
|
+
RESTRICT_ON_SEND.include?(node.method_name)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def bad_include?(node)
|
78
|
+
RESTRICT_ON_INCLUDE.include?(node.const_name.to_sym)
|
79
|
+
end
|
80
|
+
|
81
|
+
def correct_offense(corrector, node)
|
82
|
+
if node.send_type? && bad_method?(node)
|
83
|
+
corrector.remove(range_with_comments_and_lines(node))
|
84
|
+
elsif node.const_type? && node.const_name == 'Warden::Test::Helpers'
|
85
|
+
corrector.remove(range_with_comments_and_lines(node.parent))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-mable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mable Engineers
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -59,7 +59,9 @@ files:
|
|
59
59
|
- lib/rubocop-mable.rb
|
60
60
|
- lib/rubocop/cop/mable/graph_ql_helper_specs.rb
|
61
61
|
- lib/rubocop/cop/mable/hardcoded_database_factory_bot_id.rb
|
62
|
+
- lib/rubocop/cop/mable/no_post_in_graph_ql.rb
|
62
63
|
- lib/rubocop/cop/mable/no_safety_assured.rb
|
64
|
+
- lib/rubocop/cop/mable/no_warden_in_graph_ql.rb
|
63
65
|
- lib/rubocop/cop/mable_cops.rb
|
64
66
|
- lib/rubocop/mable.rb
|
65
67
|
- lib/rubocop/mable/inject.rb
|
@@ -73,7 +75,7 @@ metadata:
|
|
73
75
|
homepage_uri: https://github.com/rubocop-mable
|
74
76
|
source_code_uri: https://github.com/bettercaring/rubocop-mable
|
75
77
|
rubygems_mfa_required: 'true'
|
76
|
-
post_install_message:
|
78
|
+
post_install_message:
|
77
79
|
rdoc_options: []
|
78
80
|
require_paths:
|
79
81
|
- lib
|
@@ -89,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
91
|
version: '0'
|
90
92
|
requirements: []
|
91
93
|
rubygems_version: 3.1.6
|
92
|
-
signing_key:
|
94
|
+
signing_key:
|
93
95
|
specification_version: 4
|
94
96
|
summary: Mable's custom rubocop cops
|
95
97
|
test_files: []
|