-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathordered_fields.rb
93 lines (81 loc) · 2.51 KB
/
ordered_fields.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
module RuboCop
module Cop
module GraphQL
# Fields should be alphabetically sorted within groups.
#
# @example
# # good
#
# class UserType < BaseType
# field :name, String, null: true
# field :phone, String, null: true do
# argument :something, String, required: false
# end
# end
#
# # good
#
# class UserType < BaseType
# field :phone, String, null: true
#
# field :name, String, null: true
# end
#
# # bad
#
# class UserType < BaseType
# field :phone, String, null: true
# field :name, String, null: true
# end
#
class OrderedFields < Base
extend AutoCorrector
include RuboCop::GraphQL::SwapRange
include RuboCop::GraphQL::CompareOrder
MSG = "Fields should be sorted in an alphabetical order within their "\
"section. "\
"Field `%<current>s` should appear before `%<previous>s`."
# @!method field_declarations(node)
def_node_search :field_declarations, <<~PATTERN
{
(send nil? :field (:sym _) ...)
(block
(send nil? :field (:sym _) ...) ...)
}
PATTERN
def on_class(node)
field_declarations(node).each_cons(2) do |previous, current|
next unless consecutive_fields(previous, current)
next if correct_order?(field_name(previous), field_name(current))
register_offense(previous, current)
end
end
private
def consecutive_fields(previous, current)
return true if cop_config["Groups"] == false
(previous.source_range.last_line == current.source_range.first_line - 1) ||
(previous.parent.block_type? &&
previous.parent.last_line == current.source_range.first_line - 1)
end
def register_offense(previous, current)
message = format(
self.class::MSG,
previous: field_name(previous),
current: field_name(current)
)
add_offense(current, message: message) do |corrector|
swap_range(corrector, current, previous)
end
end
def field_name(node)
if node.block_type?
field_name(node.send_node)
else
node.first_argument.value.to_s
end
end
end
end
end
end