rubocop-graphql 0.10.1 → 0.11.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: 306f0b68587158302fed968f58c2696c5cfcbd65a1a762ef8d80c99ced67915e
4
- data.tar.gz: 73e0308c1bd1a159264a34372c92d8bc8b8814aa60f872ca38df91e1b16f845d
3
+ metadata.gz: 660e5888a933adf211a4c140882cd9bab799e0f0bcaa545f7ec1bbe9668e064f
4
+ data.tar.gz: efca9c85b37c7a91d46844ef7f574dc434ffffbb1a01adfcefc3a31c71cf2d02
5
5
  SHA512:
6
- metadata.gz: d0c6d84f77b000fde31fe670666710c50b7609d5f947553a41bd50534e2e1e66415449a9fdaa750dd6570cc6be48c0b4f759c5532bd7753a50b61d2023df0001
7
- data.tar.gz: ba226bf80d8bd6a52b95813dc20e5aff871b01bf71b39da9f0f9937fbb7449729ea889e95f9dc2a2939ebb72e4434524c386bebc9da3755ea77ff6487c878fc3
6
+ metadata.gz: b51da944eeca0ff3be527823992860e9c690bf8dca86eab7800e0d694cd212654bdc614c5e849516bf7afabbb36c36c1da8cdcb7bb490c84f66606d79439dab5
7
+ data.tar.gz: acd228993fe068085136b68e21d872ac52db7ddd7dd36e7ee047d95b2f7fe406db2af8de01a51275b023d45a29bf4a9b0eca60d824fcfb31742e3115b0703bb8
data/config/default.yml CHANGED
@@ -12,6 +12,11 @@ GraphQL/ArgumentName:
12
12
  VersionAdded: '0.80'
13
13
  Description: 'This cop checks whether argument names are snake_case'
14
14
 
15
+ GraphQL/ArgumentUniqueness:
16
+ Enabled: true
17
+ VersionAdded: '0.80'
18
+ Description: 'This cop enforces arguments to be defined once per block'
19
+
15
20
  GraphQL/ResolverMethodLength:
16
21
  Enabled: true
17
22
  VersionAdded: '0.80'
@@ -50,6 +55,11 @@ GraphQL/FieldName:
50
55
  Description: 'This cop checks whether field names are snake_case'
51
56
  SafeAutoCorrect: false
52
57
 
58
+ GraphQL/FieldUniqueness:
59
+ Enabled: true
60
+ VersionAdded: '0.80'
61
+ Description: 'This cop enforces fields to be defined once'
62
+
53
63
  GraphQL/ExtractInputType:
54
64
  Enabled: true
55
65
  VersionAdded: '0.80'
@@ -80,3 +90,15 @@ GraphQL/ObjectDescription:
80
90
  Exclude:
81
91
  - '**/*_schema.rb'
82
92
  - '**/base_*.rb'
93
+
94
+ GraphQL/OrderedArguments:
95
+ Enabled: true
96
+ VersionAdded: '0.80'
97
+ Description: 'Arguments should be alphabetically sorted within groups'
98
+
99
+ GraphQL/OrderedFields:
100
+ Enabled: true
101
+ VersionAdded: '0.80'
102
+ Description: 'Fields should be alphabetically sorted within groups'
103
+
104
+
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module GraphQL
6
+ # This cop detects duplicate argument definitions
7
+ #
8
+ # @example
9
+ # # good
10
+ #
11
+ # class BanUser < BaseMutation
12
+ # argument :user_id, ID, required: true
13
+ # end
14
+ #
15
+ # # bad
16
+ #
17
+ # class BanUser < BaseMutation
18
+ # argument :user_id, ID, required: true
19
+ # argument :user_id, ID, required: true
20
+ # end
21
+ #
22
+ class ArgumentUniqueness < Base
23
+ MSG = "Argument names should only be defined once per block. "\
24
+ "Argument `%<current>s` is duplicated%<field_name>s."
25
+
26
+ def on_class(node)
27
+ global_argument_names = Set.new
28
+ argument_names_by_field = {}
29
+
30
+ argument_declarations(node).each do |current|
31
+ current_field_name = field_name(current)
32
+ current_argument_name = argument_name(current)
33
+
34
+ if current_field_name
35
+ argument_names_by_field[current_field_name] ||= Set.new
36
+ argument_names = argument_names_by_field[current_field_name]
37
+ else
38
+ argument_names = global_argument_names
39
+ end
40
+
41
+ unless argument_names.include?(current_argument_name)
42
+ argument_names.add(current_argument_name)
43
+ next
44
+ end
45
+
46
+ register_offense(current)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def register_offense(current)
53
+ current_field_name = field_name(current)
54
+ field_name_message = " in field `#{current_field_name}`" if current_field_name
55
+
56
+ message = format(
57
+ self.class::MSG,
58
+ current: argument_name(current),
59
+ field_name: field_name_message
60
+ )
61
+
62
+ add_offense(current, message: message)
63
+ end
64
+
65
+ def argument_name(node)
66
+ node.first_argument.value.to_s
67
+ end
68
+
69
+ # Find parent field block, if available
70
+ def field_name(node)
71
+ return if node.nil?
72
+
73
+ is_field_block = node.block_type? &&
74
+ node.respond_to?(:method_name) &&
75
+ node.method_name == :field
76
+ return node.send_node.first_argument.value.to_s if is_field_block
77
+
78
+ field_name(node.parent)
79
+ end
80
+
81
+ def_node_search :argument_declarations, <<~PATTERN
82
+ (send nil? :argument (:sym _) ...)
83
+ PATTERN
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module GraphQL
6
+ # This cop detects duplicate field definitions within
7
+ # the same type
8
+ #
9
+ # @example
10
+ # # good
11
+ #
12
+ # class UserType < BaseType
13
+ # field :name, String, null: true
14
+ # field :phone, String, null: true do
15
+ # argument :something, String, required: false
16
+ # end
17
+ # end
18
+ #
19
+ # # bad
20
+ #
21
+ # class UserType < BaseType
22
+ # field :name, String, null: true
23
+ # field :phone, String, null: true
24
+ # field :phone, String, null: true do
25
+ # argument :something, String, required: false
26
+ # end
27
+ # end
28
+ #
29
+ class FieldUniqueness < Base
30
+ MSG = "Field names should only be defined once per type. "\
31
+ "Field `%<current>s` is duplicated."
32
+
33
+ def on_class(node)
34
+ field_names = Set.new
35
+
36
+ field_declarations(node).each do |current|
37
+ current_field_name = field_name(current)
38
+
39
+ unless field_names.include?(current_field_name)
40
+ field_names.add(current_field_name)
41
+ next
42
+ end
43
+
44
+ register_offense(current)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def register_offense(current)
51
+ message = format(
52
+ self.class::MSG,
53
+ current: field_name(current)
54
+ )
55
+
56
+ add_offense(current, message: message)
57
+ end
58
+
59
+ def field_name(node)
60
+ node.first_argument.value.to_s
61
+ end
62
+
63
+ def_node_search :field_declarations, <<~PATTERN
64
+ {
65
+ (send nil? :field (:sym _) ...)
66
+ }
67
+ PATTERN
68
+ end
69
+ end
70
+ end
71
+ end
@@ -59,7 +59,7 @@ module RuboCop
59
59
  def on_class(node)
60
60
  argument_declarations(node).each_cons(2) do |previous, current|
61
61
  next unless consecutive_lines(previous, current)
62
- next if argument_name(current) > argument_name(previous)
62
+ next if argument_name(current) >= argument_name(previous)
63
63
 
64
64
  register_offense(previous, current)
65
65
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module GraphQL
6
- # Field should be alphabetically sorted within groups.
6
+ # Fields should be alphabetically sorted within groups.
7
7
  #
8
8
  # @example
9
9
  # # good
@@ -42,7 +42,7 @@ module RuboCop
42
42
  def on_class(node)
43
43
  field_declarations(node).each_cons(2) do |previous, current|
44
44
  next unless consecutive_lines(previous, current)
45
- next if field_name(current) > field_name(previous)
45
+ next if field_name(current) >= field_name(previous)
46
46
 
47
47
  register_offense(previous, current)
48
48
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "graphql/argument_description"
4
4
  require_relative "graphql/argument_name"
5
+ require_relative "graphql/argument_uniqueness"
5
6
  require_relative "graphql/extract_input_type"
6
7
  require_relative "graphql/extract_type"
7
8
  require_relative "graphql/field_definitions"
@@ -9,6 +10,7 @@ require_relative "graphql/field_description"
9
10
  require_relative "graphql/field_hash_key"
10
11
  require_relative "graphql/field_method"
11
12
  require_relative "graphql/field_name"
13
+ require_relative "graphql/field_uniqueness"
12
14
  require_relative "graphql/legacy_dsl"
13
15
  require_relative "graphql/resolver_method_length"
14
16
  require_relative "graphql/object_description"
@@ -21,7 +21,8 @@ module RuboCop
21
21
  extend RuboCop::NodePattern::Macros
22
22
 
23
23
  def_node_matcher :description_kwarg?, <<~PATTERN
24
- (send nil? :description {({str|dstr} ...)|(send ({str|dstr} ...) _)})
24
+ (send nil? :description
25
+ {({str|dstr|const} ...)|(send const ...)|(send ({str|dstr} ...) _)})
25
26
  PATTERN
26
27
 
27
28
  def find_description_method(nodes)
@@ -11,7 +11,7 @@ module RuboCop
11
11
  (block
12
12
  (send nil? :field ...)
13
13
  (args)
14
- $...
14
+ {(begin $...)|$...}
15
15
  )
16
16
  PATTERN
17
17
 
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.10.1".freeze
3
+ VERSION = "0.11.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: 0.10.1
4
+ version: 0.11.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: 2021-08-19 00:00:00.000000000 Z
11
+ date: 2021-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,7 @@ files:
86
86
  - lib/rubocop-graphql.rb
87
87
  - lib/rubocop/cop/graphql/argument_description.rb
88
88
  - lib/rubocop/cop/graphql/argument_name.rb
89
+ - lib/rubocop/cop/graphql/argument_uniqueness.rb
89
90
  - lib/rubocop/cop/graphql/extract_input_type.rb
90
91
  - lib/rubocop/cop/graphql/extract_type.rb
91
92
  - lib/rubocop/cop/graphql/field_definitions.rb
@@ -93,6 +94,7 @@ files:
93
94
  - lib/rubocop/cop/graphql/field_hash_key.rb
94
95
  - lib/rubocop/cop/graphql/field_method.rb
95
96
  - lib/rubocop/cop/graphql/field_name.rb
97
+ - lib/rubocop/cop/graphql/field_uniqueness.rb
96
98
  - lib/rubocop/cop/graphql/legacy_dsl.rb
97
99
  - lib/rubocop/cop/graphql/object_description.rb
98
100
  - lib/rubocop/cop/graphql/ordered_arguments.rb
@@ -135,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
137
  - !ruby/object:Gem::Version
136
138
  version: '0'
137
139
  requirements: []
138
- rubygems_version: 3.1.2
140
+ rubygems_version: 3.2.31
139
141
  signing_key:
140
142
  specification_version: 4
141
143
  summary: Automatic performance checking tool for Ruby code.