rubocop-graphql 0.3.1 → 0.4.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: 1c659875c5f0ff36b73bb44becbdbf2b0db912979041ff09f090fa3a265dfe9a
4
- data.tar.gz: a76e1b6a142e23ec20a0d1936e28dc1e626436691700d2a704c275f1f4d7b134
3
+ metadata.gz: 5be7099abfc014351e097467d9dfe7c687d4a0323241f7ce592e0abe25f6b0cb
4
+ data.tar.gz: da4e69f7810f416873d3f3f0742b70758a3c66c343bbd0d2572c223605156c77
5
5
  SHA512:
6
- metadata.gz: 1ee99d3d6d3876effb01e6f4f67db2a9f1a700dd991957705f95c6608ffde97709069af1a75417baebaefa16f32c83f6fec157a649d469cd3ab9aa4e99554f7a
7
- data.tar.gz: 1d2ceb8a083f36bb141addbc43a22b273048dc3abbeacd2891bbb4709d4558ac3f4a524e80fc07bd56609281d734c35f386f3e28c2255868fb9876f0ad95231f
6
+ metadata.gz: ea0737a86c403dbd57b5777c221ce80ec86e97c6fce499d00f573ef7adeb15f37e2db7b2c8f6275ff7aa58e1a17b12472a748cdf06f26c0411cb9943075386fd
7
+ data.tar.gz: c0b0235d1035b9b77ffaaac3ee419b4e240c9eb7363033f37f9396a028930e33f6d11d3145f95c708e68a0b2f7566de0e1cf96264619eb1da0c3626e99304a0b
@@ -35,6 +35,11 @@ GraphQL/FieldDescription:
35
35
  VersionAdded: '0.80'
36
36
  Description: 'Ensures all fields have a description'
37
37
 
38
+ GraphQL/FieldHashKey:
39
+ Enabled: true
40
+ VersionAdded: '0.80'
41
+ Description: 'Checks :hash_key option is used for appropriate fields'
42
+
38
43
  GraphQL/FieldMethod:
39
44
  Enabled: true
40
45
  VersionAdded: '0.80'
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module GraphQL
6
+ # This cop prevents defining unnecessary resolver methods in cases
7
+ # when :hash_key option can be used
8
+ #
9
+ # @example
10
+ # # good
11
+ #
12
+ # class Types::UserType < Types::BaseObject
13
+ # field :phone, String, null: true, hash_key: :home_phone
14
+ # end
15
+ #
16
+ # # bad
17
+ #
18
+ # class Types::UserType < Types::BaseObject
19
+ # field :phone, String, null: true
20
+ #
21
+ # def phone
22
+ # object[:home_phone]
23
+ # end
24
+ # end
25
+ #
26
+ class FieldHashKey < Cop
27
+ include RuboCop::GraphQL::NodePattern
28
+ include RuboCop::Cop::RangeHelp
29
+
30
+ def_node_matcher :hash_key_to_use, <<~PATTERN
31
+ (def
32
+ _
33
+ (args)
34
+ (send
35
+ (send nil? :object) :[]
36
+ (_type $_)
37
+ )
38
+ )
39
+ PATTERN
40
+
41
+ MSG = "Use hash_key: %<hash_key>p"
42
+
43
+ def on_send(node)
44
+ return unless field_definition?(node)
45
+
46
+ field = RuboCop::GraphQL::Field.new(node)
47
+ method_definition = resolver_method_definition_for(field)
48
+
49
+ if (suggested_hash_key_name = hash_key_to_use(method_definition))
50
+ add_offense(node, message: message(suggested_hash_key_name))
51
+ end
52
+ end
53
+
54
+ def autocorrect(node)
55
+ lambda do |corrector|
56
+ field = RuboCop::GraphQL::Field.new(node)
57
+ method_definition = resolver_method_definition_for(field)
58
+ suggested_hash_key_name = hash_key_to_use(method_definition)
59
+
60
+ corrector.insert_after(node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}")
61
+
62
+ range = range_with_surrounding_space(range: method_definition.loc.expression, side: :left)
63
+ corrector.remove(range)
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def message(hash_key)
70
+ format(MSG, hash_key: hash_key)
71
+ end
72
+
73
+ def resolver_method_definition_for(field)
74
+ method_name = field.resolver_method_name
75
+ field.schema_member.find_method_definition(method_name)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -8,6 +8,7 @@ require_relative "graphql/extract_input_type"
8
8
  require_relative "graphql/extract_type"
9
9
  require_relative "graphql/field_definitions"
10
10
  require_relative "graphql/field_description"
11
+ require_relative "graphql/field_hash_key"
11
12
  require_relative "graphql/field_method"
12
13
  require_relative "graphql/field_name"
13
14
  require_relative "graphql/resolver_method_length"
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
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.3.1
4
+ version: 0.4.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: 2020-07-26 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,7 @@ files:
84
84
  - lib/rubocop/cop/graphql/extract_type.rb
85
85
  - lib/rubocop/cop/graphql/field_definitions.rb
86
86
  - lib/rubocop/cop/graphql/field_description.rb
87
+ - lib/rubocop/cop/graphql/field_hash_key.rb
87
88
  - lib/rubocop/cop/graphql/field_method.rb
88
89
  - lib/rubocop/cop/graphql/field_name.rb
89
90
  - lib/rubocop/cop/graphql/object_description.rb