/
analyzer.go
83 lines (70 loc) · 2.03 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
package customizedifffunccontext
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"github.com/bflad/tfproviderlint/passes/helper/schema/customizedifffuncinfo"
"golang.org/x/tools/go/analysis"
)
const analyzerName = "customizedifffunccontext"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: fmt.Sprintf("add missing context.Context parameter to schema.CustomizeDiffFunc"),
Requires: []*analysis.Analyzer{
customizedifffuncinfo.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
infos := pass.ResultOf[customizedifffuncinfo.Analyzer].([]*schema.CustomizeDiffFuncInfo)
for _, info := range infos {
// Existing: func(*schema.ResourceDiff, interface {}) error
// New: func(context.Context, *schema.ResourceDiff, interface {}) error
newParam := &ast.Field{
Names: []*ast.Ident{
{
Name: "ctx",
},
},
Type: &ast.SelectorExpr{
Sel: &ast.Ident{
Name: "Context",
},
X: &ast.Ident{
Name: "context",
},
},
}
info.Type.Params.List = append([]*ast.Field{newParam}, info.Type.Params.List...)
var newAstBuf bytes.Buffer
// Only write the FuncType because rewriting loses comments
if err := format.Node(&newAstBuf, pass.Fset, info.Type); err != nil {
return nil, fmt.Errorf("error formatting new: %s", err)
}
newText := newAstBuf.Bytes()
// Keep function name for FuncDecl
if info.AstFuncDecl != nil {
newText = append(newText[:4], append([]byte(" "+info.AstFuncDecl.Name.Name), newText[4:]...)...)
}
pass.Report(analysis.Diagnostic{
Pos: info.Type.Pos(),
End: info.Type.End(),
Message: fmt.Sprintf("%s: add missing context.Context parameter to schema.CustomizeDiffFunc", analyzerName),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "Replace",
TextEdits: []analysis.TextEdit{
{
Pos: info.Type.Pos(),
End: info.Type.End(),
NewText: newText,
},
},
},
},
})
}
return nil, nil
}