-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval.go
76 lines (62 loc) · 1.38 KB
/
eval.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
package goast
import (
"context"
"go/ast"
"go/parser"
"go/token"
"io"
"regexp"
"github.com/m-mizutani/goerr"
)
type failCase struct {
Msg string `json:"msg"`
Pos int `json:"pos"`
Sev string `json:"sev"`
}
type evalOutput struct {
Fail []*failCase `json:"fail"`
}
var generatedCodePattern = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
func isGeneratedFile(file *ast.File) bool {
for _, comment := range file.Comments {
for _, row := range comment.List {
if generatedCodePattern.MatchString(row.Text) {
return true
}
}
}
return false
}
func (x *Goast) Eval(filePath string, r io.Reader) ([]*Fail, error) {
var fails []*Fail
callback := func(data *Node) error {
ctx := context.Background()
var out evalOutput
if err := x.opac.Query(ctx, data, &out); err != nil {
return goerr.Wrap(err)
}
for _, fail := range out.Fail {
pos := data.Pos(token.Pos(fail.Pos))
fails = append(fails, &Fail{
Path: data.Path,
Line: pos.Line,
Column: pos.Column,
Msg: fail.Msg,
Sev: fail.Sev,
})
}
return nil
}
fSet := token.NewFileSet()
f, err := parser.ParseFile(fSet, filePath, r, parser.ParseComments)
if err != nil {
return nil, err
}
if x.ignoreAutoGen && isGeneratedFile(f) {
return nil, nil
}
if err := Inspect(f, fSet, callback, x.inspectOpt...); err != nil {
return nil, err
}
return fails, nil
}