-
Notifications
You must be signed in to change notification settings - Fork 2
/
lackid.go
142 lines (117 loc) · 2.91 KB
/
lackid.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package lackid
import (
"github.com/vektah/gqlparser/v2/ast"
"github.com/gqlgo/gqlanalysis"
)
var Analyzer = &gqlanalysis.Analyzer{
Name: "lackid",
Doc: "lackid finds a selection for a type which has id field but the selection does not have id",
Run: run,
}
func run(pass *gqlanalysis.Pass) (interface{}, error) {
for _, q := range pass.Queries {
checkOperations(pass, q.Operations)
checkFragments(pass, q.Fragments)
}
return nil, nil
}
func checkOperations(pass *gqlanalysis.Pass, ops ast.OperationList) {
for _, op := range ops {
for _, sel := range op.SelectionSet {
checkField(pass, sel)
checkInlineFragment(pass, sel)
}
}
}
func checkFragments(pass *gqlanalysis.Pass, fs ast.FragmentDefinitionList) {
for _, f := range fs {
checkFragment(pass, f)
}
}
func checkFragment(pass *gqlanalysis.Pass, f *ast.FragmentDefinition) {
if !hasID(f.Definition.Fields) {
return
}
var hasID bool
for _, s := range f.SelectionSet {
hasID = hasID || isID(s)
checkField(pass, s)
}
if hasID {
return
}
name := f.Definition.Name
if name == "" {
name = "fragment"
}
pass.Reportf(f.Position, "type %s has id field but %s does not have id field", name, f.Name)
}
func checkField(pass *gqlanalysis.Pass, sel ast.Selection) {
field, _ := sel.(*ast.Field)
if field == nil || allFragmentSpread(field.SelectionSet) {
return
}
for _, s := range field.SelectionSet {
checkField(pass, s)
checkInlineFragment(pass, s)
}
ft := pass.Schema.Types[field.Definition.Type.Name()]
if ft == nil || !hasID(ft.Fields) {
return
}
var hasID bool
for _, s := range field.SelectionSet {
hasID = hasID || isID(s)
}
if !hasID && !allInlineFragment(field.SelectionSet) {
pass.Reportf(field.Position, "type %s has id field but selection %s does not have id field", ft.Name, field.Name)
}
}
func hasID(fields ast.FieldList) bool {
for _, field := range fields {
if field.Name == "id" {
return true
}
}
return false
}
func isID(sel ast.Selection) bool {
field, _ := sel.(*ast.Field)
return field != nil && field.Name == "id"
}
func checkInlineFragment(pass *gqlanalysis.Pass, sel ast.Selection) {
f, _ := sel.(*ast.InlineFragment)
if f == nil || allFragmentSpread(f.SelectionSet) {
return
}
for _, s := range f.SelectionSet {
checkField(pass, s)
checkInlineFragment(pass, s)
}
if !hasID(f.ObjectDefinition.Fields) {
return
}
var hasID bool
for _, s := range f.SelectionSet {
hasID = hasID || isID(s)
}
if !hasID && !allInlineFragment(f.SelectionSet) {
pass.Reportf(f.Position, "type %s has id field but fragment does not have id field", f.ObjectDefinition.Name)
}
}
func allFragmentSpread(set ast.SelectionSet) bool {
for _, sel := range set {
if _, ok := sel.(*ast.FragmentSpread); !ok {
return false
}
}
return true
}
func allInlineFragment(set ast.SelectionSet) bool {
for _, sel := range set {
if _, ok := sel.(*ast.InlineFragment); !ok {
return false
}
}
return true
}