Permalink
Browse files

Switch to the go/types API in the standard library.

This means that golint now requires Go 1.5 or later.
It also means that vendored packages will now be correctly handled,
assuming you are using Go 1.6 or later.

Fixes #151.
  • Loading branch information...
1 parent 32a8716 commit a113636da96a2efee02051f523c74104dbe4d202 @dsymonds dsymonds committed Mar 17, 2016
Showing with 29 additions and 8 deletions.
  1. +2 −0 README.md
  2. +24 −5 lint.go
  3. +3 −3 lint_test.go
View
@@ -4,6 +4,8 @@ Golint is a linter for Go source code.
## Installation
+Golint requires Go 1.5 or later.
+
go get -u github.com/golang/lint/golint
@dubek
dubek Apr 28, 2016

I think you'll need to add /... at the end in order to actually build the golint binary into $GOPATH/bin, so it should be:

go get -u github.com/golang/lint/golint/...
## Usage
View
@@ -14,15 +14,15 @@ import (
"go/parser"
"go/printer"
"go/token"
+ "go/types"
@dsymonds
dsymonds Mar 18, 2016 Member

As the README says, golint now requires Go 1.5 or later. Your Travis build is using Go 1.4.3.

@vbatts
vbatts Mar 21, 2016

I see that. It is less than neat to do go-version specific tests.

"regexp"
"sort"
"strconv"
"strings"
"unicode"
"unicode/utf8"
- "golang.org/x/tools/go/gcimporter"
- "golang.org/x/tools/go/types"
+ "golang.org/x/tools/go/gcimporter15"
)
const styleGuideBase = "https://golang.org/wiki/CodeReviewComments"
@@ -236,11 +236,30 @@ argLoop:
var gcImporter = gcimporter.Import
+// importer implements go/types.Importer.
+// It also implements go/types.ImporterFrom, which was new in Go 1.6,
+// so vendoring will work.
+type importer struct {
+ impFn func(packages map[string]*types.Package, path, srcDir string) (*types.Package, error)
+ packages map[string]*types.Package
+}
+
+func (i importer) Import(path string) (*types.Package, error) {
+ return i.impFn(i.packages, path, "")
+}
+
+func (i importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
+ return i.impFn(i.packages, path, srcDir)
+}
+
func (p *pkg) typeCheck() error {
config := &types.Config{
// By setting a no-op error reporter, the type checker does as much work as possible.
- Error: func(error) {},
- Import: gcImporter,
+ Error: func(error) {},
+ Importer: importer{
+ impFn: gcImporter,
+ packages: make(map[string]*types.Package),
+ },
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
@@ -397,7 +416,7 @@ func (f *file) lintPackageComment() {
s = ts
}
// Only non-main packages need to keep to this form.
- if f.f.Name.Name != "main" && !strings.HasPrefix(s, prefix) {
+ if !f.pkg.main && !strings.HasPrefix(s, prefix) {
f.errorf(f.f.Doc, 1, link(ref), category("comments"), `package comment should be of the form "%s..."`, prefix)
}
}
View
@@ -14,14 +14,13 @@ import (
"go/parser"
"go/printer"
"go/token"
+ "go/types"
"io/ioutil"
"path"
"regexp"
"strconv"
"strings"
"testing"
-
- "golang.org/x/tools/go/types"
)
var lintMatch = flag.String("lint.match", "", "restrict testdata matches to this pattern")
@@ -274,7 +273,8 @@ func TestExportedType(t *testing.T) {
t.Fatalf("Parsing %q: %v", src, err)
}
// use the package name as package path
- pkg, err := types.Check(file.Name.Name, fset, []*ast.File{file})
+ config := &types.Config{}
+ pkg, err := config.Check(file.Name.Name, fset, []*ast.File{file}, nil)
if err != nil {
t.Fatalf("Type checking %q: %v", src, err)
}

0 comments on commit a113636

Please sign in to comment.