-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
109 lines (101 loc) · 2.64 KB
/
context.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
package main
import "fmt"
import "regexp"
import "go/ast"
import "go/importer"
import "go/parser"
import "go/token"
import "go/types"
import "os"
import "path/filepath"
// context is the top level oblect representing the task of running
// defimpl for a single go package source directory.
type context struct {
dir string
fset *token.FileSet
info *types.Info
astFiles []*ast.File
files []*File
typeErrors []error
}
// NewContext returns a context for orchestrating defimpl's operations.
// dir should be an absolute path to a go package source directory.
// The go source files in dir will be parsed and File objects added to
// the files field of the new context.
func NewContext(dir string) (*context, error) {
if !filepath.IsAbs(dir) {
return nil, fmt.Errorf("%s is not an absolute path", dir)
}
ctx := &context{dir: dir}
ctx.fset = token.NewFileSet()
ctx.info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
pkgs, err := parser.ParseDir(ctx.fset, dir,
func(fi os.FileInfo) bool {
return !IsOutputFilePath(fi.Name())
}, parser.ParseComments)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
for _, astFile := range pkg.Files {
ctx.astFiles = append(ctx.astFiles, astFile)
}
}
ctx.Check()
for _, astFile := range ctx.astFiles {
// NewFile is where interface definitions are
// processed and VerbPhrases created.
ctx.files = append(ctx.files, NewFile(ctx, astFile))
}
ctx.ReportTypeErrors()
return ctx, nil
}
// Check runs the go type checker on all of the files in ctx.
func (ctx *context) Check() {
conf := types.Config{
Importer: importer.For("source", nil), // importer.Default(),
Error: func(err error) {
ctx.typeErrors = append(ctx.typeErrors, err)
},
}
_, _ = conf.Check(ctx.astFiles[0].Name.Name, ctx.fset, ctx.astFiles, ctx.info)
}
func (ctx *context) ReportTypeErrors() {
ignore := false
for _, err := range ctx.typeErrors {
missing := typeErrorUndeclaredName(err)
if missing != "" {
for _, f := range ctx.files {
for _, idef := range f.Interfaces {
if idef.StructName() == missing {
ignore = true
break
}
}
if ignore {
break
}
}
}
if !ignore {
fmt.Fprintf(os.Stderr, "defimpl error while type checking: %s\n", err)
}
}
}
var typeErrorUndeclaredNameRegexp = regexp.MustCompile(
`^undeclared name: (?P<type>[a-zA-Z_0-9]+)$`)
func typeErrorUndeclaredName(err error) string {
e, ok := err.(types.Error)
if !ok {
return ""
}
m := typeErrorUndeclaredNameRegexp.FindStringSubmatch(e.Msg)
if len(m) > 1 {
return m[1]
}
return ""
}