-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite_test.go
55 lines (52 loc) · 1.02 KB
/
rewrite_test.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 astvisit
import (
"errors"
"fmt"
"go/ast"
"go/token"
"io"
"os"
"path/filepath"
"strings"
)
func ExampleRewrite() {
cwd, _ := os.Getwd()
cwd += string(os.PathSeparator)
// Use verboseWriter to print encountered file paths
err := Rewrite(
"./...",
nil, // verboseOut
nil, // resultOut
func(fset *token.FileSet, filePkg *ast.Package, astFile *ast.File, filePath string, verboseWriter io.Writer) ([]byte, error) {
info, err := os.Stat(filePath)
if err != nil {
return nil, err
}
if info.IsDir() {
return nil, errors.New("listed directory")
}
if filepath.Ext(filePath) != ".go" {
return nil, errors.New("listed non .go file")
}
_, err = fmt.Println(strings.TrimPrefix(filePath, cwd))
return nil, err
})
if err != nil {
panic(err)
}
// Output:
// cursor.go
// errors.go
// nodereplacements.go
// nodetype.go
// packagelocation.go
// parse.go
// path.go
// pathitem.go
// rewrite.go
// tostring.go
// visit.go
// visitor.go
// visitorimpl.go
// test/dummy.go
}