/
main.go
56 lines (50 loc) · 1.34 KB
/
main.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
package main
import (
"context"
"fmt"
"go/ast"
"strings"
"github.com/cretz/superpose"
)
func main() {
superpose.RunMain(
context.Background(),
superpose.Config{
Version: superpose.MustLoadCurrentExeContentID(),
Transformers: map[string]superpose.Transformer{"tests-simple": transformer{}},
Verbose: true,
},
superpose.RunMainConfig{},
)
}
type transformer struct{}
func (transformer) AppliesToPackage(ctx *superpose.TransformContext, pkgPath string) (bool, error) {
return strings.HasPrefix(pkgPath, "github.com/cretz/superpose/tests/simple"), nil
}
func (transformer) Transform(
ctx *superpose.TransformContext,
pkg *superpose.TransformPackage,
) (*superpose.TransformResult, error) {
// Change any ReturnString function to return "foo"
res := &superpose.TransformResult{
AddLineDirectives: true,
LogPatchedFiles: true,
}
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
// Add patch if it's the func we want
decl, _ := decl.(*ast.FuncDecl)
if decl == nil || decl.Name.Name != "ReturnString" {
continue
}
res.Patches = append(res.Patches, &superpose.Patch{
Range: superpose.Range{Pos: decl.Body.Lbrace + 1, End: decl.Body.Rbrace},
Str: fmt.Sprintf(
` return "foo" /*line :%v*/`,
pkg.Fset.Position(decl.Body.Rbrace).Line,
),
})
}
}
return res, nil
}