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 +4 -4
- data/config/default.yml +5 -0
- data/lib/rubocop/cop/graphql/field_hash_key.rb +80 -0
- data/lib/rubocop/cop/graphql_cops.rb +1 -0
- data/lib/rubocop/graphql/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5be7099abfc014351e097467d9dfe7c687d4a0323241f7ce592e0abe25f6b0cb
|
4
|
+
data.tar.gz: da4e69f7810f416873d3f3f0742b70758a3c66c343bbd0d2572c223605156c77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea0737a86c403dbd57b5777c221ce80ec86e97c6fce499d00f573ef7adeb15f37e2db7b2c8f6275ff7aa58e1a17b12472a748cdf06f26c0411cb9943075386fd
|
7
|
+
data.tar.gz: c0b0235d1035b9b77ffaaac3ee419b4e240c9eb7363033f37f9396a028930e33f6d11d3145f95c708e68a0b2f7566de0e1cf96264619eb1da0c3626e99304a0b
|
data/config/default.yml
CHANGED
@@ -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"
|
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.
|
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-
|
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
|