rubocop-graphql 1.4.0 → 1.5.1

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: 1307db3b7122e5a576ca03bbaecbb5798b03a927718aa85fa25fd5a9afe1ec07
4
+ data.tar.gz: ea58763eb067a3e576149ebe41830a7ca7210e080e61382011fab9707ceaf2b9
5
5
  SHA512:
6
- metadata.gz: d5e22e52e4b9e4c090dcec11933bb2fa8e22134c49963088828c9352190e0f4b834ede4e4f280402e904ad8c099db987645b689107860571172898c52e1c61b0
7
- data.tar.gz: 284ef533642cdaf70e75dda615e77406eba375d607827f40c59bbdc47e714d5b0be724bfc710189ccc211a9f3a5cf558eb38ee161bc81c559285db88a12df0d8
6
+ metadata.gz: 86d2c4c34918bb8713b15c4485b681d62b8b8c74b894aefbfb4d12e1d364ef2eff48c6c5562ca5b10690195b8f8403dacac73552b0cdfa2960f64fde07b63e3c
7
+ data.tar.gz: 784d752ff412289d3eeeabdd204939bef5ad946e6cd5f40c8cc2ccfd1ebe4b7ec657f8ebe44c3d14fc7d8b50508891bc9b945f732b1b25c1a9dff6d6751ce87f
data/config/default.yml CHANGED
@@ -128,12 +128,15 @@ GraphQL/ResolverMethodLength:
128
128
  Max: 10
129
129
  CountComments: false
130
130
  ExcludedMethods: []
131
+ CountAsOne: []
131
132
 
132
133
  GraphQL/ObjectDescription:
133
134
  Enabled: true
134
135
  VersionAdded: '0.3.0'
135
136
  Description: 'Ensures all types have a description'
136
137
  Exclude:
138
+ - "spec/**/*"
139
+ - "test/**/*"
137
140
  - '**/*_schema.rb'
138
141
  - '**/base_*.rb'
139
142
  - '**/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
@@ -23,9 +23,11 @@ module RuboCop
23
23
  return if excluded_methods.include?(String(node.method_name))
24
24
 
25
25
  if field_is_defined?(node)
26
- length = code_length(node)
26
+ return if node.line_count <= max_length
27
27
 
28
- return unless length > max_length
28
+ calculator = build_code_length_calculator(node)
29
+ length = calculator.calculate
30
+ return if length <= max_length
29
31
 
30
32
  add_offense(node, message: message(length))
31
33
  end
@@ -34,10 +36,6 @@ module RuboCop
34
36
 
35
37
  private
36
38
 
37
- def code_length(node)
38
- node.source.lines[1..-2].count { |line| !irrelevant_line(line) }
39
- end
40
-
41
39
  def field_is_defined?(node)
42
40
  node.parent
43
41
  .children
@@ -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
 
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  # @!method field_description(node)
35
35
  def_node_matcher :field_description, <<~PATTERN
36
- (send nil? :field _ _ (:str $_) ...)
36
+ (send nil? :field _ _ {(:str $_)|(:dstr $...)} ...)
37
37
  PATTERN
38
38
 
39
39
  attr_reader :node
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "1.4.0".freeze
3
+ VERSION = "1.5.1".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.1
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-03-31 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