-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewriter.go
192 lines (169 loc) · 4.89 KB
/
rewriter.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package listEntryNonNullablePatcher
import (
"fmt"
"go/ast"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
)
var Analyzer = &analysis.Analyzer{
Name: "listEntryNonNullablePatcher",
Doc: "Add ListEntryNonNullable schemabuilder option to fieldfuncs that returns a list with pointer entries",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
RunDespiteErrors: true,
}
// fileLine encodes a location as a combination of a file name and a line number.
type fileLine struct {
FileName string
LineNumber int
}
func rewrite(pass *analysis.Pass, callExpr *ast.CallExpr) {
newText := ",schemabuilder.ListEntryNonNullable"
// If the last ")" is not at the same line as the last argument, we don't need the "," prefix.
if pass.Fset.Position(callExpr.Rparen).Line != pass.Fset.Position(callExpr.Args[len(callExpr.Args)-1].End()).Line {
newText = "schemabuilder.ListEntryNonNullable"
}
pass.Report(analysis.Diagnostic{
Pos: callExpr.Rparen,
Message: "Fix",
SuggestedFixes: []analysis.SuggestedFix{
{
TextEdits: []analysis.TextEdit{
{
Pos: callExpr.Rparen,
End: callExpr.Rparen,
NewText: []byte(newText),
},
},
},
},
})
}
func unwrapNamed(typ types.Type) types.Type {
if named, isNamed := typ.(*types.Named); isNamed {
return unwrapNamed(named.Underlying())
}
return typ
}
func unwrapIdent(typ ast.Expr) ast.Expr {
if ident, ok := typ.(*ast.Ident); ok {
// If the response type is a custom type.
if ident.Obj != nil {
if typeSpec, ok := ident.Obj.Decl.(*ast.TypeSpec); ok {
return unwrapIdent(typeSpec.Type)
}
}
}
return typ
}
func run(pass *analysis.Pass) (interface{}, error) {
fmt.Println("hello")
callFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
inspect.Nodes(callFilter, func(node ast.Node, push bool) bool {
if !push {
return true
}
callExpr, ok := node.(*ast.CallExpr)
if !ok {
return true
}
invocation, ok := typeutil.Callee(pass.TypesInfo, callExpr).(*types.Func)
if !ok {
return true
}
signature, ok := invocation.Type().(*types.Signature)
if !ok {
return true
}
receiver := signature.Recv()
if receiver == nil {
return true
}
receiverType := receiver.Type().String()
// Check if it's a FieldFunc.
if !strings.HasSuffix(receiverType, ".Object") || len(callExpr.Args) <= 1 {
return true
}
if invocation.Name() != "FieldFunc" &&
invocation.Name() != "BatchFieldFunc" &&
invocation.Name() != "BatchFieldFuncWithFallback" &&
invocation.Name() != "ManualPaginationWithFallback" {
return true
}
for _, arg := range callExpr.Args {
if sel, ok := arg.(*ast.SelectorExpr); ok {
if sel.Sel.Name == "ListEntryNonNullable" {
return true
}
}
}
// If the fieldfunc is a method of a struct.
if fieldFunc, ok := callExpr.Args[1].(*ast.SelectorExpr); ok {
if sel, ok := pass.TypesInfo.Selections[fieldFunc]; ok {
if sig, ok := sel.Type().(*types.Signature); ok {
if sig.Results().Len() > 0 {
resultType := unwrapNamed(sig.Results().At(0).Type())
// BatchFieldFunc: map[index][]*object.
if mapType, ok := resultType.(*types.Map); ok {
if slice, ok := unwrapNamed(mapType.Elem()).(*types.Slice); ok {
if _, isPtr := unwrapNamed(slice.Elem()).(*types.Pointer); isPtr {
rewrite(pass, callExpr)
}
}
}
// Fieldfunc: []*object.
if slice, ok := resultType.(*types.Slice); ok {
elemType := unwrapNamed(slice.Elem())
if _, isPtr := elemType.(*types.Pointer); isPtr {
rewrite(pass, callExpr)
}
}
}
}
}
return true
}
var funcType *ast.FuncType
if fieldFunc, ok := callExpr.Args[1].(*ast.FuncLit); ok {
// If the fieldfunc is an anonymous function.
funcType = fieldFunc.Type
} else if fieldFunc, ok := callExpr.Args[1].(*ast.Ident); ok {
// If the fieldfunc is a normal function.
if fieldFunc.Obj != nil && fieldFunc.Obj.Decl != nil {
if funcDecl, ok := fieldFunc.Obj.Decl.(*ast.FuncDecl); ok {
funcType = funcDecl.Type
}
}
}
if funcType == nil {
return true
}
if len(funcType.Results.List) > 0 {
resultType := unwrapIdent(funcType.Results.List[0].Type)
var sliceType *ast.ArrayType
var isSlice bool
if mapType, ok := resultType.(*ast.MapType); ok {
// BatchFieldFunc: map[index][]*object.
sliceType, isSlice = unwrapIdent(mapType.Value).(*ast.ArrayType)
} else {
// FieldFunc: []*object.
sliceType, isSlice = resultType.(*ast.ArrayType)
}
if isSlice {
eltType := unwrapIdent(sliceType.Elt)
if _, isPtr := eltType.(*ast.StarExpr); isPtr {
rewrite(pass, callExpr)
}
}
}
return true
})
return nil, nil
}