/
attribute.go
55 lines (51 loc) · 1.4 KB
/
attribute.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
package name
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
"github.com/fatih/color"
"github.com/nao1215/goavl/internal/utils/ioutils"
"github.com/nao1215/goavl/internal/utils/strutils"
)
// AttributeNameChecker check argument name.
func AttributeNameChecker(filepath, inspectionID string) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filepath, nil, 0)
if err != nil {
ioutils.Die(err.Error())
}
for _, decl := range f.Decls {
checkAttributeArgName(filepath, inspectionID, fset, decl)
}
}
func checkAttributeArgName(filepath, inspectionID string, fset *token.FileSet, decl ast.Decl) {
switch d := decl.(type) {
case *ast.GenDecl:
ast.Inspect(d, func(node ast.Node) bool {
switch node := node.(type) {
case *ast.ExprStmt:
if node.X.(*ast.CallExpr).Fun.(*ast.Ident).Name == "Attribute" {
for _, arg := range node.X.(*ast.CallExpr).Args {
switch bl := arg.(type) {
case *ast.BasicLit:
firstArg := strings.Replace(bl.Value, "\"", "", -1)
if !strutils.IsSnakeCase(firstArg) {
fmt.Fprintf(os.Stderr,
"[%s] %s:%-4d Attribute(\"%s\") is not snake case ('%s')\n",
color.YellowString(inspectionID),
filepath,
fset.Position(node.X.(*ast.CallExpr).Fun.(*ast.Ident).NamePos).Line,
firstArg,
strutils.ToSnakeCase(firstArg))
}
}
}
}
}
return true
})
}
}