-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from gostaticanalysis/add-packages-template
Add packages template
- Loading branch information
Showing
52 changed files
with
1,071 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@@gomodinit .Path@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@@gomodinit "a"@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.