Skip to content

Commit

Permalink
Merge pull request #48 from gostaticanalysis/add-packages-template
Browse files Browse the repository at this point in the history
Add packages template
  • Loading branch information
tenntenn authored Mar 22, 2022
2 parents 7f3de02 + ec5c494 commit 94188b5
Show file tree
Hide file tree
Showing 52 changed files with 1,071 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testandvet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
go-version: 1.18.x

- name: Checkout code
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gostaticanalysis/skeleton/v2
go 1.16

require (
github.com/gostaticanalysis/skeletonkit v0.2.0
github.com/gostaticanalysis/skeletonkit v0.4.0
github.com/tenntenn/golden v0.2.0
golang.org/x/mod v0.4.2
)
4 changes: 4 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gostaticanalysis/skeletonkit v0.2.0 h1:DKvDs7HENrSZA+p/tiwRqioeIwVy5Hsgsf1QGupgDt8=
github.com/gostaticanalysis/skeletonkit v0.2.0/go.mod h1:SwFszYU8h5yWQE9rrWlpEiHXqa6zBl8M+y0kqvYpJX4=
github.com/gostaticanalysis/skeletonkit v0.3.1 h1:hd93Bzc0w9SMcOc65BcYCLZiOwOsvqr+vsU2bpbL3kk=
github.com/gostaticanalysis/skeletonkit v0.3.1/go.mod h1:UcRLvBDmeN7krfn6gM2W58XEA5IzjHxtX1NLGR3p2CM=
github.com/gostaticanalysis/skeletonkit v0.4.0 h1:dHR48OSGfrbA6G36W5wd6TuY6w6Gooi4hRrvxa5uH9o=
github.com/gostaticanalysis/skeletonkit v0.4.0/go.mod h1:UcRLvBDmeN7krfn6gM2W58XEA5IzjHxtX1NLGR3p2CM=
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6 h1:c+ctPFdISggaSNCfU1IueNBAsqetJSvMcpQlT+0OVdY=
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6/go.mod h1:Rv/momJI8DgrWnBZip+SgagpcgORIZQE5SERlxNb8LY=
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca h1:a8xeK4GsWLE4LYo5VI4u1Cn7ZvT1NtXouXR3DdKLB8Q=
Expand Down
42 changes: 0 additions & 42 deletions v2/skeleton/_template/@@.Pkg@@_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,87 +1,3 @@
@@ if eq .Kind "inspect" -@@
package @@.Pkg@@

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const doc = "@@.Pkg@@ is ..."

// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "@@.Pkg@@",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.Ident)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.Ident:
if n.Name == "gopher" {
pass.Reportf(n.Pos(), "identifier is gopher")
}
}
})

return nil, nil
}
@@ end -@@
@@ if eq .Kind "ssa" -@@
package @@.Pkg@@

import (
"fmt"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
)

const doc = "@@.Pkg@@ is ..."

// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "@@.Pkg@@",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
s := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
for _, f := range s.SrcFuncs {
fmt.Println(f)
for _, b := range f.Blocks {
fmt.Printf("\tBlock %d\n", b.Index)
for _, instr := range b.Instrs {
fmt.Printf("\t\t%[1]T\t%[1]v(%[1]p)\n", instr)
for _, v := range instr.Operands(nil) {
if v != nil {
fmt.Printf("\t\t\t%[1]T\t%[1]v(%[1]p)\n", *v)
}
}
}
}
}
return nil, nil
}
@@ end -@@
@@ if eq .Kind "codegen" -@@
package @@.Pkg@@

import (
Expand Down Expand Up @@ -196,4 +112,3 @@ func (m *Mock{{$tn}}) {{$n}}({{range $f.Signature.Params}}
{{end}}
{{end}}
`
@@ end -@@
23 changes: 23 additions & 0 deletions v2/skeleton/_template/codegen/@@.Pkg@@_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package @@.Pkg@@_test

import (
"flag"
"os"
"testing"

"@@.Path@@"
"github.com/gostaticanalysis/codegen/codegentest"
)

var flagUpdate bool

func TestMain(m *testing.M) {
flag.BoolVar(&flagUpdate, "update", false, "update the golden files")
flag.Parse()
os.Exit(m.Run())
}

func TestGenerator(t *testing.T) {
rs := codegentest.Run(t, codegentest.TestData(), @@.Pkg@@.Generator, "a")
codegentest.Golden(t, rs, flagUpdate)
}
File renamed without changes.
12 changes: 12 additions & 0 deletions v2/skeleton/_template/codegen/cmd/@@.Pkg@@/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@@ if .Cmd -@@
package main

import (
"@@.Path@@"
"github.com/gostaticanalysis/codegen/@@.Checker@@generator"
)

func main() {
@@.Checker@@generator.Main(@@.Pkg@@.Generator)
}
@@end@@
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@@ if eq .Kind "codegen" -@@
// Code generated by @@.Pkg@@; DO NOT EDIT.
package a

Expand Down Expand Up @@ -27,4 +26,3 @@ func (m *MockLogger) Errorf(format string, args ...interface{}) {
func (m *MockLogger) Infof(format string, args ...interface{}) {
m.InfofFunc(format, args...)
}
@@ end -@@
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
@@ if (or (eq .Kind "inspect") (eq .Kind "ssa")) -@@
package a

func f() {
// The pattern can be written in regular expression.
var gopher int // want "pattern"
print(gopher) // want "identifier is gopher"
}
@@ end -@@
@@ if eq .Kind "codegen" -@@
package a

type DB interface {
Expand All @@ -24,4 +14,3 @@ type Logger interface {
Infof(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
@@ end -@@
40 changes: 40 additions & 0 deletions v2/skeleton/_template/inspect/@@.Pkg@@.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package @@.Pkg@@

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const doc = "@@.Pkg@@ is ..."

// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "@@.Pkg@@",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.Ident)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.Ident:
if n.Name == "gopher" {
pass.Reportf(n.Pos(), "identifier is gopher")
}
}
})

return nil, nil
}
15 changes: 15 additions & 0 deletions v2/skeleton/_template/inspect/@@.Pkg@@_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package @@.Pkg@@_test

import (
"testing"

"@@.Path@@"
"github.com/gostaticanalysis/testutil"
"golang.org/x/tools/go/analysis/analysistest"
)

// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, @@.Pkg@@.Analyzer, "a")
}
1 change: 1 addition & 0 deletions v2/skeleton/_template/inspect/@@gomod@@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@@gomodinit .Path@@
10 changes: 10 additions & 0 deletions v2/skeleton/_template/inspect/cmd/@@.Pkg@@/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@@ if .Cmd -@@
package main

import (
"@@.Path@@"
"golang.org/x/tools/go/analysis/@@.Checker@@checker"
)

func main() { @@.Checker@@checker.Main(@@.Pkg@@.Analyzer) }
@@end@@
File renamed without changes.
1 change: 1 addition & 0 deletions v2/skeleton/_template/inspect/testdata/src/a/@@gomod@@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@@gomodinit "a"@@
7 changes: 7 additions & 0 deletions v2/skeleton/_template/inspect/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package a

func f() {
// The pattern can be written in regular expression.
var gopher int // want "pattern"
print(gopher) // want "identifier is gopher"
}
60 changes: 60 additions & 0 deletions v2/skeleton/_template/packages/@@.Pkg@@.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package @@.Pkg@@

import (
"flag"
"fmt"
"go/ast"
"io"
"path/filepath"

"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/packages"
)

type Pass struct {
Pkg *packages.Package
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}

var Analyzer = struct {
Name string
Doc string
Flags *flag.FlagSet
Config *packages.Config
Run func(pass *Pass) error
}{
Name: "@@.Pkg@@",
Doc: "@@.Pkg@@ is ...",
Config: &packages.Config{
Mode: packages.NeedName | packages.NeedTypes |
packages.NeedSyntax | packages.NeedTypesInfo |
packages.NeedModule,
},
Run: run,
}

func run(pass *Pass) error {
inspect := inspector.New(pass.Pkg.Syntax)

nodeFilter := []ast.Node{
(*ast.Ident)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.Ident:
if n.Name == "gopher" {
pos := pass.Pkg.Fset.Position(n.Pos())
fname, err := filepath.Rel(pass.Pkg.Module.Dir, pos.Filename)
if err != nil {
return
}
fmt.Fprintf(pass.Stdout, "%s:%d:%d identifier is gopher\n", fname, pos.Line, pos.Column)
}
}
})

return nil
}
Loading

0 comments on commit 94188b5

Please sign in to comment.