rubocop-graphql 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/graphql/ordered_fields.rb +83 -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: a3be1c838260c7be6902b6c06cf411ee768c39dab704b078d0204d8d1fa24fc5
|
4
|
+
data.tar.gz: 1061e5a30067f55254d5a89e87ff6a1597ac8df321e6be74b06a937ddd8da1e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32e42d5a360bf522df3408bcfbc906e830eb7dcbc46aef53e967567c73f3634908a9cb6e9fb51967e79d1e64be24074067eca0e2870d45137c97b66f2b6354f4
|
7
|
+
data.tar.gz: 83a9f4756bec98ff082561a1cc1b81e64335a914bd343ba9353ccb865f38714219619594b8c6844b020fa04b3202412b5533a6fb01e2cbfb59b2a352d6465f57
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# Field should be alphabetically sorted within groups.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # good
|
10
|
+
#
|
11
|
+
# class UserType < BaseType
|
12
|
+
# field :name, String, null: true
|
13
|
+
# field :phone, String, null: true do
|
14
|
+
# argument :something, String, required: false
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
#
|
20
|
+
# class UserType < BaseType
|
21
|
+
# field :phone, String, null: true
|
22
|
+
#
|
23
|
+
# field :name, String, null: true
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # bad
|
27
|
+
#
|
28
|
+
# class UserType < BaseType
|
29
|
+
# field :phone, String, null: true
|
30
|
+
# field :name, String, null: true
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
class OrderedFields < Cop
|
34
|
+
MSG = "Fields should be sorted in an alphabetical order within their "\
|
35
|
+
"section. "\
|
36
|
+
"Field `%<current>s` should appear before `%<previous>s`."
|
37
|
+
|
38
|
+
def investigate(processed_source)
|
39
|
+
return if processed_source.blank?
|
40
|
+
|
41
|
+
field_declarations(processed_source.ast)
|
42
|
+
.each_cons(2) do |previous, current|
|
43
|
+
next unless consecutive_lines(previous, current)
|
44
|
+
next if field_name(current) > field_name(previous)
|
45
|
+
|
46
|
+
register_offense(previous, current)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def register_offense(previous, current)
|
53
|
+
message = format(
|
54
|
+
self.class::MSG,
|
55
|
+
previous: field_name(previous),
|
56
|
+
current: field_name(current)
|
57
|
+
)
|
58
|
+
add_offense(current, message: message)
|
59
|
+
end
|
60
|
+
|
61
|
+
def field_name(node)
|
62
|
+
if node.block_type?
|
63
|
+
field_name(node.send_node)
|
64
|
+
else
|
65
|
+
node.first_argument.value.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def consecutive_lines(previous, current)
|
70
|
+
previous.source_range.last_line == current.source_range.first_line - 1
|
71
|
+
end
|
72
|
+
|
73
|
+
def_node_search :field_declarations, <<~PATTERN
|
74
|
+
{
|
75
|
+
(send nil? :field (:sym _) ...)
|
76
|
+
(block
|
77
|
+
(send nil? :field (:sym _) ...) ...)
|
78
|
+
}
|
79
|
+
PATTERN
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
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.
|
4
|
+
version: 0.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: 2020-
|
11
|
+
date: 2020-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/rubocop/cop/graphql/field_method.rb
|
89
89
|
- lib/rubocop/cop/graphql/field_name.rb
|
90
90
|
- lib/rubocop/cop/graphql/object_description.rb
|
91
|
+
- lib/rubocop/cop/graphql/ordered_fields.rb
|
91
92
|
- lib/rubocop/cop/graphql/resolver_method_length.rb
|
92
93
|
- lib/rubocop/cop/graphql_cops.rb
|
93
94
|
- lib/rubocop/graphql.rb
|