From d7c9acf76f9b316d0361d2f7d265ccd353d35d28 Mon Sep 17 00:00:00 2001 From: Takuya Ueda Date: Wed, 11 Mar 2020 04:31:04 +0900 Subject: [PATCH] Fix templates --- template.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/template.go b/template.go index 548d4e4..087b380 100644 --- a/template.go +++ b/template.go @@ -12,17 +12,18 @@ import ( "golang.org/x/tools/go/ast/inspector" ) +const doc = "{{.Pkg}} is ..." + +// Analyzer is ... var Analyzer = &analysis.Analyzer{ Name: "{{.Pkg}}", - Doc: Doc, + Doc: doc, Run: run, Requires: []*analysis.Analyzer{ inspect.Analyzer, }, } -const Doc = "{{.Pkg}} is ..." - func run(pass *analysis.Pass) (interface{}, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) @@ -33,7 +34,9 @@ func run(pass *analysis.Pass) (interface{}, error) { inspect.Preorder(nodeFilter, func(n ast.Node) { switch n := n.(type) { case *ast.Ident: - _ = n + if n.Name == "gopher" { + pass.Reportf(n.Pos(), "identifyer is gopher") + } } }) @@ -50,7 +53,8 @@ import ( "golang.org/x/tools/go/analysis/analysistest" ) -func Test(t *testing.T) { +// TestAnalyzer is a test for Analyzer. +func TestAnalyzer(t *testing.T) { testdata := analysistest.TestData() analysistest.Run(t, testdata, {{.Pkg}}.Analyzer, "a") } @@ -58,8 +62,10 @@ func Test(t *testing.T) { var adotgoTempl = template.Must(template.New("a.go").Parse(`package a -func main() { - // want "pattern" +func f() { + // The pattern can be written in regular expression. + var gopher int // want "pattern" + print(gopher) // want "identifyer is gopher" } `))