Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Commit a113636

Browse files
committed
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.
1 parent 32a8716 commit a113636

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Golint is a linter for Go source code.
44

55
## Installation
66

7+
Golint requires Go 1.5 or later.
8+
79
go get -u github.com/golang/lint/golint
810

911
## Usage

lint.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
"go/parser"
1515
"go/printer"
1616
"go/token"
17+
"go/types"
1718
"regexp"
1819
"sort"
1920
"strconv"
2021
"strings"
2122
"unicode"
2223
"unicode/utf8"
2324

24-
"golang.org/x/tools/go/gcimporter"
25-
"golang.org/x/tools/go/types"
25+
"golang.org/x/tools/go/gcimporter15"
2626
)
2727

2828
const styleGuideBase = "https://golang.org/wiki/CodeReviewComments"
@@ -236,11 +236,30 @@ argLoop:
236236

237237
var gcImporter = gcimporter.Import
238238

239+
// importer implements go/types.Importer.
240+
// It also implements go/types.ImporterFrom, which was new in Go 1.6,
241+
// so vendoring will work.
242+
type importer struct {
243+
impFn func(packages map[string]*types.Package, path, srcDir string) (*types.Package, error)
244+
packages map[string]*types.Package
245+
}
246+
247+
func (i importer) Import(path string) (*types.Package, error) {
248+
return i.impFn(i.packages, path, "")
249+
}
250+
251+
func (i importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
252+
return i.impFn(i.packages, path, srcDir)
253+
}
254+
239255
func (p *pkg) typeCheck() error {
240256
config := &types.Config{
241257
// By setting a no-op error reporter, the type checker does as much work as possible.
242-
Error: func(error) {},
243-
Import: gcImporter,
258+
Error: func(error) {},
259+
Importer: importer{
260+
impFn: gcImporter,
261+
packages: make(map[string]*types.Package),
262+
},
244263
}
245264
info := &types.Info{
246265
Types: make(map[ast.Expr]types.TypeAndValue),
@@ -397,7 +416,7 @@ func (f *file) lintPackageComment() {
397416
s = ts
398417
}
399418
// Only non-main packages need to keep to this form.
400-
if f.f.Name.Name != "main" && !strings.HasPrefix(s, prefix) {
419+
if !f.pkg.main && !strings.HasPrefix(s, prefix) {
401420
f.errorf(f.f.Doc, 1, link(ref), category("comments"), `package comment should be of the form "%s..."`, prefix)
402421
}
403422
}

lint_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import (
1414
"go/parser"
1515
"go/printer"
1616
"go/token"
17+
"go/types"
1718
"io/ioutil"
1819
"path"
1920
"regexp"
2021
"strconv"
2122
"strings"
2223
"testing"
23-
24-
"golang.org/x/tools/go/types"
2524
)
2625

2726
var lintMatch = flag.String("lint.match", "", "restrict testdata matches to this pattern")
@@ -274,7 +273,8 @@ func TestExportedType(t *testing.T) {
274273
t.Fatalf("Parsing %q: %v", src, err)
275274
}
276275
// use the package name as package path
277-
pkg, err := types.Check(file.Name.Name, fset, []*ast.File{file})
276+
config := &types.Config{}
277+
pkg, err := config.Check(file.Name.Name, fset, []*ast.File{file}, nil)
278278
if err != nil {
279279
t.Fatalf("Type checking %q: %v", src, err)
280280
}

0 commit comments

Comments
 (0)