-
Notifications
You must be signed in to change notification settings - Fork 14
/
template.go
7 lines (4 loc) · 8.52 KB
/
template.go
1
2
3
4
5
6
7
// Code generated by _tools/txtar/main.go; DO NOT EDIT.
package main
import "text/template"
var tmpl = template.Must(template.New("template").Delims(`@@`, `@@`).Parse("-- @@.Pkg@@.go --\n@@ if eq .Type \"inspect\" -@@\npackage @@.Pkg@@\n\nimport (\n\t\"go/ast\"\n\n\t\"golang.org/x/tools/go/analysis\"\n\t\"golang.org/x/tools/go/analysis/passes/inspect\"\n\t\"golang.org/x/tools/go/ast/inspector\"\n)\n\nconst doc = \"@@.Pkg@@ is ...\"\n\n// Analyzer is ...\nvar Analyzer = &analysis.Analyzer{\n\tName: \"@@.Pkg@@\",\n\tDoc: doc,\n\tRun: run,\n\tRequires: []*analysis.Analyzer{\n\t\tinspect.Analyzer,\n\t},\n}\n\nfunc run(pass *analysis.Pass) (interface{}, error) {\n\tinspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)\n\n\tnodeFilter := []ast.Node{\n\t\t(*ast.Ident)(nil),\n\t}\n\n\tinspect.Preorder(nodeFilter, func(n ast.Node) {\n\t\tswitch n := n.(type) {\n\t\tcase *ast.Ident:\n\t\t\tif n.Name == \"gopher\" {\n\t\t\t\tpass.Reportf(n.Pos(), \"identifier is gopher\")\n\t\t\t}\n\t\t}\n\t})\n\n\treturn nil, nil\n}\n@@ end -@@\n@@ if eq .Type \"ssa\" -@@\npackage @@.Pkg@@\n\nimport (\n\t\"fmt\"\n\n\t\"golang.org/x/tools/go/analysis\"\n\t\"golang.org/x/tools/go/analysis/passes/buildssa\"\n)\n\nconst doc = \"@@.Pkg@@ is ...\"\n\n// Analyzer is ...\nvar Analyzer = &analysis.Analyzer{\n\tName: \"@@.Pkg@@\",\n\tDoc: doc,\n\tRun: run,\n\tRequires: []*analysis.Analyzer{\n\t\tbuildssa.Analyzer,\n\t},\n}\n\nfunc run(pass *analysis.Pass) (interface{}, error) {\n\ts := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)\n\tfor _, f := range s.SrcFuncs {\n\t\tfmt.Println(f)\n\t\tfor _, b := range f.Blocks {\n\t\t\tfmt.Printf(\"\\tBlock %d\\n\", b.Index)\n\t\t\tfor _, instr := range b.Instrs {\n\t\t\t\tfmt.Printf(\"\\t\\t%[1]T\\t%[1]v(%[1]p)\\n\", instr)\n\t\t\t\tfor _, v := range instr.Operands(nil) {\n\t\t\t\t\tif v != nil {\n\t\t\t\t\t\tfmt.Printf(\"\\t\\t\\t%[1]T\\t%[1]v(%[1]p)\\n\", *v)\n\t\t\t\t\t}\n \t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn nil, nil\n}\n@@ end -@@\n@@ if eq .Type \"codegen\" -@@\npackage @@.Pkg@@\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"go/format\"\n\t\"go/types\"\n\t\"os\"\n\n\t\"github.com/gostaticanalysis/analysisutil\"\n\t\"github.com/gostaticanalysis/codegen\"\n\t\"github.com/gostaticanalysis/knife\"\n)\n\nconst doc = \"@@.Pkg@@ is ...\"\n\nvar (\n\tflagOutput string\n)\n\nfunc init() {\n\tGenerator.Flags.StringVar(&flagOutput, \"o\", \"\", \"output file name\")\n}\n\nvar Generator = &codegen.Generator{\n\tName: \"@@.Pkg@@\",\n\tDoc: doc,\n\tRun: run,\n}\n\nfunc run(pass *codegen.Pass) error {\n\tifaces := map[string]*knife.Interface{}\n\n\ts := pass.Pkg.Scope()\n\tfor _, name := range s.Names() {\n\t\tobj := s.Lookup(name)\n\t\tif !obj.Exported() {\n\t\t\tcontinue\n\t\t}\n\t\tiface, _ := analysisutil.Under(obj.Type()).(*types.Interface)\n\t\tif iface != nil {\n\t\t\tifaces[name] = knife.NewInterface(iface)\n\t\t}\n\t}\n\n\ttd := &knife.TempalteData{\n\t\tFset: pass.Fset,\n\t\tFiles: pass.Files,\n\t\tTypesInfo: pass.TypesInfo,\n\t\tPkg: pass.Pkg,\n\t}\n\tt, err := knife.NewTemplate(td).Parse(tmpl)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar buf bytes.Buffer\n\tif err := t.Execute(&buf, ifaces); err != nil {\n\t\treturn err\n\t}\n\n\tsrc, err := format.Source(buf.Bytes())\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif flagOutput == \"\" {\n\t\tpass.Print(string(src))\n\t\treturn nil\n\t}\n\n\tf, err := os.Create(flagOutput)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfmt.Fprint(f, string(src))\n\n\tif err := f.Close(); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nvar tmpl = `// Code generated by @@.Pkg@@; DO NOT EDIT.\npackage {{(pkg).Name}}\n{{range $tn, $t := .}}\ntype Mock{{$tn}} struct {\n{{- range $n, $f := $t.Methods}}\n {{$n}}Func {{$f.Signature}}\n{{- end}}\n}\n{{range $n, $f := $t.Methods}}\nfunc (m *Mock{{$tn}}) {{$n}}({{range $f.Signature.Params}}\n\t{{- if (and $f.Signature.Variadic (eq . (last $f.Signature.Params)))}}\n \t{{- .Name}} ...{{(slice .Type).Elem}},\n\t{{- else}}\n \t{{- .Name}} {{.Type}},\n\t{{- end}}\n{{- end}}) ({{range $f.Signature.Results}}\n {{- .Name}} {{.Type}},\n{{- end}}) {\n {{if $f.Signature.Results}}return {{end}}m.{{$n}}Func({{range $f.Signature.Params}}\n\t\t{{- if (and $f.Signature.Variadic (eq . (last $f.Signature.Params)))}}\n \t\t{{- .Name}}...,\n\t\t{{- else}}\n \t\t{{- .Name}},\n\t\t{{- end}}\n {{- end}})\n}\n{{end}}\n{{end}}\n`\n@@ end -@@\n-- @@.Pkg@@_test.go --\n@@ if (or (eq .Type \"inspect\") (eq .Type \"ssa\")) -@@\npackage @@.Pkg@@_test\n\nimport (\n\t\"testing\"\n\n\t\"@@.ImportPath@@\"\n\t\"github.com/gostaticanalysis/testutil\"\n\t\"golang.org/x/tools/go/analysis/analysistest\"\n)\n\n// TestAnalyzer is a test for Analyzer.\nfunc TestAnalyzer(t *testing.T) {\n\ttestdata := testutil.WithModules(t, analysistest.TestData(), nil)\n\tanalysistest.Run(t, testdata, @@.Pkg@@.Analyzer, \"a\")\n}\n@@ end -@@\n@@ if eq .Type \"codegen\" -@@\npackage @@.Pkg@@_test\n\nimport (\n\t\"flag\"\n\t\"os\"\n\t\"testing\"\n\n\t\"@@.ImportPath@@\"\n\t\"github.com/gostaticanalysis/codegen/codegentest\"\n)\n\nvar flagUpdate bool\n\nfunc TestMain(m *testing.M) {\n\tflag.BoolVar(&flagUpdate, \"update\", false, \"update the golden files\")\n\tflag.Parse()\n\tos.Exit(m.Run())\n}\n\nfunc TestGenerator(t *testing.T) {\n\trs := codegentest.Run(t, codegentest.TestData(), @@.Pkg@@.Generator, \"a\")\n\tcodegentest.Golden(t, rs, flagUpdate)\n}\n@@ end -@@\n-- cmd/@@.Pkg@@/main.go --\n@@ if .Cmd -@@\n@@ if (or (eq .Type \"inspect\") (eq .Type \"ssa\")) -@@\npackage main\n\nimport (\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis/@@.Checker@@checker\"\n)\n\nfunc main() { @@.Checker@@checker.Main(@@.Pkg@@.Analyzer) }\n@@ end -@@\n@@ if eq .Type \"codegen\" -@@\npackage main\n\nimport (\n\t\"@@.ImportPath@@\"\n\t\"github.com/gostaticanalysis/codegen/@@.Checker@@generator\"\n)\n\nfunc main() {\n\t@@.Checker@@generator.Main(@@.Pkg@@.Generator)\n}\n@@ end -@@\n@@end@@\n-- go.mod --\n@@ if .Mod -@@\nmodule @@.ImportPath@@\n\ngo @@.GoVer@@\n@@end@@\n-- plugin/main.go --\n@@ if .Plugin -@@\n// This file can build as a plugin for golangci-lint by below command.\n// go build -buildmode=plugin -o path_to_plugin_dir @@.ImportPath@@/plugin/@@.Pkg@@\n// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint\n\npackage main\n\nimport (\n\t\"strings\"\n\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis\"\n)\n\n// flags for Analyzer.Flag.\n// If you would like to specify flags for your plugin, you can put them via 'ldflags' as below.\n// $ go build -buildmode=plugin -ldflags \"-X 'main.flags=-opt val'\" @@.ImportPath@@/plugin/@@.Pkg@@\nvar flags string\n\n// AnalyzerPlugin provides analyzers as a plugin.\n// It follows golangci-lint style plugin.\nvar AnalyzerPlugin analyzerPlugin\n\ntype analyzerPlugin struct{}\n\nfunc (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {\n\tif flags != \"\" {\n\t\tflagset := @@.Pkg@@.Analyzer.Flags\n\t\tif err := flagset.Parse(strings.Split(flags, \" \")); err != nil {\n\t\t\tpanic(\"cannot parse flags of @@.Pkg@@: \" + err.Error())\n\t\t}\n\t}\n\treturn []*analysis.Analyzer{\n\t\t@@.Pkg@@.Analyzer,\n\t}\n}\n@@end@@\n-- testdata/src/a/@@.Pkg@@.golden --\n@@ if eq .Type \"codegen\" -@@\n// Code generated by @@.Pkg@@; DO NOT EDIT.\npackage a\n\ntype MockDB struct {\n\tGetFunc func(id string) int\n\tSetFunc func(id string, v int)\n}\n\nfunc (m *MockDB) Get(id string) int {\n\treturn m.GetFunc(id)\n}\n\nfunc (m *MockDB) Set(id string, v int) {\n\tm.SetFunc(id, v)\n}\n\ntype MockLogger struct {\n\tErrorfFunc func(format string, args ...interface{})\n\tInfofFunc func(format string, args ...interface{})\n}\n\nfunc (m *MockLogger) Errorf(format string, args ...interface{}) {\n\tm.ErrorfFunc(format, args...)\n}\n\nfunc (m *MockLogger) Infof(format string, args ...interface{}) {\n\tm.InfofFunc(format, args...)\n}\n@@ end -@@\n-- testdata/src/a/a.go --\n@@ if (or (eq .Type \"inspect\") (eq .Type \"ssa\")) -@@\npackage a\n\nfunc f() {\n\t// The pattern can be written in regular expression.\n\tvar gopher int // want \"pattern\"\n\tprint(gopher) // want \"identifier is gopher\"\n}\n@@ end -@@\n@@ if eq .Type \"codegen\" -@@\npackage a\n\ntype DB interface {\n\tGet(id string) int\n\tSet(id string, v int)\n}\n\ntype db struct{}\n\nfunc (db) Get(id string) int { return 0 }\nfunc (db) Set(id string, v int) {}\n\ntype Logger interface {\n\tInfof(format string, args ...interface{})\n\tErrorf(format string, args ...interface{})\n}\n@@ end -@@\n-- testdata/src/a/go.mod --\n@@ if ne .Type \"codegen\" -@@\nmodule a\n\ngo @@.GoVer@@\n@@ end -@@\n"))