Skip to content

Commit b061907

Browse files
committed
Initial example
0 parents  commit b061907

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

README.md

Whitespace-only changes.

example.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package linters
2+
3+
import (
4+
"go/ast"
5+
"golang.org/x/tools/go/analysis"
6+
"strings"
7+
)
8+
9+
var TodoAnalyzer = &analysis.Analyzer{
10+
Name: "todo",
11+
Doc: "finds todos without author",
12+
Run: run,
13+
}
14+
15+
func run(pass *analysis.Pass) (interface{}, error) {
16+
for _, file := range pass.Files {
17+
ast.Inspect(file, func(n ast.Node) bool {
18+
if comment, ok := n.(*ast.Comment); ok {
19+
if strings.HasPrefix(comment.Text, "// TODO:") ||
20+
strings.HasPrefix(comment.Text, "// TODO():") {
21+
pass.Report(analysis.Diagnostic{
22+
Pos: comment.Pos(),
23+
End: 0,
24+
Category: "todo",
25+
Message: "TODO comment has no author",
26+
SuggestedFixes: nil,
27+
})
28+
}
29+
}
30+
return true
31+
})
32+
}
33+
return nil, nil
34+
}

go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/dbraley/example-linter
2+
3+
// All versions here need to be the same as in golangci-lint/mod.go if present
4+
5+
go 1.12
6+
7+
require (
8+
github.com/stretchr/testify v1.4.0
9+
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff
10+
)
11+
12+
replace golang.org/x/tools => github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c

go.sum

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c h1:JF7g2hV+1F/DwJ3CrSxOc9ZNVY6N8zYt5mB0Qve//fU=
4+
github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
9+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
10+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
11+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
12+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
14+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
15+
golang.org/x/tools v0.0.0-20191031220737-6d8f1af9ccc0 h1:+o3suEKE/4hCUt6qjV8SDcVZhz2dO8UWlHliCa+4bvg=
16+
golang.org/x/tools v0.0.0-20191031220737-6d8f1af9ccc0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
17+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
18+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
20+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

lint_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package linters
2+
3+
// Tests for linters.
4+
5+
import (
6+
"github.com/stretchr/testify/suite"
7+
"path/filepath"
8+
"runtime"
9+
"testing"
10+
11+
"golang.org/x/tools/go/analysis/analysistest"
12+
)
13+
14+
type linterSuite struct {
15+
suite.Suite
16+
}
17+
18+
func (suite *linterSuite) TestContextLinter() {
19+
analysistest.Run(
20+
suite.T(), TestdataDir(),
21+
TodoAnalyzer, "testlintdata/todo")
22+
}
23+
24+
func TestLinterSuite(t *testing.T) {
25+
suite.Run(t, new(linterSuite))
26+
}
27+
28+
func TestdataDir() string {
29+
_, testFilename, _, ok := runtime.Caller(1)
30+
if !ok {
31+
panic("unable to get current test filename")
32+
}
33+
return filepath.Join(filepath.Dir(testFilename), "testdata")
34+
}

plugin/example.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This must be package main
2+
package main
3+
4+
import (
5+
"fmt"
6+
linters "github.com/dbraley/example-linter"
7+
"golang.org/x/tools/go/analysis"
8+
)
9+
10+
type analyzerPlugin struct{}
11+
12+
func main() {
13+
fmt.Printf("Plugin library for %s", AnalyzerPlugin.GetLinterName())
14+
fmt.Println("To compile this into a plugin, run:")
15+
fmt.Println(" go build -buildmode=plugin -o /desired/path/to/example.so plugin/example.go")
16+
fmt.Println("See README.md for more info")
17+
}
18+
19+
// This must be implemented
20+
func (*analyzerPlugin) GetLinterName() string {
21+
return "example"
22+
}
23+
24+
// This must be implemented
25+
func (*analyzerPlugin) GetLinterDesc() string {
26+
return "A package of custom linters that can be used in golangci-lint"
27+
}
28+
29+
// This must be implemented
30+
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
31+
return []*analysis.Analyzer{
32+
linters.TodoAnalyzer,
33+
}
34+
}
35+
36+
// This must be defined
37+
var AnalyzerPlugin analyzerPlugin
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package todo
2+
3+
// comment without a to do
4+
func SomeFunc1() {
5+
_ = 1 + 1
6+
}
7+
8+
// TODO: do something // want "TODO comment has no author"
9+
func SomeFunc2() {
10+
_ = 1 + 2
11+
}
12+
13+
// TODO(): do something // want "TODO comment has no author"
14+
func SomeFunc3() {
15+
_ = 1 + 3
16+
}
17+
18+
// TODO(dbraley): Do something with the value
19+
func SomeFunc4() {
20+
_ = 1 + 4
21+
}

0 commit comments

Comments
 (0)