-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.go
More file actions
54 lines (48 loc) · 973 Bytes
/
fs.go
File metadata and controls
54 lines (48 loc) · 973 Bytes
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
package golang
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strings"
)
type SourceDir struct {
Path string
Files int
Pkg bool
}
type SourceFile struct {
Path string
Name string
Dir *SourceDir
AST *ast.File
GoSource bool
Test bool
Error string
}
func (sf *SourceFile) Parse2AST(fset *token.FileSet) {
sf.checkGo()
if !sf.GoSource {
return
}
// TODO: decide whether the comment should be parsed
// TODO: handle error
p := filepath.Join(sf.Path, sf.Name)
parsed, err := parser.ParseFile(fset, p, nil, parser.ParseComments)
// don't stop if current file can't be parsed
if err != nil {
sf.Error = err.Error()
}
sf.AST = parsed
sf.Dir.Pkg = true
}
func (sf *SourceFile) checkGo() {
p := filepath.Join(sf.Path, sf.Name)
// TODO: should we check the content detail of the file?
if strings.HasSuffix(p, ".go") {
if strings.HasSuffix(p, "_test.go") {
sf.Test = true
}
sf.GoSource = true
}
}