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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67638fefeb268ea870db034bc46363245b644ec8df5f620878e8e1c90b681975
4
- data.tar.gz: b378c1830557ccbf00b7be144b6ac6141c7246e60b848d91b14a5cc8eee86a2a
3
+ metadata.gz: a3be1c838260c7be6902b6c06cf411ee768c39dab704b078d0204d8d1fa24fc5
4
+ data.tar.gz: 1061e5a30067f55254d5a89e87ff6a1597ac8df321e6be74b06a937ddd8da1e1
5
5
  SHA512:
6
- metadata.gz: e9dbfcab7cdb4820b9ab7d1a9425bb1378bcc279aebb82116058398f9b66b97d90c6af5edf38dd0c94d079f57395ffc7055936e00d89ec98c72e606b4c4d955a
7
- data.tar.gz: 0dd262d908e7de3cbb2fc8b57aab421a7e32214f42322c1bdeab03f43b96072e349e2d0165158df123e78cf0700b9bebc95acb4876a7b5ba655c0fa44eb566f6
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
@@ -13,3 +13,4 @@ require_relative "graphql/field_method"
13
13
  require_relative "graphql/field_name"
14
14
  require_relative "graphql/resolver_method_length"
15
15
  require_relative "graphql/object_description"
16
+ require_relative "graphql/ordered_fields"
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.4.1".freeze
3
+ VERSION = "0.5.0".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.4.1
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-08-17 00:00:00.000000000 Z
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