rubocop-graphql 1.4.0 → 1.5.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: 5dc015328dbeffe70db24816b19cc9252244e5311ce2957ceb970a08a6812132
4
- data.tar.gz: 5763888eb52a54c529d02e2f7f01560002c5172cace12ea440b85e64e7a0898d
3
+ metadata.gz: b6b8cbd4a0d9dec84fd66e8fd76716d4d23d1a6ce486c713a951cd6c375255dd
4
+ data.tar.gz: 9f7b85517392c031a54e63d9cbd28280cf41d9afb890f67f8e4054fc0704e553
5
5
  SHA512:
6
- metadata.gz: d5e22e52e4b9e4c090dcec11933bb2fa8e22134c49963088828c9352190e0f4b834ede4e4f280402e904ad8c099db987645b689107860571172898c52e1c61b0
7
- data.tar.gz: 284ef533642cdaf70e75dda615e77406eba375d607827f40c59bbdc47e714d5b0be724bfc710189ccc211a9f3a5cf558eb38ee161bc81c559285db88a12df0d8
6
+ metadata.gz: 3474232c9c512d5b86d3c8fd43b790562b01ac458786e96c3c34a1556f530390fff21bd55a7805c382278b43d6c13cb336307b987ce070cbab01788e3c6e5194
7
+ data.tar.gz: f8c5fba9247ad3ce42131b4bd3ee8dfd7f623eb896ca7cce9fb6e0f6ad29c6d38ce616f877e0a5d92dedc8a7701110cd37334fd9af1cb042bcc130285fa2b7d6
data/config/default.yml CHANGED
@@ -134,6 +134,8 @@ GraphQL/ObjectDescription:
134
134
  VersionAdded: '0.3.0'
135
135
  Description: 'Ensures all types have a description'
136
136
  Exclude:
137
+ - "spec/**/*"
138
+ - "test/**/*"
137
139
  - '**/*_schema.rb'
138
140
  - '**/base_*.rb'
139
141
  - '**/graphql/query_context.rb'
@@ -63,9 +63,16 @@ module RuboCop
63
63
  end
64
64
 
65
65
  def field_name(node)
66
- node.first_argument.value.to_s
66
+ field = RuboCop::GraphQL::Field.new(node)
67
+
68
+ "#{field.name}#{':non-camelized' if false_value?(field.kwargs.camelize)}"
67
69
  end
68
70
 
71
+ # @!method false_value?(node)
72
+ def_node_matcher :false_value?, <<~PATTERN
73
+ (pair ... false)
74
+ PATTERN
75
+
69
76
  # @!method field_declarations(node)
70
77
  def_node_search :field_declarations, <<~PATTERN
71
78
  {
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module GraphQL
8
+ # Checks that GraphQL Argument definitions prepare arguments use String or constants
9
+ # instead of `prepare: CONST_REF`
10
+ # This allows better Sorbet typing.
11
+ #
12
+ # Preference is given to an input object declaring a `def prepare(...); end` method
13
+ #
14
+ # @example
15
+ # # bad
16
+ # PREPARE = ->(input, context) { ... }
17
+ #
18
+ # argument :input, prepare: PREPARE
19
+ #
20
+ # # good
21
+ # def input_prepare(input); ...; end
22
+ #
23
+ # argument :input, prepare: :input_prepare
24
+ #
25
+ class PrepareMethod < RuboCop::Cop::Base
26
+ extend AutoCorrector
27
+ GENERAL_MSG = "Avoid using prepare lambdas, use prepare: :method_name or "\
28
+ "prepare: \"method_name\" instead."
29
+ STRING_MSG = "Avoid using prepare lambdas, use prepare: \"method_name\" instead."
30
+ PREFER_STRING_MSG = "Avoid using prepare symbols, use prepare: \"%s\" instead."
31
+ SYMBOL_MSG = "Avoid using prepare lambdas, use prepare: :method_name instead."
32
+ PREFER_SYMBOL_MSG = "Avoid using prepare string, use prepare: :%s instead."
33
+
34
+ ARGUMENT_METHODS = Set[:argument, :public_argument].freeze
35
+
36
+ # @!method graphql_argument_with_prepare_block?(node)
37
+ def_node_matcher :graphql_argument_with_prepare_block?, <<~PATTERN
38
+ (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $({ const | block } ...) )))
39
+ PATTERN
40
+
41
+ # @!method prepare_method_string_name?(node)
42
+ def_node_matcher :prepare_method_string_name?, <<~PATTERN
43
+ (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(str ...) )))
44
+ PATTERN
45
+
46
+ # @!method prepare_method_symbol_name?(node)
47
+ def_node_matcher :prepare_method_symbol_name?, <<~PATTERN
48
+ (send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(sym ...) )))
49
+ PATTERN
50
+
51
+ def on_send(node)
52
+ type = cop_config["EnforcedStyle"]
53
+
54
+ message = case type
55
+ when "symbol"
56
+ SYMBOL_MSG
57
+ when "string"
58
+ STRING_MSG
59
+ else
60
+ GENERAL_MSG
61
+ end
62
+ graphql_argument_with_prepare_block?(node) do |prepare_definition|
63
+ add_offense(prepare_definition, message: message)
64
+ end
65
+
66
+ if type == "symbol"
67
+ prepare_method_string_name?(node) do |prepare_definition|
68
+ method_name = prepare_definition.value
69
+ add_offense(prepare_definition,
70
+ message: format(PREFER_SYMBOL_MSG, method_name)) do |corrector|
71
+ autocorrect(corrector, node, "\"#{method_name}\"", ":#{method_name}")
72
+ end
73
+ end
74
+ elsif type == "string"
75
+ prepare_method_symbol_name?(node) do |prepare_definition|
76
+ method_name = prepare_definition.value
77
+ add_offense(prepare_definition,
78
+ message: format(PREFER_STRING_MSG, method_name)) do |corrector|
79
+ autocorrect(corrector, node, ":#{method_name}", "\"#{method_name}\"")
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def autocorrect(corrector, node, original_method_name, new_method_name)
88
+ new_source = node.source.sub("prepare: #{original_method_name}",
89
+ "prepare: #{new_method_name}")
90
+ corrector.replace(node, new_source)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  if (unnecessary_kwarg = validate_kwargs(field))
36
36
  message = format(self.class::MSG, kwarg: unnecessary_kwarg)
37
37
  add_offense(node, message: message) do |corrector|
38
- kwarg_node = node.arguments.last.pairs.find do |pair|
38
+ kwarg_node = node.last_argument.pairs.find do |pair|
39
39
  pair.key.value == unnecessary_kwarg.to_sym
40
40
  end
41
41
  corrector.remove_preceding(kwarg_node, 2)
@@ -132,7 +132,7 @@ module RuboCop
132
132
 
133
133
  add_offense(node, message: message) do |corrector|
134
134
  if node.arguments?
135
- corrector.insert_after(arg_end(node.arguments.last), ", #{unresolved_args_source}")
135
+ corrector.insert_after(arg_end(node.last_argument), ", #{unresolved_args_source}")
136
136
  else
137
137
  corrector.insert_after(method_name(node), "(#{unresolved_args_source})")
138
138
  end
@@ -21,6 +21,7 @@ require_relative "graphql/resolver_method_length"
21
21
  require_relative "graphql/object_description"
22
22
  require_relative "graphql/ordered_arguments"
23
23
  require_relative "graphql/ordered_fields"
24
+ require_relative "graphql/prepare_method"
24
25
  require_relative "graphql/unused_argument"
25
26
  require_relative "graphql/unnecessary_argument_camelize"
26
27
  require_relative "graphql/unnecessary_field_alias"
@@ -12,7 +12,7 @@ module RuboCop
12
12
  (block
13
13
  (send nil? :argument ...)
14
14
  (args ...)
15
- $...
15
+ {(begin $...)|$...}
16
16
  )
17
17
  PATTERN
18
18
 
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "1.4.0".freeze
3
+ VERSION = "1.5.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Tsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-31 00:00:00.000000000 Z
11
+ date: 2024-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,6 +104,7 @@ files:
104
104
  - lib/rubocop/cop/graphql/object_description.rb
105
105
  - lib/rubocop/cop/graphql/ordered_arguments.rb
106
106
  - lib/rubocop/cop/graphql/ordered_fields.rb
107
+ - lib/rubocop/cop/graphql/prepare_method.rb
107
108
  - lib/rubocop/cop/graphql/resolver_method_length.rb
108
109
  - lib/rubocop/cop/graphql/unnecessary_argument_camelize.rb
109
110
  - lib/rubocop/cop/graphql/unnecessary_field_alias.rb