-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.go
111 lines (99 loc) · 3.25 KB
/
analyzer.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
package directive
import (
"github.com/gqlgo/gqlanalysis"
)
func NewAnalyzers(config *Config) []*gqlanalysis.Analyzer {
analyzers := make([]*gqlanalysis.Analyzer, 0, len(config.Analyzer))
for _, analyzerConfig := range config.Analyzer {
analyzers = append(analyzers, NewAnalyzer(analyzerConfig))
}
return analyzers
}
func NewAnalyzer(analyzerConfig *AnalyzerConfig) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: analyzerConfig.AnalyzerName,
Doc: analyzerConfig.Description,
Run: MergeAnalyzers(analyzerConfig),
}
}
func MergeAnalyzers(analyzerConfig *AnalyzerConfig) func(pass *gqlanalysis.Pass) (any, error) {
var analyzers []*gqlanalysis.Analyzer
for _, c := range analyzerConfig.DefinitionConfig {
analyzer := DefinitionAnalyzer(c)
analyzers = append(analyzers, analyzer)
}
for _, c := range analyzerConfig.FieldConfig {
analyzer := FieldAnalyzer(c)
analyzers = append(analyzers, analyzer)
}
for _, c := range analyzerConfig.ArgumentConfig {
analyzer := ArgumentAnalyzer(c)
analyzers = append(analyzers, analyzer)
}
return func(pass *gqlanalysis.Pass) (any, error) {
for _, analyzer := range analyzers {
if _, err := analyzer.Run(pass); err != nil {
return nil, err
}
}
return nil, nil
}
}
func DefinitionAnalyzer(config *DefinitionConfig) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: config.Directive,
Doc: config.Description,
Run: func(pass *gqlanalysis.Pass) (any, error) {
definitions := NewDefinitionsByMap(pass.Schema.Types).
NotBuildIn().
FilterByKinds(config.Kinds).
ExcludeByDefinitionName(config.ExcludeDefinitionPatterns).
FilterByNotHasDirective(config.Directive).
FilterByPositionNotNil()
for _, definition := range definitions {
pass.Reportf(definition.Position, config.ReportFormat, definition.Name)
}
return nil, nil
},
}
}
func FieldAnalyzer(config *FieldConfig) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: config.Directive,
Doc: config.Description,
Run: func(pass *gqlanalysis.Pass) (any, error) {
definitions := NewDefinitionsByMap(pass.Schema.Types).
NotBuildIn().
FilterByKinds(config.Kinds).
FilterByDefinitionName(config.FieldParentTypePatterns)
fields := definitions.Fields().
FilterByFieldType(config.FieldTypePatterns).
ExcludeByField(config.ExcludeFieldPatterns).
FilterByNotHasDirective(config.Directive).
FilterByPositionNotNil()
for _, field := range fields {
pass.Reportf(field.Position, config.ReportFormat, field.Name)
}
return nil, nil
},
}
}
func ArgumentAnalyzer(config *ArgumentConfig) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: config.Directive,
Doc: config.Description,
Run: func(pass *gqlanalysis.Pass) (any, error) {
definitions := NewDefinitionsByMap(pass.Schema.Types).NotBuildIn().FilterByKinds(config.Kinds)
fields := definitions.Fields().FilterByNotNil()
arguments := fields.Arguments().
FilterByArgumentType(config.ArgumentTypePatterns).
ExcludeByArgumentName(config.ExcludeArgumentPatterns).
FilterByNotHasDirective(config.Directive).
FilterByPositionNotNil()
for _, argument := range arguments {
pass.Reportf(argument.Position, config.ReportFormat, argument.Name)
}
return nil, nil
},
}
}