rubocop-mable 0.1.3 → 0.1.4

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: 402b8f356986fdfa523dcecccf1b2fb74f4a9c6ce34c191db74d1b839f00058b
4
- data.tar.gz: 1e671dd68702a88b13eb3b0fe39954a710f5422e3c13d3c331424f2ab5fa9b17
3
+ metadata.gz: 11f4a0980ecdeb9bd5423be381dd7a644af6c9d6b88588ea3c19142103d07f15
4
+ data.tar.gz: 9b7b498e693680865f1c195913feb385c6d125cee1ad92869aaa327b1f6d8e69
5
5
  SHA512:
6
- metadata.gz: c261761ae3726394462842ff37774f47984567e66c3152e7a86a6d960b15c15251a660c5f53e949bb2b4669e76aa1344b4d3eaea82c410d9cc19bbd83a088a19
7
- data.tar.gz: 6d07e483bc9da12e4d7875d12673cbf05401ffaaadb76db3cb42d226d467e7f19bf04bd39d77a19e2a206b8733649dedbbd9e77804691db12c6660ad7ec9ed50
6
+ metadata.gz: 8bd1daf2d6d0e8e11ae71e6a0827c43b3a2e4f2600ad37e5fad6836189eb0ad23bd11db73872a52a07035b944d0cded9bd1d1159bf5bfac9b3fdcfec3a12cc06
7
+ data.tar.gz: 765c60614e41af4a95e45c2136ab5464b68d2ecb5d21ebebab6e3b0dfa2f93737756f659db49893eee21c0bedd35e722fd97350ac853db6373ffff58bc144be0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-mable (0.1.3)
4
+ rubocop-mable (0.1.4)
5
5
  rubocop
6
6
 
7
7
  GEM
@@ -56,6 +56,7 @@ GEM
56
56
 
57
57
  PLATFORMS
58
58
  arm64-darwin-21
59
+ ruby
59
60
 
60
61
  DEPENDENCIES
61
62
  pry
data/README.md CHANGED
@@ -5,20 +5,32 @@ Mable's custom Rubocop cops
5
5
  ## Cops
6
6
 
7
7
  ```
8
+
9
+ Mable/NoPostInGraphQL:
10
+ Description: "Use graphQL helper method instead of use rails/rack request stack"
11
+ Enabled: true
12
+ VersionAdded: "0.1.4"
13
+ ReplacePostWith: "make_graphql_request"
14
+ SafeAutoCorrect: false
15
+ AllowedGraphQLPaths:
16
+ - "/graphql"
17
+ - " graphql_path"
18
+
8
19
  Mable/NoSafetyAssured:
9
- Description: 'An extra check to ensure that the safety_assured is required'
10
- Enabled: true
11
- VersionAdded: '0.1.3'
20
+ Description: 'An extra check to ensure that the safety_assured is required'
21
+ Enabled: true
22
+ VersionAdded: '0.1.3'
12
23
 
13
24
  Mable/GraphQLHelperSpecs:
14
- Description: 'Avoid hardcoding GraphQl path use helper instead.'
15
- Enabled: true
16
- VersionAdded: '0.1.2'
25
+ Description: 'Avoid hardcoding GraphQl path use helper instead.'
26
+ Enabled: true
27
+ VersionAdded: '0.1.2'
17
28
 
18
29
  Mable/HardcodedDatabaseFactoryBotId:
19
- Enabled: true
20
- Description: 'Avoid hardcoding factory bot database IDs, instead, dynamically test for the ID'
21
- VersionAdded: '0.1.1'
30
+ Enabled: true
31
+ Description: 'Avoid hardcoding factory bot database IDs, instead, dynamically test for the ID'
32
+ VersionAdded: '0.1.1'
33
+
22
34
  ```
23
35
 
24
36
  ## Installation
data/config/default.yml CHANGED
@@ -1,14 +1,24 @@
1
1
  Mable/GraphQLHelperSpecs:
2
- Description: 'Avoid hardcoding GraphQl path use helper instead.'
2
+ Description: "Avoid hardcoding GraphQl path use helper instead."
3
3
  Enabled: true
4
- VersionAdded: '0.1.2'
4
+ VersionAdded: "0.1.2"
5
5
 
6
6
  Mable/HardcodedDatabaseFactoryBotId:
7
7
  Enabled: true
8
- Description: 'Avoid hardcoding factory bot database IDs, instead, dynamically test for the ID'
9
- VersionAdded: '0.1.1'
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: 'An extra check to ensure that the safety_assured is required'
12
+ Description: "An extra check to ensure that the safety_assured is required"
13
13
  Enabled: true
14
- VersionAdded: '0.1.3'
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"
@@ -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
@@ -2,4 +2,5 @@
2
2
 
3
3
  require_relative 'mable/graph_ql_helper_specs'
4
4
  require_relative 'mable/hardcoded_database_factory_bot_id'
5
+ require_relative 'mable/no_post_in_graph_ql'
5
6
  require_relative 'mable/no_safety_assured'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Mable
5
- VERSION = '0.1.3'
5
+ VERSION = '0.1.4'
6
6
  end
7
7
  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.3
4
+ version: 0.1.4
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: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2024-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -59,6 +59,7 @@ 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
63
64
  - lib/rubocop/cop/mable_cops.rb
64
65
  - lib/rubocop/mable.rb
@@ -73,7 +74,7 @@ metadata:
73
74
  homepage_uri: https://github.com/rubocop-mable
74
75
  source_code_uri: https://github.com/bettercaring/rubocop-mable
75
76
  rubygems_mfa_required: 'true'
76
- post_install_message:
77
+ post_install_message:
77
78
  rdoc_options: []
78
79
  require_paths:
79
80
  - lib
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  version: '0'
90
91
  requirements: []
91
92
  rubygems_version: 3.1.6
92
- signing_key:
93
+ signing_key:
93
94
  specification_version: 4
94
95
  summary: Mable's custom rubocop cops
95
96
  test_files: []