ed64e33c8c8bc9a919e2b85a1a08225b5ae59d70. Also add tests for local mode of goimports and do refactoring of tests.
This commit is contained in:
parent
7836034ecf
commit
ac77eaac68
go.modgo.sum
pkg/golinters
test
bench
data.goenabled_linters.golinters_test.gorun_test.gotestdata
deadcode.godepguard.godupl.goerrcheck.gogochecknoglobals.gogochecknoinits.gogoconst.gogocritic.gogocyclo.gogofmt.gogoimports.go
goimports
golint.gogosec.gogovet.goineffassign.gointerfacer.golll.gomaligned.gomegacheck.gomisspell.gonotcompiles
prealloc.goscopelint.gostructcheck.gounconvert.gounparam.govarcheck.gotestshared
vendor
3
go.mod
3
go.mod
@ -37,7 +37,7 @@ require (
|
||||
github.com/golangci/misspell v0.0.0-20180809174111-950f5d19e770
|
||||
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
|
||||
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
|
||||
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b
|
||||
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b
|
||||
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
|
||||
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16
|
||||
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
|
||||
@ -68,6 +68,7 @@ require (
|
||||
golang.org/x/tools v0.0.0-20180831211245-6c7e314b6563
|
||||
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
|
||||
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.1
|
||||
sourcegraph.com/sourcegraph/go-diff v0.0.0-20171119081133-3f415a150aec
|
||||
sourcegraph.com/sqs/pbtypes v0.0.0-20160107090929-4d1b9dc7ffc3 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -75,8 +75,8 @@ github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSS
|
||||
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI=
|
||||
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0 h1:HVfrLniijszjS1aiNg8JbBMO2+E1WIQ+j/gL4SQqGPg=
|
||||
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0/go.mod h1:qOQCunEYvmd/TLamH+7LlVccLvUH5kZNhbCgTHoBbp4=
|
||||
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b h1:3a73k6ptEhiI9YA8cEe4i6nsLWQRjtBG97+tjhS+QBs=
|
||||
github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
|
||||
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b h1:3hI7NZ9D3edEBVbN6V1urHWbFKJfcIlOFvX5m10jB88=
|
||||
github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b/go.mod h1:zgj6NOYXOC1cexsdtDceI4/mj3aXK4JOVg9AV3C5LWI=
|
||||
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys=
|
||||
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ=
|
||||
github.com/golangci/unparam v0.0.0-20180902112548-7ad9dbcccc16 h1:QURX/XMP2uJUzzEvfJ291v1snmbJuyznAJLSQVnPyko=
|
||||
|
@ -14,9 +14,10 @@ import (
|
||||
_ "github.com/go-critic/checkers" // this import register checkers
|
||||
|
||||
"github.com/go-lintpack/lintpack"
|
||||
"golang.org/x/tools/go/loader"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
"golang.org/x/tools/go/loader"
|
||||
)
|
||||
|
||||
type Gocritic struct{}
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
"go/token"
|
||||
|
||||
"golang.org/x/tools/imports"
|
||||
"github.com/golangci/tools/imports" // TODO: replace with x/tools when use it in golangci/gofmt/gofmt
|
||||
|
||||
gofmtAPI "github.com/golangci/gofmt/gofmt"
|
||||
goimportsAPI "github.com/golangci/gofmt/goimports"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package test
|
||||
package bench
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -13,6 +13,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golangci/golangci-lint/test/testshared"
|
||||
|
||||
gops "github.com/mitchellh/go-ps"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
|
||||
@ -239,7 +241,7 @@ func runOne(b *testing.B, run func(*testing.B), progName string) *runResult {
|
||||
}
|
||||
|
||||
func BenchmarkWithGometalinter(b *testing.B) {
|
||||
installBinary(b)
|
||||
testshared.NewLintRunner(b).Install()
|
||||
|
||||
type bcase struct {
|
||||
name string
|
15
test/data.go
Normal file
15
test/data.go
Normal file
@ -0,0 +1,15 @@
|
||||
package test
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
const testdataDir = "testdata"
|
||||
const binName = "golangci-lint"
|
||||
|
||||
func getProjectRoot() string {
|
||||
return filepath.Join("..", "...")
|
||||
}
|
||||
|
||||
func getTestDataDir(names ...string) string {
|
||||
parts := append([]string{testdataDir}, names...)
|
||||
return filepath.Join(parts...)
|
||||
}
|
207
test/enabled_linters.go
Normal file
207
test/enabled_linters.go
Normal file
@ -0,0 +1,207 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
||||
"github.com/golangci/golangci-lint/test/testshared"
|
||||
)
|
||||
|
||||
func inSlice(s []string, v string) bool {
|
||||
for _, sv := range s {
|
||||
if sv == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
|
||||
m := lintersdb.NewManager()
|
||||
ebdl := m.GetAllEnabledByDefaultLinters()
|
||||
ret := []string{}
|
||||
for _, lc := range ebdl {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
|
||||
if !inSlice(except, lc.Name()) {
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getAllFastLintersWith(with ...string) []string {
|
||||
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
|
||||
ret := append([]string{}, with...)
|
||||
for _, lc := range linters {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getEnabledByDefaultLinters() []string {
|
||||
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
|
||||
ret := []string{}
|
||||
for _, lc := range ebdl {
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getEnabledByDefaultFastLintersWith(with ...string) []string {
|
||||
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
|
||||
ret := append([]string{}, with...)
|
||||
for _, lc := range ebdl {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func mergeMegacheck(linters []string) []string {
|
||||
if inSlice(linters, "staticcheck") &&
|
||||
inSlice(linters, "gosimple") &&
|
||||
inSlice(linters, "unused") {
|
||||
ret := []string{"megacheck"}
|
||||
for _, linter := range linters {
|
||||
if !inSlice([]string{"staticcheck", "gosimple", "unused"}, linter) {
|
||||
ret = append(ret, linter)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
return linters
|
||||
}
|
||||
|
||||
func TestEnabledLinters(t *testing.T) {
|
||||
type tc struct {
|
||||
name string
|
||||
cfg string
|
||||
el []string
|
||||
args string
|
||||
noImplicitFast bool
|
||||
}
|
||||
|
||||
cases := []tc{
|
||||
{
|
||||
name: "disable govet in config",
|
||||
cfg: `
|
||||
linters:
|
||||
disable:
|
||||
- govet
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersExcept("govet"),
|
||||
},
|
||||
{
|
||||
name: "enable golint in config",
|
||||
cfg: `
|
||||
linters:
|
||||
enable:
|
||||
- golint
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith("golint"),
|
||||
},
|
||||
{
|
||||
name: "disable govet in cmd",
|
||||
args: "-Dgovet",
|
||||
el: getEnabledByDefaultFastLintersExcept("govet"),
|
||||
},
|
||||
{
|
||||
name: "enable gofmt in cmd and enable golint in config",
|
||||
args: "-Egofmt",
|
||||
cfg: `
|
||||
linters:
|
||||
enable:
|
||||
- golint
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
|
||||
},
|
||||
{
|
||||
name: "fast option in config",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: true
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "explicitly unset fast option in config",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: false
|
||||
`,
|
||||
el: getEnabledByDefaultLinters(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "set fast option in command-line",
|
||||
args: "--fast",
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option in command-line has higher priority to enable",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: false
|
||||
`,
|
||||
args: "--fast",
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option in command-line has higher priority to disable",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: true
|
||||
`,
|
||||
args: "--fast=false",
|
||||
el: getEnabledByDefaultLinters(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option combined with enable and enable-all",
|
||||
args: "--enable-all --fast --enable=megacheck",
|
||||
el: getAllFastLintersWith("megacheck"),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
c := c
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
runArgs := []string{"-v"}
|
||||
if !c.noImplicitFast {
|
||||
runArgs = append(runArgs, "--fast")
|
||||
}
|
||||
if c.args != "" {
|
||||
runArgs = append(runArgs, strings.Split(c.args, " ")...)
|
||||
}
|
||||
r := testshared.NewLintRunner(t).RunWithYamlConfig(c.cfg, runArgs...)
|
||||
el := mergeMegacheck(c.el)
|
||||
sort.StringSlice(el).Sort()
|
||||
|
||||
expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(el), strings.Join(el, " "))
|
||||
r.ExpectOutputContains(expectedLine)
|
||||
})
|
||||
}
|
||||
}
|
@ -1,17 +1,21 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golangci/golangci-lint/test/testshared"
|
||||
|
||||
assert "github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func runGoErrchk(c *exec.Cmd, t *testing.T) {
|
||||
@ -22,16 +26,18 @@ func runGoErrchk(c *exec.Cmd, t *testing.T) {
|
||||
assert.False(t, bytes.Contains(output, []byte("BUG")), "Output:\n%s", output)
|
||||
}
|
||||
|
||||
const testdataDir = "testdata"
|
||||
const binName = "golangci-lint"
|
||||
|
||||
func testSourcesFromDir(t *testing.T, dir string) {
|
||||
t.Log(filepath.Join(dir, "*.go"))
|
||||
sources, err := filepath.Glob(filepath.Join(dir, "*.go"))
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, sources)
|
||||
|
||||
installBinary(t)
|
||||
findSources := func(pathPatterns ...string) []string {
|
||||
sources, err := filepath.Glob(filepath.Join(pathPatterns...))
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, sources)
|
||||
return sources
|
||||
}
|
||||
sources := findSources(dir, "*.go")
|
||||
|
||||
testshared.NewLintRunner(t).Install()
|
||||
|
||||
for _, s := range sources {
|
||||
s := s
|
||||
@ -50,23 +56,71 @@ func TestTypecheck(t *testing.T) {
|
||||
testSourcesFromDir(t, filepath.Join(testdataDir, "notcompiles"))
|
||||
}
|
||||
|
||||
func TestGoimportsLocal(t *testing.T) {
|
||||
t.Skipf("strange travis and go bug, enable it when it will be fixed: https://travis-ci.com/golangci/golangci-lint/jobs/157695743")
|
||||
|
||||
sourcePath := filepath.Join(testdataDir, "goimports", "goimports.go")
|
||||
args := []string{
|
||||
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
|
||||
sourcePath,
|
||||
}
|
||||
rc := extractRunContextFromComments(t, sourcePath)
|
||||
args = append(args, rc.args...)
|
||||
|
||||
cfg, err := yaml.Marshal(rc.config)
|
||||
assert.NoError(t, err)
|
||||
|
||||
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
|
||||
ExpectHasIssue("testdata/goimports/goimports.go:8: File is not `goimports`-ed")
|
||||
}
|
||||
|
||||
func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
|
||||
f, err := ioutil.TempFile("", "golangci_lint_test")
|
||||
assert.NoError(t, err)
|
||||
|
||||
cfgPath = f.Name() + ".yml"
|
||||
err = os.Rename(f.Name(), cfgPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = yaml.NewEncoder(f).Encode(cfg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
return cfgPath, func() {
|
||||
assert.NoError(t, f.Close())
|
||||
assert.NoError(t, os.Remove(cfgPath))
|
||||
}
|
||||
}
|
||||
|
||||
func testOneSource(t *testing.T, sourcePath string) {
|
||||
goErrchkBin := filepath.Join(runtime.GOROOT(), "test", "errchk")
|
||||
args := []string{
|
||||
binName, "run",
|
||||
"--no-config",
|
||||
"--disable-all",
|
||||
"--print-issued-lines=false",
|
||||
"--print-linter-name=false",
|
||||
"--out-format=line-number",
|
||||
}
|
||||
|
||||
rc := extractRunContextFromComments(t, sourcePath)
|
||||
var cfgPath string
|
||||
|
||||
if rc.config != nil {
|
||||
p, finish := saveConfig(t, rc.config)
|
||||
defer finish()
|
||||
cfgPath = p
|
||||
}
|
||||
|
||||
for _, addArg := range []string{"", "-Etypecheck"} {
|
||||
caseArgs := append([]string{}, args...)
|
||||
caseArgs = append(caseArgs, getAdditionalArgs(t, sourcePath)...)
|
||||
caseArgs = append(caseArgs, rc.args...)
|
||||
if addArg != "" {
|
||||
caseArgs = append(caseArgs, addArg)
|
||||
}
|
||||
if cfgPath == "" {
|
||||
caseArgs = append(caseArgs, "--no-config")
|
||||
} else {
|
||||
caseArgs = append(caseArgs, "-c", cfgPath)
|
||||
}
|
||||
|
||||
caseArgs = append(caseArgs, sourcePath)
|
||||
|
||||
@ -76,30 +130,81 @@ func testOneSource(t *testing.T, sourcePath string) {
|
||||
}
|
||||
}
|
||||
|
||||
func getAdditionalArgs(t *testing.T, sourcePath string) []string {
|
||||
data, err := ioutil.ReadFile(sourcePath)
|
||||
assert.NoError(t, err)
|
||||
type runContext struct {
|
||||
args []string
|
||||
config map[string]interface{}
|
||||
}
|
||||
|
||||
lines := strings.SplitN(string(data), "\n", 2)
|
||||
firstLine := lines[0]
|
||||
func buildConfigFromShortRepr(t *testing.T, repr string, config map[string]interface{}) {
|
||||
kv := strings.Split(repr, "=")
|
||||
assert.Len(t, kv, 2)
|
||||
|
||||
parts := strings.Split(firstLine, "args:")
|
||||
if len(parts) == 1 {
|
||||
return nil
|
||||
keyParts := strings.Split(kv[0], ".")
|
||||
assert.True(t, len(keyParts) >= 2, len(keyParts))
|
||||
|
||||
lastObj := config
|
||||
for _, k := range keyParts[:len(keyParts)-1] {
|
||||
var v map[string]interface{}
|
||||
if lastObj[k] == nil {
|
||||
v = map[string]interface{}{}
|
||||
} else {
|
||||
v = lastObj[k].(map[string]interface{})
|
||||
}
|
||||
|
||||
lastObj[k] = v
|
||||
lastObj = v
|
||||
}
|
||||
|
||||
return strings.Split(parts[len(parts)-1], " ")
|
||||
lastObj[keyParts[len(keyParts)-1]] = kv[1]
|
||||
}
|
||||
|
||||
func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext {
|
||||
f, err := os.Open(sourcePath)
|
||||
assert.NoError(t, err)
|
||||
defer f.Close()
|
||||
|
||||
rc := &runContext{}
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if !strings.HasPrefix(line, "//") {
|
||||
return rc
|
||||
}
|
||||
|
||||
line = strings.TrimPrefix(line, "//")
|
||||
if strings.HasPrefix(line, "args: ") {
|
||||
assert.Nil(t, rc.args)
|
||||
args := strings.TrimPrefix(line, "args: ")
|
||||
assert.NotEmpty(t, args)
|
||||
rc.args = strings.Split(args, " ")
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "config: ") {
|
||||
repr := strings.TrimPrefix(line, "config: ")
|
||||
assert.NotEmpty(t, repr)
|
||||
if rc.config == nil {
|
||||
rc.config = map[string]interface{}{}
|
||||
}
|
||||
buildConfigFromShortRepr(t, repr, rc.config)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return rc
|
||||
}
|
||||
|
||||
func TestExtractRunContextFromComments(t *testing.T) {
|
||||
rc := extractRunContextFromComments(t, filepath.Join(testdataDir, "goimports", "goimports.go"))
|
||||
assert.Equal(t, []string{"-Egoimports"}, rc.args)
|
||||
}
|
||||
|
||||
func TestGolintConsumesXTestFiles(t *testing.T) {
|
||||
dir := filepath.Join(testdataDir, "withxtest")
|
||||
dir := getTestDataDir("withxtest")
|
||||
const expIssue = "if block ends with a return statement, so drop this else and outdent its block"
|
||||
|
||||
out, ec := runGolangciLint(t, "--no-config", "--disable-all", "-Egolint", dir)
|
||||
assert.Equal(t, exitcodes.IssuesFound, ec, out)
|
||||
assert.Contains(t, out, expIssue)
|
||||
|
||||
out, ec = runGolangciLint(t, "--no-config", "--disable-all", "-Egolint", filepath.Join(dir, "p_test.go"))
|
||||
assert.Equal(t, exitcodes.IssuesFound, ec, out)
|
||||
assert.Contains(t, out, expIssue)
|
||||
r := testshared.NewLintRunner(t)
|
||||
r.Run("--no-config", "--disable-all", "-Egolint", dir).ExpectHasIssue(expIssue)
|
||||
r.Run("--no-config", "--disable-all", "-Egolint", filepath.Join(dir, "p_test.go")).ExpectHasIssue(expIssue)
|
||||
}
|
||||
|
381
test/run_test.go
381
test/run_test.go
@ -1,394 +1,97 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
assert "github.com/stretchr/testify/require"
|
||||
"github.com/golangci/golangci-lint/test/testshared"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
||||
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
||||
)
|
||||
|
||||
var root = filepath.Join("..", "...") //nolint:gochecknoglobals
|
||||
var installOnce sync.Once //nolint:gochecknoglobals
|
||||
|
||||
const noIssuesOut = ""
|
||||
|
||||
func installBinary(t assert.TestingT) {
|
||||
installOnce.Do(func() {
|
||||
cmd := exec.Command("go", "install", filepath.Join("..", "cmd", binName))
|
||||
assert.NoError(t, cmd.Run(), "Can't go install %s", binName)
|
||||
})
|
||||
}
|
||||
|
||||
func checkNoIssuesRun(t *testing.T, out string, exitCode int) {
|
||||
assert.Equal(t, exitcodes.Success, exitCode)
|
||||
assert.Equal(t, noIssuesOut, out)
|
||||
}
|
||||
|
||||
func TestNoCongratsMessage(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join("..", "..."))
|
||||
assert.Equal(t, exitcodes.Success, exitCode, out)
|
||||
assert.Equal(t, "", out)
|
||||
}
|
||||
|
||||
func TestCongratsMessageIfNoIssues(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, root)
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
func TestNoIssues(t *testing.T) {
|
||||
testshared.NewLintRunner(t).Run(getProjectRoot()).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestAutogeneratedNoIssues(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "autogenerated"))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run(getTestDataDir("autogenerated")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestEmptyDirRun(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "nogofiles"))
|
||||
assert.Equal(t, exitcodes.NoGoFiles, exitCode)
|
||||
assert.Contains(t, out, ": no go files to analyze")
|
||||
testshared.NewLintRunner(t).Run(getTestDataDir("nogofiles")).
|
||||
ExpectExitCode(exitcodes.NoGoFiles).
|
||||
ExpectOutputContains(": no go files to analyze")
|
||||
}
|
||||
|
||||
func TestNotExistingDirRun(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "no_such_dir"))
|
||||
assert.True(t, exitCode == exitcodes.WarningInTest || exitCode == exitcodes.IssuesFound)
|
||||
assert.Contains(t, out, `cannot find package \"./testdata/no_such_dir\"`)
|
||||
testshared.NewLintRunner(t).Run(getTestDataDir("no_such_dir")).
|
||||
ExpectHasIssue(`cannot find package \"./testdata/no_such_dir\"`)
|
||||
}
|
||||
|
||||
func TestSymlinkLoop(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "symlink_loop", "..."))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run(getTestDataDir("symlink_loop", "...")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestDeadline(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--deadline=1ms", root)
|
||||
assert.Equal(t, exitcodes.Timeout, exitCode)
|
||||
assert.Contains(t, strings.ToLower(out), "deadline exceeded: try increase it by passing --deadline option")
|
||||
}
|
||||
|
||||
func runGolangciLint(t *testing.T, args ...string) (string, int) {
|
||||
installBinary(t)
|
||||
|
||||
runArgs := append([]string{"run"}, args...)
|
||||
log.Printf("golangci-lint %s", strings.Join(runArgs, " "))
|
||||
cmd := exec.Command("golangci-lint", runArgs...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
t.Logf("stderr: %s", exitError.Stderr)
|
||||
ws := exitError.Sys().(syscall.WaitStatus)
|
||||
return string(out), ws.ExitStatus()
|
||||
}
|
||||
|
||||
t.Fatalf("can't get error code from %s", err)
|
||||
return "", -1
|
||||
}
|
||||
|
||||
// success, exitCode should be 0 if go is ok
|
||||
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
|
||||
return string(out), ws.ExitStatus()
|
||||
}
|
||||
|
||||
func runGolangciLintWithYamlConfig(t *testing.T, cfg string, args ...string) string {
|
||||
out, ec := runGolangciLintWithYamlConfigWithCode(t, cfg, args...)
|
||||
assert.Equal(t, exitcodes.Success, ec, out)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func runGolangciLintWithYamlConfigWithCode(t *testing.T, cfg string, args ...string) (string, int) {
|
||||
f, err := ioutil.TempFile("", "golangci_lint_test")
|
||||
assert.NoError(t, err)
|
||||
f.Close()
|
||||
|
||||
cfgPath := f.Name() + ".yml"
|
||||
err = os.Rename(f.Name(), cfgPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer os.Remove(cfgPath)
|
||||
|
||||
cfg = strings.TrimSpace(cfg)
|
||||
cfg = strings.Replace(cfg, "\t", " ", -1)
|
||||
|
||||
err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
|
||||
assert.NoError(t, err)
|
||||
|
||||
pargs := append([]string{"-c", cfgPath}, args...)
|
||||
return runGolangciLint(t, pargs...)
|
||||
testshared.NewLintRunner(t).Run("--deadline=1ms", getProjectRoot()).
|
||||
ExpectExitCode(exitcodes.Timeout).
|
||||
ExpectOutputContains(`Deadline exceeded: try increase it by passing --deadline option`)
|
||||
}
|
||||
|
||||
func TestTestsAreLintedByDefault(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, filepath.Join(testdataDir, "withtests"))
|
||||
assert.Equal(t, exitcodes.IssuesFound, exitCode)
|
||||
assert.Contains(t, out, "if block ends with a return")
|
||||
testshared.NewLintRunner(t).Run(getTestDataDir("withtests")).
|
||||
ExpectHasIssue("if block ends with a return")
|
||||
}
|
||||
|
||||
func TestCgoOk(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "cgo"))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("cgo")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestCgoWithIssues(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "cgo_with_issues"))
|
||||
assert.Equal(t, exitcodes.IssuesFound, exitCode)
|
||||
assert.Contains(t, out, "Printf format %t has arg cs of wrong type")
|
||||
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("cgo_with_issues")).
|
||||
ExpectHasIssue("Printf format %t has arg cs of wrong type")
|
||||
}
|
||||
|
||||
func TestUnsafeOk(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "unsafe"))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("unsafe")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestSkippedDirs(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--print-issued-lines=false", "--no-config", "--skip-dirs", "skip_me", "-Egolint",
|
||||
filepath.Join(testdataDir, "skipdirs", "..."))
|
||||
assert.Equal(t, exitcodes.IssuesFound, exitCode)
|
||||
assert.Equal(t, out,
|
||||
"testdata/skipdirs/examples_no_skip/with_issue.go:8:9: if block ends with "+
|
||||
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "--skip-dirs", "skip_me", "-Egolint",
|
||||
getTestDataDir("skipdirs", "..."))
|
||||
|
||||
r.ExpectExitCode(exitcodes.IssuesFound).
|
||||
ExpectOutputEq("testdata/skipdirs/examples_no_skip/with_issue.go:8:9: if block ends with " +
|
||||
"a return statement, so drop this else and outdent its block (golint)\n")
|
||||
}
|
||||
|
||||
func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--no-config", "--disable-all", "-Edeadcode",
|
||||
filepath.Join(testdataDir, "deadcode_main_pkg"))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Edeadcode", getTestDataDir("deadcode_main_pkg")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestIdentifierUsedOnlyInTests(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--no-config", "--disable-all", "-Eunused",
|
||||
filepath.Join(testdataDir, "used_only_in_tests"))
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Eunused", getTestDataDir("used_only_in_tests")).ExpectNoIssues()
|
||||
}
|
||||
|
||||
func TestConfigFileIsDetected(t *testing.T) {
|
||||
checkGotConfig := func(out string, exitCode int) {
|
||||
assert.Equal(t, exitcodes.Success, exitCode, out)
|
||||
assert.Equal(t, "test\n", out) // test config contains InternalTest: true, it triggers such output
|
||||
checkGotConfig := func(r *testshared.RunResult) {
|
||||
r.ExpectExitCode(exitcodes.Success).
|
||||
ExpectOutputEq("test\n") // test config contains InternalTest: true, it triggers such output
|
||||
}
|
||||
|
||||
checkGotConfig(runGolangciLint(t, "testdata/withconfig/pkg"))
|
||||
checkGotConfig(runGolangciLint(t, "testdata/withconfig/..."))
|
||||
|
||||
out, exitCode := runGolangciLint(t) // doesn't detect when no args
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
}
|
||||
|
||||
func inSlice(s []string, v string) bool {
|
||||
for _, sv := range s {
|
||||
if sv == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
|
||||
m := lintersdb.NewManager()
|
||||
ebdl := m.GetAllEnabledByDefaultLinters()
|
||||
ret := []string{}
|
||||
for _, lc := range ebdl {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
|
||||
if !inSlice(except, lc.Name()) {
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getAllFastLintersWith(with ...string) []string {
|
||||
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
|
||||
ret := append([]string{}, with...)
|
||||
for _, lc := range linters {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getEnabledByDefaultLinters() []string {
|
||||
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
|
||||
ret := []string{}
|
||||
for _, lc := range ebdl {
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getEnabledByDefaultFastLintersWith(with ...string) []string {
|
||||
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
|
||||
ret := append([]string{}, with...)
|
||||
for _, lc := range ebdl {
|
||||
if lc.NeedsSSARepr {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, lc.Name())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func mergeMegacheck(linters []string) []string {
|
||||
if inSlice(linters, "staticcheck") &&
|
||||
inSlice(linters, "gosimple") &&
|
||||
inSlice(linters, "unused") {
|
||||
ret := []string{"megacheck"}
|
||||
for _, linter := range linters {
|
||||
if !inSlice([]string{"staticcheck", "gosimple", "unused"}, linter) {
|
||||
ret = append(ret, linter)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
return linters
|
||||
r := testshared.NewLintRunner(t)
|
||||
checkGotConfig(r.Run(getTestDataDir("withconfig", "pkg")))
|
||||
checkGotConfig(r.Run(getTestDataDir("withconfig", "...")))
|
||||
}
|
||||
|
||||
func TestEnableAllFastAndEnableCanCoexist(t *testing.T) {
|
||||
out, exitCode := runGolangciLint(t, "--fast", "--enable-all", "--enable=typecheck")
|
||||
checkNoIssuesRun(t, out, exitCode)
|
||||
|
||||
_, exitCode = runGolangciLint(t, "--enable-all", "--enable=typecheck")
|
||||
assert.Equal(t, exitcodes.Failure, exitCode)
|
||||
|
||||
}
|
||||
|
||||
func TestEnabledLinters(t *testing.T) {
|
||||
type tc struct {
|
||||
name string
|
||||
cfg string
|
||||
el []string
|
||||
args string
|
||||
noImplicitFast bool
|
||||
}
|
||||
|
||||
cases := []tc{
|
||||
{
|
||||
name: "disable govet in config",
|
||||
cfg: `
|
||||
linters:
|
||||
disable:
|
||||
- govet
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersExcept("govet"),
|
||||
},
|
||||
{
|
||||
name: "enable golint in config",
|
||||
cfg: `
|
||||
linters:
|
||||
enable:
|
||||
- golint
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith("golint"),
|
||||
},
|
||||
{
|
||||
name: "disable govet in cmd",
|
||||
args: "-Dgovet",
|
||||
el: getEnabledByDefaultFastLintersExcept("govet"),
|
||||
},
|
||||
{
|
||||
name: "enable gofmt in cmd and enable golint in config",
|
||||
args: "-Egofmt",
|
||||
cfg: `
|
||||
linters:
|
||||
enable:
|
||||
- golint
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
|
||||
},
|
||||
{
|
||||
name: "fast option in config",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: true
|
||||
`,
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "explicitly unset fast option in config",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: false
|
||||
`,
|
||||
el: getEnabledByDefaultLinters(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "set fast option in command-line",
|
||||
args: "--fast",
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option in command-line has higher priority to enable",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: false
|
||||
`,
|
||||
args: "--fast",
|
||||
el: getEnabledByDefaultFastLintersWith(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option in command-line has higher priority to disable",
|
||||
cfg: `
|
||||
linters:
|
||||
fast: true
|
||||
`,
|
||||
args: "--fast=false",
|
||||
el: getEnabledByDefaultLinters(),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
{
|
||||
name: "fast option combined with enable and enable-all",
|
||||
args: "--enable-all --fast --enable=megacheck",
|
||||
el: getAllFastLintersWith("megacheck"),
|
||||
noImplicitFast: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
c := c
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
runArgs := []string{"-v"}
|
||||
if !c.noImplicitFast {
|
||||
runArgs = append(runArgs, "--fast")
|
||||
}
|
||||
if c.args != "" {
|
||||
runArgs = append(runArgs, strings.Split(c.args, " ")...)
|
||||
}
|
||||
out := runGolangciLintWithYamlConfig(t, c.cfg, runArgs...)
|
||||
el := mergeMegacheck(c.el)
|
||||
sort.StringSlice(el).Sort()
|
||||
expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(el), strings.Join(el, " "))
|
||||
assert.Contains(t, out, expectedLine)
|
||||
})
|
||||
}
|
||||
r := testshared.NewLintRunner(t)
|
||||
r.Run("--fast", "--enable-all", "--enable=typecheck").ExpectNoIssues()
|
||||
r.Run("--enable-all", "--enable=typecheck").ExpectExitCode(exitcodes.Failure)
|
||||
}
|
||||
|
||||
func TestEnabledPresetsAreNotDuplicated(t *testing.T) {
|
||||
out, _ := runGolangciLint(t, "--no-config", "-v", "-p", "style,bugs")
|
||||
assert.Contains(t, out, "Active presets: [bugs style]")
|
||||
testshared.NewLintRunner(t).Run("--no-config", "-v", "-p", "style,bugs").
|
||||
ExpectOutputContains("Active presets: [bugs style]")
|
||||
}
|
||||
|
||||
func TestDisallowedOptionsInConfig(t *testing.T) {
|
||||
@ -428,10 +131,10 @@ func TestDisallowedOptionsInConfig(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
r := testshared.NewLintRunner(t)
|
||||
for _, c := range cases {
|
||||
// Run with disallowed option set only in config
|
||||
_, ec := runGolangciLintWithYamlConfigWithCode(t, c.cfg)
|
||||
assert.Equal(t, exitcodes.Failure, ec)
|
||||
r.RunWithYamlConfig(c.cfg).ExpectExitCode(exitcodes.Failure)
|
||||
|
||||
if c.option == "" {
|
||||
continue
|
||||
@ -440,11 +143,9 @@ func TestDisallowedOptionsInConfig(t *testing.T) {
|
||||
args := []string{c.option, "--fast"}
|
||||
|
||||
// Run with disallowed option set only in command-line
|
||||
_, ec = runGolangciLint(t, args...)
|
||||
assert.Equal(t, exitcodes.Success, ec)
|
||||
r.Run(args...).ExpectExitCode(exitcodes.Success)
|
||||
|
||||
// Run with disallowed option set both in command-line and in config
|
||||
_, ec = runGolangciLintWithYamlConfigWithCode(t, c.cfg, args...)
|
||||
assert.Equal(t, exitcodes.Failure, ec)
|
||||
r.RunWithYamlConfig(c.cfg, args...).ExpectExitCode(exitcodes.Failure)
|
||||
}
|
||||
}
|
||||
|
2
test/testdata/deadcode.go
vendored
2
test/testdata/deadcode.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Edeadcode
|
||||
//args: -Edeadcode
|
||||
package testdata
|
||||
|
||||
var y int
|
||||
|
2
test/testdata/depguard.go
vendored
2
test/testdata/depguard.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Edepguard --depguard.include-go-root --depguard.packages='compress/*,log'
|
||||
//args: -Edepguard --depguard.include-go-root --depguard.packages='compress/*,log'
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
2
test/testdata/dupl.go
vendored
2
test/testdata/dupl.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Edupl --dupl.threshold=20
|
||||
//args: -Edupl --dupl.threshold=20
|
||||
package testdata
|
||||
|
||||
type DuplLogger struct{}
|
||||
|
2
test/testdata/errcheck.go
vendored
2
test/testdata/errcheck.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Eerrcheck
|
||||
//args: -Eerrcheck
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
2
test/testdata/gochecknoglobals.go
vendored
2
test/testdata/gochecknoglobals.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egochecknoglobals
|
||||
//args: -Egochecknoglobals
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
2
test/testdata/gochecknoinits.go
vendored
2
test/testdata/gochecknoinits.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egochecknoinits
|
||||
//args: -Egochecknoinits
|
||||
package testdata
|
||||
|
||||
import "fmt"
|
||||
|
2
test/testdata/goconst.go
vendored
2
test/testdata/goconst.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egoconst
|
||||
//args: -Egoconst
|
||||
package testdata
|
||||
|
||||
import "fmt"
|
||||
|
2
test/testdata/gocritic.go
vendored
2
test/testdata/gocritic.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egocritic
|
||||
//args: -Egocritic
|
||||
package testdata
|
||||
|
||||
import "flag"
|
||||
|
2
test/testdata/gocyclo.go
vendored
2
test/testdata/gocyclo.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egocyclo --gocyclo.min-complexity=20
|
||||
//args: -Egocyclo --gocyclo.min-complexity=20
|
||||
package testdata
|
||||
|
||||
func GocycloBigComplexity(s string) { // ERROR "cyclomatic complexity .* of func .* is high .*"
|
||||
|
2
test/testdata/gofmt.go
vendored
2
test/testdata/gofmt.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egofmt
|
||||
//args: -Egofmt
|
||||
package testdata
|
||||
|
||||
import "fmt"
|
||||
|
2
test/testdata/goimports.go
vendored
2
test/testdata/goimports.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egoimports
|
||||
//args: -Egoimports
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
16
test/testdata/goimports/goimports.go
vendored
Normal file
16
test/testdata/goimports/goimports.go
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
//args: -Egoimports
|
||||
//config: linters-settings.goimports.local-prefixes=github.com/golangci/golangci-lint
|
||||
package goimports
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func GoimportsLocalTest() {
|
||||
fmt.Print("x")
|
||||
_ = config.Config{}
|
||||
_ = errors.New("")
|
||||
}
|
2
test/testdata/golint.go
vendored
2
test/testdata/golint.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egolint
|
||||
//args: -Egolint
|
||||
package testdata
|
||||
|
||||
var Go_lint string // ERROR "don't use underscores in Go names; var Go_lint should be GoLint"
|
||||
|
2
test/testdata/gosec.go
vendored
2
test/testdata/gosec.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egosec
|
||||
//args: -Egosec
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
2
test/testdata/govet.go
vendored
2
test/testdata/govet.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Egovet --govet.check-shadowing=true
|
||||
//args: -Egovet --govet.check-shadowing=true
|
||||
package testdata
|
||||
|
||||
import (
|
||||
|
2
test/testdata/ineffassign.go
vendored
2
test/testdata/ineffassign.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Eineffassign
|
||||
//args: -Eineffassign
|
||||
package testdata
|
||||
|
||||
func _() {
|
||||
|
2
test/testdata/interfacer.go
vendored
2
test/testdata/interfacer.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Einterfacer
|
||||
//args: -Einterfacer
|
||||
package testdata
|
||||
|
||||
import "io"
|
||||
|
2
test/testdata/lll.go
vendored
2
test/testdata/lll.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Elll --lll.tab-width 4
|
||||
//args: -Elll --lll.tab-width 4
|
||||
package testdata
|
||||
|
||||
func Lll() {
|
||||
|
2
test/testdata/maligned.go
vendored
2
test/testdata/maligned.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Emaligned
|
||||
//args: -Emaligned
|
||||
package testdata
|
||||
|
||||
type BadAlignedStruct struct { // ERROR "struct of size 24 bytes could be of size 16 bytes"
|
||||
|
2
test/testdata/megacheck.go
vendored
2
test/testdata/megacheck.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Emegacheck
|
||||
//args: -Emegacheck
|
||||
package testdata
|
||||
|
||||
func Megacheck() {
|
||||
|
2
test/testdata/misspell.go
vendored
2
test/testdata/misspell.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Emisspell
|
||||
//args: -Emisspell
|
||||
package testdata
|
||||
|
||||
func Misspell() {
|
||||
|
2
test/testdata/notcompiles/typecheck.go
vendored
2
test/testdata/notcompiles/typecheck.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Etypecheck
|
||||
//args: -Etypecheck
|
||||
package testdata
|
||||
|
||||
fun NotCompiles() { // ERROR "expected declaration, found.* fun"
|
||||
|
@ -1,4 +1,4 @@
|
||||
// args: -Etypecheck
|
||||
//args: -Etypecheck
|
||||
package testdata
|
||||
|
||||
func TypeCheckBadCalls() {
|
||||
|
2
test/testdata/prealloc.go
vendored
2
test/testdata/prealloc.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Eprealloc
|
||||
//args: -Eprealloc
|
||||
package testdata
|
||||
|
||||
func Prealloc(source []int) []int {
|
||||
|
2
test/testdata/scopelint.go
vendored
2
test/testdata/scopelint.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Escopelint
|
||||
//args: -Escopelint
|
||||
package testdata
|
||||
|
||||
import "fmt"
|
||||
|
2
test/testdata/structcheck.go
vendored
2
test/testdata/structcheck.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Estructcheck
|
||||
//args: -Estructcheck
|
||||
package testdata
|
||||
|
||||
type t struct {
|
||||
|
2
test/testdata/unconvert.go
vendored
2
test/testdata/unconvert.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Eunconvert
|
||||
//args: -Eunconvert
|
||||
package testdata
|
||||
|
||||
import "log"
|
||||
|
2
test/testdata/unparam.go
vendored
2
test/testdata/unparam.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Eunparam
|
||||
//args: -Eunparam
|
||||
package testdata
|
||||
|
||||
func unparamUnused(a, b uint) uint { // ERROR "`unparamUnused` - `b` is unused"
|
||||
|
2
test/testdata/varcheck.go
vendored
2
test/testdata/varcheck.go
vendored
@ -1,4 +1,4 @@
|
||||
// args: -Evarcheck
|
||||
//args: -Evarcheck
|
||||
package testdata
|
||||
|
||||
var v string // ERROR "`v` is unused"
|
||||
|
128
test/testshared/testshared.go
Normal file
128
test/testshared/testshared.go
Normal file
@ -0,0 +1,128 @@
|
||||
package testshared
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
)
|
||||
|
||||
type LintRunner struct {
|
||||
t assert.TestingT
|
||||
log logutils.Log
|
||||
|
||||
installed bool
|
||||
}
|
||||
|
||||
func NewLintRunner(t assert.TestingT) *LintRunner {
|
||||
return &LintRunner{
|
||||
t: t,
|
||||
log: logutils.NewStderrLog("test"),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *LintRunner) Install() {
|
||||
if r.installed {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command("go", "install", filepath.Join("..", "cmd", "golangci-lint"))
|
||||
assert.NoError(r.t, cmd.Run(), "Can't go install golangci-lint")
|
||||
r.installed = true
|
||||
}
|
||||
|
||||
type RunResult struct {
|
||||
t assert.TestingT
|
||||
|
||||
output string
|
||||
exitCode int
|
||||
}
|
||||
|
||||
func (r *RunResult) ExpectNoIssues() {
|
||||
assert.Equal(r.t, "", r.output, r.exitCode)
|
||||
assert.Equal(r.t, exitcodes.Success, r.exitCode, r.output)
|
||||
}
|
||||
|
||||
func (r *RunResult) ExpectExitCode(possibleCodes ...int) *RunResult {
|
||||
for _, pc := range possibleCodes {
|
||||
if pc == r.exitCode {
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
assert.Fail(r.t, "invalid exit code", "exit code (%d) must be one of %v: %s", r.exitCode, possibleCodes, r.output)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *RunResult) ExpectOutputContains(s string) *RunResult {
|
||||
assert.Contains(r.t, r.output, s, "exit code is %d", r.exitCode)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *RunResult) ExpectOutputEq(s string) *RunResult {
|
||||
assert.Equal(r.t, r.output, s, "exit code is %d", r.exitCode)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *RunResult) ExpectHasIssue(issueText string) *RunResult {
|
||||
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
|
||||
}
|
||||
|
||||
func (r *LintRunner) Run(args ...string) *RunResult {
|
||||
r.Install()
|
||||
|
||||
runArgs := append([]string{"run"}, args...)
|
||||
r.log.Infof("golangci-lint %s", strings.Join(runArgs, " "))
|
||||
cmd := exec.Command("golangci-lint", runArgs...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
r.log.Infof("stderr: %s", exitError.Stderr)
|
||||
ws := exitError.Sys().(syscall.WaitStatus)
|
||||
return &RunResult{
|
||||
t: r.t,
|
||||
output: string(out),
|
||||
exitCode: ws.ExitStatus(),
|
||||
}
|
||||
}
|
||||
|
||||
r.t.Errorf("can't get error code from %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// success, exitCode should be 0 if go is ok
|
||||
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
|
||||
return &RunResult{
|
||||
t: r.t,
|
||||
output: string(out),
|
||||
exitCode: ws.ExitStatus(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *LintRunner) RunWithYamlConfig(cfg string, args ...string) *RunResult {
|
||||
f, err := ioutil.TempFile("", "golangci_lint_test")
|
||||
assert.NoError(r.t, err)
|
||||
f.Close()
|
||||
|
||||
cfgPath := f.Name() + ".yml"
|
||||
err = os.Rename(f.Name(), cfgPath)
|
||||
assert.NoError(r.t, err)
|
||||
|
||||
defer os.Remove(cfgPath)
|
||||
|
||||
cfg = strings.TrimSpace(cfg)
|
||||
cfg = strings.Replace(cfg, "\t", " ", -1)
|
||||
|
||||
err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
|
||||
assert.NoError(r.t, err)
|
||||
|
||||
pargs := append([]string{"-c", cfgPath}, args...)
|
||||
return r.Run(pargs...)
|
||||
}
|
41
vendor/github.com/golangci/tools/imports/fix.go
generated
vendored
41
vendor/github.com/golangci/tools/imports/fix.go
generated
vendored
@ -364,46 +364,7 @@ func fixImports(fset *token.FileSet, f *ast.File, filename string) (added []stri
|
||||
}
|
||||
|
||||
// importPathToName returns the package name for the given import path.
|
||||
var importPathToName func(importPath, srcDir string) (packageName string) = importPathToNameModules
|
||||
|
||||
// importPathToNameModules is intended to be a backwards compatible
|
||||
// importPathtoName func that in the presence of modules, falls back
|
||||
// to importPathToNameBasic. This solves a speed issues as
|
||||
// importPathToNameGoPath calls build.Imports, which in the presence
|
||||
// of modules runs the 'go list' command.
|
||||
func importPathToNameModules(importPath, srcDir string) (packageName string) {
|
||||
// modules are disabled, so preserve previous behavior
|
||||
if os.Getenv("GO111MODULE") == "off" {
|
||||
return importPathToNameGoPath(importPath, srcDir)
|
||||
}
|
||||
|
||||
// modules may or may not be in use, so check for a go.mod
|
||||
abs, err := filepath.Abs(srcDir)
|
||||
if err != nil {
|
||||
return importPathToNameGoPath(importPath, srcDir)
|
||||
}
|
||||
|
||||
hasGoMod := false
|
||||
for {
|
||||
info, err := os.Stat(filepath.Join(abs, "go.mod"))
|
||||
if err == nil && !info.IsDir() {
|
||||
hasGoMod = true
|
||||
break
|
||||
}
|
||||
d := filepath.Dir(abs)
|
||||
if len(d) >= len(abs) {
|
||||
break
|
||||
}
|
||||
abs = d
|
||||
}
|
||||
|
||||
// found a go.mod so resort to the faster fallback importPathToNameBasic
|
||||
if hasGoMod {
|
||||
return importPathToNameBasic(importPath, srcDir)
|
||||
}
|
||||
|
||||
return importPathToNameGoPath(importPath, srcDir)
|
||||
}
|
||||
var importPathToName func(importPath, srcDir string) (packageName string) = importPathToNameGoPath
|
||||
|
||||
// importPathToNameBasic assumes the package name is the base of import path,
|
||||
// except that if the path ends in foo/vN, it assumes the package name is foo.
|
||||
|
934
vendor/golang.org/x/tools/imports/fix.go
generated
vendored
934
vendor/golang.org/x/tools/imports/fix.go
generated
vendored
@ -1,934 +0,0 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package imports
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
"golang.org/x/tools/internal/gopathwalk"
|
||||
)
|
||||
|
||||
// Debug controls verbose logging.
|
||||
var Debug = false
|
||||
|
||||
// LocalPrefix is a comma-separated string of import path prefixes, which, if
|
||||
// set, instructs Process to sort the import paths with the given prefixes
|
||||
// into another group after 3rd-party packages.
|
||||
var LocalPrefix string
|
||||
|
||||
func localPrefixes() []string {
|
||||
if LocalPrefix != "" {
|
||||
return strings.Split(LocalPrefix, ",")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// importToGroup is a list of functions which map from an import path to
|
||||
// a group number.
|
||||
var importToGroup = []func(importPath string) (num int, ok bool){
|
||||
func(importPath string) (num int, ok bool) {
|
||||
for _, p := range localPrefixes() {
|
||||
if strings.HasPrefix(importPath, p) || strings.TrimSuffix(p, "/") == importPath {
|
||||
return 3, true
|
||||
}
|
||||
}
|
||||
return
|
||||
},
|
||||
func(importPath string) (num int, ok bool) {
|
||||
if strings.HasPrefix(importPath, "appengine") {
|
||||
return 2, true
|
||||
}
|
||||
return
|
||||
},
|
||||
func(importPath string) (num int, ok bool) {
|
||||
if strings.Contains(importPath, ".") {
|
||||
return 1, true
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func importGroup(importPath string) int {
|
||||
for _, fn := range importToGroup {
|
||||
if n, ok := fn(importPath); ok {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// importInfo is a summary of information about one import.
|
||||
type importInfo struct {
|
||||
Path string // full import path (e.g. "crypto/rand")
|
||||
Alias string // import alias, if present (e.g. "crand")
|
||||
}
|
||||
|
||||
// packageInfo is a summary of features found in a package.
|
||||
type packageInfo struct {
|
||||
Globals map[string]bool // symbol => true
|
||||
Imports map[string]importInfo // pkg base name or alias => info
|
||||
// refs are a set of package references currently satisfied by imports.
|
||||
// first key: either base package (e.g. "fmt") or renamed package
|
||||
// second key: referenced package symbol (e.g. "Println")
|
||||
Refs map[string]map[string]bool
|
||||
}
|
||||
|
||||
// dirPackageInfo exposes the dirPackageInfoFile function so that it can be overridden.
|
||||
var dirPackageInfo = dirPackageInfoFile
|
||||
|
||||
// dirPackageInfoFile gets information from other files in the package.
|
||||
func dirPackageInfoFile(pkgName, srcDir, filename string) (*packageInfo, error) {
|
||||
considerTests := strings.HasSuffix(filename, "_test.go")
|
||||
|
||||
fileBase := filepath.Base(filename)
|
||||
packageFileInfos, err := ioutil.ReadDir(srcDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := &packageInfo{
|
||||
Globals: make(map[string]bool),
|
||||
Imports: make(map[string]importInfo),
|
||||
Refs: make(map[string]map[string]bool),
|
||||
}
|
||||
|
||||
visitor := collectReferences(info.Refs)
|
||||
for _, fi := range packageFileInfos {
|
||||
if fi.Name() == fileBase || !strings.HasSuffix(fi.Name(), ".go") {
|
||||
continue
|
||||
}
|
||||
if !considerTests && strings.HasSuffix(fi.Name(), "_test.go") {
|
||||
continue
|
||||
}
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
root, err := parser.ParseFile(fileSet, filepath.Join(srcDir, fi.Name()), nil, 0)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, decl := range root.Decls {
|
||||
genDecl, ok := decl.(*ast.GenDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, spec := range genDecl.Specs {
|
||||
valueSpec, ok := spec.(*ast.ValueSpec)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
info.Globals[valueSpec.Names[0].Name] = true
|
||||
}
|
||||
}
|
||||
|
||||
for _, imp := range root.Imports {
|
||||
impInfo := importInfo{Path: strings.Trim(imp.Path.Value, `"`)}
|
||||
name := path.Base(impInfo.Path)
|
||||
if imp.Name != nil {
|
||||
name = strings.Trim(imp.Name.Name, `"`)
|
||||
impInfo.Alias = name
|
||||
}
|
||||
info.Imports[name] = impInfo
|
||||
}
|
||||
|
||||
ast.Walk(visitor, root)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// collectReferences returns a visitor that collects all exported package
|
||||
// references
|
||||
func collectReferences(refs map[string]map[string]bool) visitFn {
|
||||
var visitor visitFn
|
||||
visitor = func(node ast.Node) ast.Visitor {
|
||||
if node == nil {
|
||||
return visitor
|
||||
}
|
||||
switch v := node.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
xident, ok := v.X.(*ast.Ident)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if xident.Obj != nil {
|
||||
// if the parser can resolve it, it's not a package ref
|
||||
break
|
||||
}
|
||||
pkgName := xident.Name
|
||||
r := refs[pkgName]
|
||||
if r == nil {
|
||||
r = make(map[string]bool)
|
||||
refs[pkgName] = r
|
||||
}
|
||||
if ast.IsExported(v.Sel.Name) {
|
||||
r[v.Sel.Name] = true
|
||||
}
|
||||
}
|
||||
return visitor
|
||||
}
|
||||
return visitor
|
||||
}
|
||||
|
||||
func fixImports(fset *token.FileSet, f *ast.File, filename string) (added []string, err error) {
|
||||
// refs are a set of possible package references currently unsatisfied by imports.
|
||||
// first key: either base package (e.g. "fmt") or renamed package
|
||||
// second key: referenced package symbol (e.g. "Println")
|
||||
refs := make(map[string]map[string]bool)
|
||||
|
||||
// decls are the current package imports. key is base package or renamed package.
|
||||
decls := make(map[string]*ast.ImportSpec)
|
||||
|
||||
abs, err := filepath.Abs(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
srcDir := filepath.Dir(abs)
|
||||
if Debug {
|
||||
log.Printf("fixImports(filename=%q), abs=%q, srcDir=%q ...", filename, abs, srcDir)
|
||||
}
|
||||
|
||||
var packageInfo *packageInfo
|
||||
var loadedPackageInfo bool
|
||||
|
||||
// collect potential uses of packages.
|
||||
var visitor visitFn
|
||||
visitor = visitFn(func(node ast.Node) ast.Visitor {
|
||||
if node == nil {
|
||||
return visitor
|
||||
}
|
||||
switch v := node.(type) {
|
||||
case *ast.ImportSpec:
|
||||
if v.Name != nil {
|
||||
decls[v.Name.Name] = v
|
||||
break
|
||||
}
|
||||
ipath := strings.Trim(v.Path.Value, `"`)
|
||||
if ipath == "C" {
|
||||
break
|
||||
}
|
||||
local := importPathToName(ipath, srcDir)
|
||||
decls[local] = v
|
||||
case *ast.SelectorExpr:
|
||||
xident, ok := v.X.(*ast.Ident)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if xident.Obj != nil {
|
||||
// if the parser can resolve it, it's not a package ref
|
||||
break
|
||||
}
|
||||
pkgName := xident.Name
|
||||
if refs[pkgName] == nil {
|
||||
refs[pkgName] = make(map[string]bool)
|
||||
}
|
||||
if !loadedPackageInfo {
|
||||
loadedPackageInfo = true
|
||||
packageInfo, _ = dirPackageInfo(f.Name.Name, srcDir, filename)
|
||||
}
|
||||
if decls[pkgName] == nil && (packageInfo == nil || !packageInfo.Globals[pkgName]) {
|
||||
refs[pkgName][v.Sel.Name] = true
|
||||
}
|
||||
}
|
||||
return visitor
|
||||
})
|
||||
ast.Walk(visitor, f)
|
||||
|
||||
// Nil out any unused ImportSpecs, to be removed in following passes
|
||||
unusedImport := map[string]string{}
|
||||
for pkg, is := range decls {
|
||||
if refs[pkg] == nil && pkg != "_" && pkg != "." {
|
||||
name := ""
|
||||
if is.Name != nil {
|
||||
name = is.Name.Name
|
||||
}
|
||||
unusedImport[strings.Trim(is.Path.Value, `"`)] = name
|
||||
}
|
||||
}
|
||||
for ipath, name := range unusedImport {
|
||||
if ipath == "C" {
|
||||
// Don't remove cgo stuff.
|
||||
continue
|
||||
}
|
||||
astutil.DeleteNamedImport(fset, f, name, ipath)
|
||||
}
|
||||
|
||||
for pkgName, symbols := range refs {
|
||||
if len(symbols) == 0 {
|
||||
// skip over packages already imported
|
||||
delete(refs, pkgName)
|
||||
}
|
||||
}
|
||||
|
||||
// Fast path, all references already imported.
|
||||
if len(refs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Can assume this will be necessary in all cases now.
|
||||
if !loadedPackageInfo {
|
||||
packageInfo, _ = dirPackageInfo(f.Name.Name, srcDir, filename)
|
||||
}
|
||||
|
||||
// Search for imports matching potential package references.
|
||||
type result struct {
|
||||
ipath string // import path
|
||||
name string // optional name to rename import as
|
||||
}
|
||||
results := make(chan result, len(refs))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
var wg sync.WaitGroup
|
||||
defer func() {
|
||||
cancel()
|
||||
wg.Wait()
|
||||
}()
|
||||
var (
|
||||
firstErr error
|
||||
firstErrOnce sync.Once
|
||||
)
|
||||
for pkgName, symbols := range refs {
|
||||
wg.Add(1)
|
||||
go func(pkgName string, symbols map[string]bool) {
|
||||
defer wg.Done()
|
||||
|
||||
if packageInfo != nil {
|
||||
sibling := packageInfo.Imports[pkgName]
|
||||
if sibling.Path != "" {
|
||||
refs := packageInfo.Refs[pkgName]
|
||||
for symbol := range symbols {
|
||||
if refs[symbol] {
|
||||
results <- result{ipath: sibling.Path, name: sibling.Alias}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ipath, rename, err := findImport(ctx, pkgName, symbols, filename)
|
||||
if err != nil {
|
||||
firstErrOnce.Do(func() {
|
||||
firstErr = err
|
||||
cancel()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if ipath == "" {
|
||||
return // No matching package.
|
||||
}
|
||||
|
||||
r := result{ipath: ipath}
|
||||
if rename {
|
||||
r.name = pkgName
|
||||
}
|
||||
results <- r
|
||||
return
|
||||
}(pkgName, symbols)
|
||||
}
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(results)
|
||||
}()
|
||||
|
||||
for result := range results {
|
||||
if result.name != "" {
|
||||
astutil.AddNamedImport(fset, f, result.name, result.ipath)
|
||||
} else {
|
||||
astutil.AddImport(fset, f, result.ipath)
|
||||
}
|
||||
added = append(added, result.ipath)
|
||||
}
|
||||
|
||||
if firstErr != nil {
|
||||
return nil, firstErr
|
||||
}
|
||||
return added, nil
|
||||
}
|
||||
|
||||
// importPathToName returns the package name for the given import path.
|
||||
var importPathToName func(importPath, srcDir string) (packageName string) = importPathToNameGoPath
|
||||
|
||||
// importPathToNameBasic assumes the package name is the base of import path,
|
||||
// except that if the path ends in foo/vN, it assumes the package name is foo.
|
||||
func importPathToNameBasic(importPath, srcDir string) (packageName string) {
|
||||
base := path.Base(importPath)
|
||||
if strings.HasPrefix(base, "v") {
|
||||
if _, err := strconv.Atoi(base[1:]); err == nil {
|
||||
dir := path.Dir(importPath)
|
||||
if dir != "." {
|
||||
return path.Base(dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
// importPathToNameGoPath finds out the actual package name, as declared in its .go files.
|
||||
// If there's a problem, it falls back to using importPathToNameBasic.
|
||||
func importPathToNameGoPath(importPath, srcDir string) (packageName string) {
|
||||
// Fast path for standard library without going to disk.
|
||||
if pkg, ok := stdImportPackage[importPath]; ok {
|
||||
return pkg
|
||||
}
|
||||
|
||||
pkgName, err := importPathToNameGoPathParse(importPath, srcDir)
|
||||
if Debug {
|
||||
log.Printf("importPathToNameGoPathParse(%q, srcDir=%q) = %q, %v", importPath, srcDir, pkgName, err)
|
||||
}
|
||||
if err == nil {
|
||||
return pkgName
|
||||
}
|
||||
return importPathToNameBasic(importPath, srcDir)
|
||||
}
|
||||
|
||||
// importPathToNameGoPathParse is a faster version of build.Import if
|
||||
// the only thing desired is the package name. It uses build.FindOnly
|
||||
// to find the directory and then only parses one file in the package,
|
||||
// trusting that the files in the directory are consistent.
|
||||
func importPathToNameGoPathParse(importPath, srcDir string) (packageName string, err error) {
|
||||
buildPkg, err := build.Import(importPath, srcDir, build.FindOnly)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
d, err := os.Open(buildPkg.Dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
names, err := d.Readdirnames(-1)
|
||||
d.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sort.Strings(names) // to have predictable behavior
|
||||
var lastErr error
|
||||
var nfile int
|
||||
for _, name := range names {
|
||||
if !strings.HasSuffix(name, ".go") {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(name, "_test.go") {
|
||||
continue
|
||||
}
|
||||
nfile++
|
||||
fullFile := filepath.Join(buildPkg.Dir, name)
|
||||
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, fullFile, nil, parser.PackageClauseOnly)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
pkgName := f.Name.Name
|
||||
if pkgName == "documentation" {
|
||||
// Special case from go/build.ImportDir, not
|
||||
// handled by ctx.MatchFile.
|
||||
continue
|
||||
}
|
||||
if pkgName == "main" {
|
||||
// Also skip package main, assuming it's a +build ignore generator or example.
|
||||
// Since you can't import a package main anyway, there's no harm here.
|
||||
continue
|
||||
}
|
||||
return pkgName, nil
|
||||
}
|
||||
if lastErr != nil {
|
||||
return "", lastErr
|
||||
}
|
||||
return "", fmt.Errorf("no importable package found in %d Go files", nfile)
|
||||
}
|
||||
|
||||
var stdImportPackage = map[string]string{} // "net/http" => "http"
|
||||
|
||||
func init() {
|
||||
// Nothing in the standard library has a package name not
|
||||
// matching its import base name.
|
||||
for _, pkg := range stdlib {
|
||||
if _, ok := stdImportPackage[pkg]; !ok {
|
||||
stdImportPackage[pkg] = path.Base(pkg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Directory-scanning state.
|
||||
var (
|
||||
// scanOnce guards calling scanGoDirs and assigning dirScan
|
||||
scanOnce sync.Once
|
||||
dirScan map[string]*pkg // abs dir path => *pkg
|
||||
)
|
||||
|
||||
type pkg struct {
|
||||
dir string // absolute file path to pkg directory ("/usr/lib/go/src/net/http")
|
||||
importPath string // full pkg import path ("net/http", "foo/bar/vendor/a/b")
|
||||
importPathShort string // vendorless import path ("net/http", "a/b")
|
||||
}
|
||||
|
||||
type pkgDistance struct {
|
||||
pkg *pkg
|
||||
distance int // relative distance to target
|
||||
}
|
||||
|
||||
// byDistanceOrImportPathShortLength sorts by relative distance breaking ties
|
||||
// on the short import path length and then the import string itself.
|
||||
type byDistanceOrImportPathShortLength []pkgDistance
|
||||
|
||||
func (s byDistanceOrImportPathShortLength) Len() int { return len(s) }
|
||||
func (s byDistanceOrImportPathShortLength) Less(i, j int) bool {
|
||||
di, dj := s[i].distance, s[j].distance
|
||||
if di == -1 {
|
||||
return false
|
||||
}
|
||||
if dj == -1 {
|
||||
return true
|
||||
}
|
||||
if di != dj {
|
||||
return di < dj
|
||||
}
|
||||
|
||||
vi, vj := s[i].pkg.importPathShort, s[j].pkg.importPathShort
|
||||
if len(vi) != len(vj) {
|
||||
return len(vi) < len(vj)
|
||||
}
|
||||
return vi < vj
|
||||
}
|
||||
func (s byDistanceOrImportPathShortLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
func distance(basepath, targetpath string) int {
|
||||
p, err := filepath.Rel(basepath, targetpath)
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
if p == "." {
|
||||
return 0
|
||||
}
|
||||
return strings.Count(p, string(filepath.Separator)) + 1
|
||||
}
|
||||
|
||||
// scanGoDirs populates the dirScan map for GOPATH and GOROOT.
|
||||
func scanGoDirs() map[string]*pkg {
|
||||
result := make(map[string]*pkg)
|
||||
var mu sync.Mutex
|
||||
|
||||
add := func(root gopathwalk.Root, dir string) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, dup := result[dir]; dup {
|
||||
return
|
||||
}
|
||||
importpath := filepath.ToSlash(dir[len(root.Path)+len("/"):])
|
||||
result[dir] = &pkg{
|
||||
importPath: importpath,
|
||||
importPathShort: VendorlessPath(importpath),
|
||||
dir: dir,
|
||||
}
|
||||
}
|
||||
gopathwalk.Walk(gopathwalk.SrcDirsRoots(), add, gopathwalk.Options{Debug: Debug, ModulesEnabled: false})
|
||||
return result
|
||||
}
|
||||
|
||||
// VendorlessPath returns the devendorized version of the import path ipath.
|
||||
// For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b".
|
||||
func VendorlessPath(ipath string) string {
|
||||
// Devendorize for use in import statement.
|
||||
if i := strings.LastIndex(ipath, "/vendor/"); i >= 0 {
|
||||
return ipath[i+len("/vendor/"):]
|
||||
}
|
||||
if strings.HasPrefix(ipath, "vendor/") {
|
||||
return ipath[len("vendor/"):]
|
||||
}
|
||||
return ipath
|
||||
}
|
||||
|
||||
// loadExports returns the set of exported symbols in the package at dir.
|
||||
// It returns nil on error or if the package name in dir does not match expectPackage.
|
||||
var loadExports func(ctx context.Context, expectPackage, dir string) (map[string]bool, error) = loadExportsGoPath
|
||||
|
||||
func loadExportsGoPath(ctx context.Context, expectPackage, dir string) (map[string]bool, error) {
|
||||
if Debug {
|
||||
log.Printf("loading exports in dir %s (seeking package %s)", dir, expectPackage)
|
||||
}
|
||||
exports := make(map[string]bool)
|
||||
|
||||
buildCtx := build.Default
|
||||
|
||||
// ReadDir is like ioutil.ReadDir, but only returns *.go files
|
||||
// and filters out _test.go files since they're not relevant
|
||||
// and only slow things down.
|
||||
buildCtx.ReadDir = func(dir string) (notTests []os.FileInfo, err error) {
|
||||
all, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notTests = all[:0]
|
||||
for _, fi := range all {
|
||||
name := fi.Name()
|
||||
if strings.HasSuffix(name, ".go") && !strings.HasSuffix(name, "_test.go") {
|
||||
notTests = append(notTests, fi)
|
||||
}
|
||||
}
|
||||
return notTests, nil
|
||||
}
|
||||
|
||||
files, err := buildCtx.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fset := token.NewFileSet()
|
||||
|
||||
for _, fi := range files {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
match, err := buildCtx.MatchFile(dir, fi.Name())
|
||||
if err != nil || !match {
|
||||
continue
|
||||
}
|
||||
fullFile := filepath.Join(dir, fi.Name())
|
||||
f, err := parser.ParseFile(fset, fullFile, nil, 0)
|
||||
if err != nil {
|
||||
if Debug {
|
||||
log.Printf("Parsing %s: %v", fullFile, err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
pkgName := f.Name.Name
|
||||
if pkgName == "documentation" {
|
||||
// Special case from go/build.ImportDir, not
|
||||
// handled by buildCtx.MatchFile.
|
||||
continue
|
||||
}
|
||||
if pkgName != expectPackage {
|
||||
err := fmt.Errorf("scan of dir %v is not expected package %v (actually %v)", dir, expectPackage, pkgName)
|
||||
if Debug {
|
||||
log.Print(err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
for name := range f.Scope.Objects {
|
||||
if ast.IsExported(name) {
|
||||
exports[name] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if Debug {
|
||||
exportList := make([]string, 0, len(exports))
|
||||
for k := range exports {
|
||||
exportList = append(exportList, k)
|
||||
}
|
||||
sort.Strings(exportList)
|
||||
log.Printf("loaded exports in dir %v (package %v): %v", dir, expectPackage, strings.Join(exportList, ", "))
|
||||
}
|
||||
return exports, nil
|
||||
}
|
||||
|
||||
// findImport searches for a package with the given symbols.
|
||||
// If no package is found, findImport returns ("", false, nil)
|
||||
//
|
||||
// This is declared as a variable rather than a function so goimports
|
||||
// can be easily extended by adding a file with an init function.
|
||||
//
|
||||
// The rename value tells goimports whether to use the package name as
|
||||
// a local qualifier in an import. For example, if findImports("pkg",
|
||||
// "X") returns ("foo/bar", rename=true), then goimports adds the
|
||||
// import line:
|
||||
// import pkg "foo/bar"
|
||||
// to satisfy uses of pkg.X in the file.
|
||||
var findImport func(ctx context.Context, pkgName string, symbols map[string]bool, filename string) (foundPkg string, rename bool, err error) = findImportGoPath
|
||||
|
||||
// findImportGoPath is the normal implementation of findImport.
|
||||
// (Some companies have their own internally.)
|
||||
func findImportGoPath(ctx context.Context, pkgName string, symbols map[string]bool, filename string) (foundPkg string, rename bool, err error) {
|
||||
pkgDir, err := filepath.Abs(filename)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
pkgDir = filepath.Dir(pkgDir)
|
||||
|
||||
// Fast path for the standard library.
|
||||
// In the common case we hopefully never have to scan the GOPATH, which can
|
||||
// be slow with moving disks.
|
||||
if pkg, ok := findImportStdlib(pkgName, symbols); ok {
|
||||
return pkg, false, nil
|
||||
}
|
||||
if pkgName == "rand" && symbols["Read"] {
|
||||
// Special-case rand.Read.
|
||||
//
|
||||
// If findImportStdlib didn't find it above, don't go
|
||||
// searching for it, lest it find and pick math/rand
|
||||
// in GOROOT (new as of Go 1.6)
|
||||
//
|
||||
// crypto/rand is the safer choice.
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
// TODO(sameer): look at the import lines for other Go files in the
|
||||
// local directory, since the user is likely to import the same packages
|
||||
// in the current Go file. Return rename=true when the other Go files
|
||||
// use a renamed package that's also used in the current file.
|
||||
|
||||
// Scan $GOROOT and each $GOPATH.
|
||||
scanOnce.Do(func() { dirScan = scanGoDirs() })
|
||||
|
||||
// Find candidate packages, looking only at their directory names first.
|
||||
var candidates []pkgDistance
|
||||
for _, pkg := range dirScan {
|
||||
if pkgIsCandidate(filename, pkgName, pkg) {
|
||||
candidates = append(candidates, pkgDistance{
|
||||
pkg: pkg,
|
||||
distance: distance(pkgDir, pkg.dir),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the candidates by their import package length,
|
||||
// assuming that shorter package names are better than long
|
||||
// ones. Note that this sorts by the de-vendored name, so
|
||||
// there's no "penalty" for vendoring.
|
||||
sort.Sort(byDistanceOrImportPathShortLength(candidates))
|
||||
if Debug {
|
||||
for i, c := range candidates {
|
||||
log.Printf("%s candidate %d/%d: %v in %v", pkgName, i+1, len(candidates), c.pkg.importPathShort, c.pkg.dir)
|
||||
}
|
||||
}
|
||||
|
||||
// Collect exports for packages with matching names.
|
||||
|
||||
rescv := make([]chan *pkg, len(candidates))
|
||||
for i := range candidates {
|
||||
rescv[i] = make(chan *pkg, 1)
|
||||
}
|
||||
const maxConcurrentPackageImport = 4
|
||||
loadExportsSem := make(chan struct{}, maxConcurrentPackageImport)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
var wg sync.WaitGroup
|
||||
defer func() {
|
||||
cancel()
|
||||
wg.Wait()
|
||||
}()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i, c := range candidates {
|
||||
select {
|
||||
case loadExportsSem <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func(c pkgDistance, resc chan<- *pkg) {
|
||||
defer func() {
|
||||
<-loadExportsSem
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
exports, err := loadExports(ctx, pkgName, c.pkg.dir)
|
||||
if err != nil {
|
||||
resc <- nil
|
||||
return
|
||||
}
|
||||
|
||||
// If it doesn't have the right
|
||||
// symbols, send nil to mean no match.
|
||||
for symbol := range symbols {
|
||||
if !exports[symbol] {
|
||||
resc <- nil
|
||||
return
|
||||
}
|
||||
}
|
||||
resc <- c.pkg
|
||||
}(c, rescv[i])
|
||||
}
|
||||
}()
|
||||
|
||||
for _, resc := range rescv {
|
||||
pkg := <-resc
|
||||
if pkg == nil {
|
||||
continue
|
||||
}
|
||||
// If the package name in the source doesn't match the import path's base,
|
||||
// return true so the rewriter adds a name (import foo "github.com/bar/go-foo")
|
||||
needsRename := path.Base(pkg.importPath) != pkgName
|
||||
return pkg.importPathShort, needsRename, nil
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
// pkgIsCandidate reports whether pkg is a candidate for satisfying the
|
||||
// finding which package pkgIdent in the file named by filename is trying
|
||||
// to refer to.
|
||||
//
|
||||
// This check is purely lexical and is meant to be as fast as possible
|
||||
// because it's run over all $GOPATH directories to filter out poor
|
||||
// candidates in order to limit the CPU and I/O later parsing the
|
||||
// exports in candidate packages.
|
||||
//
|
||||
// filename is the file being formatted.
|
||||
// pkgIdent is the package being searched for, like "client" (if
|
||||
// searching for "client.New")
|
||||
func pkgIsCandidate(filename, pkgIdent string, pkg *pkg) bool {
|
||||
// Check "internal" and "vendor" visibility:
|
||||
if !canUse(filename, pkg.dir) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Speed optimization to minimize disk I/O:
|
||||
// the last two components on disk must contain the
|
||||
// package name somewhere.
|
||||
//
|
||||
// This permits mismatch naming like directory
|
||||
// "go-foo" being package "foo", or "pkg.v3" being "pkg",
|
||||
// or directory "google.golang.org/api/cloudbilling/v1"
|
||||
// being package "cloudbilling", but doesn't
|
||||
// permit a directory "foo" to be package
|
||||
// "bar", which is strongly discouraged
|
||||
// anyway. There's no reason goimports needs
|
||||
// to be slow just to accommodate that.
|
||||
lastTwo := lastTwoComponents(pkg.importPathShort)
|
||||
if strings.Contains(lastTwo, pkgIdent) {
|
||||
return true
|
||||
}
|
||||
if hasHyphenOrUpperASCII(lastTwo) && !hasHyphenOrUpperASCII(pkgIdent) {
|
||||
lastTwo = lowerASCIIAndRemoveHyphen(lastTwo)
|
||||
if strings.Contains(lastTwo, pkgIdent) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func hasHyphenOrUpperASCII(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
b := s[i]
|
||||
if b == '-' || ('A' <= b && b <= 'Z') {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func lowerASCIIAndRemoveHyphen(s string) (ret string) {
|
||||
buf := make([]byte, 0, len(s))
|
||||
for i := 0; i < len(s); i++ {
|
||||
b := s[i]
|
||||
switch {
|
||||
case b == '-':
|
||||
continue
|
||||
case 'A' <= b && b <= 'Z':
|
||||
buf = append(buf, b+('a'-'A'))
|
||||
default:
|
||||
buf = append(buf, b)
|
||||
}
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// canUse reports whether the package in dir is usable from filename,
|
||||
// respecting the Go "internal" and "vendor" visibility rules.
|
||||
func canUse(filename, dir string) bool {
|
||||
// Fast path check, before any allocations. If it doesn't contain vendor
|
||||
// or internal, it's not tricky:
|
||||
// Note that this can false-negative on directories like "notinternal",
|
||||
// but we check it correctly below. This is just a fast path.
|
||||
if !strings.Contains(dir, "vendor") && !strings.Contains(dir, "internal") {
|
||||
return true
|
||||
}
|
||||
|
||||
dirSlash := filepath.ToSlash(dir)
|
||||
if !strings.Contains(dirSlash, "/vendor/") && !strings.Contains(dirSlash, "/internal/") && !strings.HasSuffix(dirSlash, "/internal") {
|
||||
return true
|
||||
}
|
||||
// Vendor or internal directory only visible from children of parent.
|
||||
// That means the path from the current directory to the target directory
|
||||
// can contain ../vendor or ../internal but not ../foo/vendor or ../foo/internal
|
||||
// or bar/vendor or bar/internal.
|
||||
// After stripping all the leading ../, the only okay place to see vendor or internal
|
||||
// is at the very beginning of the path.
|
||||
absfile, err := filepath.Abs(filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
absdir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
rel, err := filepath.Rel(absfile, absdir)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
relSlash := filepath.ToSlash(rel)
|
||||
if i := strings.LastIndex(relSlash, "../"); i >= 0 {
|
||||
relSlash = relSlash[i+len("../"):]
|
||||
}
|
||||
return !strings.Contains(relSlash, "/vendor/") && !strings.Contains(relSlash, "/internal/") && !strings.HasSuffix(relSlash, "/internal")
|
||||
}
|
||||
|
||||
// lastTwoComponents returns at most the last two path components
|
||||
// of v, using either / or \ as the path separator.
|
||||
func lastTwoComponents(v string) string {
|
||||
nslash := 0
|
||||
for i := len(v) - 1; i >= 0; i-- {
|
||||
if v[i] == '/' || v[i] == '\\' {
|
||||
nslash++
|
||||
if nslash == 2 {
|
||||
return v[i:]
|
||||
}
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
type visitFn func(node ast.Node) ast.Visitor
|
||||
|
||||
func (fn visitFn) Visit(node ast.Node) ast.Visitor {
|
||||
return fn(node)
|
||||
}
|
||||
|
||||
func findImportStdlib(shortPkg string, symbols map[string]bool) (importPath string, ok bool) {
|
||||
for symbol := range symbols {
|
||||
key := shortPkg + "." + symbol
|
||||
path := stdlib[key]
|
||||
if path == "" {
|
||||
if key == "rand.Read" {
|
||||
continue
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
if importPath != "" && importPath != path {
|
||||
// Ambiguous. Symbols pointed to different things.
|
||||
return "", false
|
||||
}
|
||||
importPath = path
|
||||
}
|
||||
if importPath == "" && shortPkg == "rand" && symbols["Read"] {
|
||||
return "crypto/rand", true
|
||||
}
|
||||
return importPath, importPath != ""
|
||||
}
|
310
vendor/golang.org/x/tools/imports/imports.go
generated
vendored
310
vendor/golang.org/x/tools/imports/imports.go
generated
vendored
@ -1,310 +0,0 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:generate go run mkstdlib.go
|
||||
|
||||
// Package imports implements a Go pretty-printer (like package "go/format")
|
||||
// that also adds or removes import statements as necessary.
|
||||
package imports // import "golang.org/x/tools/imports"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/ast/astutil"
|
||||
)
|
||||
|
||||
// Options specifies options for processing files.
|
||||
type Options struct {
|
||||
Fragment bool // Accept fragment of a source file (no package statement)
|
||||
AllErrors bool // Report all errors (not just the first 10 on different lines)
|
||||
|
||||
Comments bool // Print comments (true if nil *Options provided)
|
||||
TabIndent bool // Use tabs for indent (true if nil *Options provided)
|
||||
TabWidth int // Tab width (8 if nil *Options provided)
|
||||
|
||||
FormatOnly bool // Disable the insertion and deletion of imports
|
||||
}
|
||||
|
||||
// Process formats and adjusts imports for the provided file.
|
||||
// If opt is nil the defaults are used.
|
||||
//
|
||||
// Note that filename's directory influences which imports can be chosen,
|
||||
// so it is important that filename be accurate.
|
||||
// To process data ``as if'' it were in filename, pass the data as a non-nil src.
|
||||
func Process(filename string, src []byte, opt *Options) ([]byte, error) {
|
||||
if opt == nil {
|
||||
opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
|
||||
}
|
||||
if src == nil {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
src = b
|
||||
}
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
file, adjust, err := parse(fileSet, filename, src, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !opt.FormatOnly {
|
||||
_, err = fixImports(fileSet, file, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
sortImports(fileSet, file)
|
||||
imps := astutil.Imports(fileSet, file)
|
||||
var spacesBefore []string // import paths we need spaces before
|
||||
for _, impSection := range imps {
|
||||
// Within each block of contiguous imports, see if any
|
||||
// import lines are in different group numbers. If so,
|
||||
// we'll need to put a space between them so it's
|
||||
// compatible with gofmt.
|
||||
lastGroup := -1
|
||||
for _, importSpec := range impSection {
|
||||
importPath, _ := strconv.Unquote(importSpec.Path.Value)
|
||||
groupNum := importGroup(importPath)
|
||||
if groupNum != lastGroup && lastGroup != -1 {
|
||||
spacesBefore = append(spacesBefore, importPath)
|
||||
}
|
||||
lastGroup = groupNum
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printerMode := printer.UseSpaces
|
||||
if opt.TabIndent {
|
||||
printerMode |= printer.TabIndent
|
||||
}
|
||||
printConfig := &printer.Config{Mode: printerMode, Tabwidth: opt.TabWidth}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = printConfig.Fprint(&buf, fileSet, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := buf.Bytes()
|
||||
if adjust != nil {
|
||||
out = adjust(src, out)
|
||||
}
|
||||
if len(spacesBefore) > 0 {
|
||||
out, err = addImportSpaces(bytes.NewReader(out), spacesBefore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
out, err = format.Source(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// parse parses src, which was read from filename,
|
||||
// as a Go source file or statement list.
|
||||
func parse(fset *token.FileSet, filename string, src []byte, opt *Options) (*ast.File, func(orig, src []byte) []byte, error) {
|
||||
parserMode := parser.Mode(0)
|
||||
if opt.Comments {
|
||||
parserMode |= parser.ParseComments
|
||||
}
|
||||
if opt.AllErrors {
|
||||
parserMode |= parser.AllErrors
|
||||
}
|
||||
|
||||
// Try as whole source file.
|
||||
file, err := parser.ParseFile(fset, filename, src, parserMode)
|
||||
if err == nil {
|
||||
return file, nil, nil
|
||||
}
|
||||
// If the error is that the source file didn't begin with a
|
||||
// package line and we accept fragmented input, fall through to
|
||||
// try as a source fragment. Stop and return on any other error.
|
||||
if !opt.Fragment || !strings.Contains(err.Error(), "expected 'package'") {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// If this is a declaration list, make it a source file
|
||||
// by inserting a package clause.
|
||||
// Insert using a ;, not a newline, so that parse errors are on
|
||||
// the correct line.
|
||||
const prefix = "package main;"
|
||||
psrc := append([]byte(prefix), src...)
|
||||
file, err = parser.ParseFile(fset, filename, psrc, parserMode)
|
||||
if err == nil {
|
||||
// Gofmt will turn the ; into a \n.
|
||||
// Do that ourselves now and update the file contents,
|
||||
// so that positions and line numbers are correct going forward.
|
||||
psrc[len(prefix)-1] = '\n'
|
||||
fset.File(file.Package).SetLinesForContent(psrc)
|
||||
|
||||
// If a main function exists, we will assume this is a main
|
||||
// package and leave the file.
|
||||
if containsMainFunc(file) {
|
||||
return file, nil, nil
|
||||
}
|
||||
|
||||
adjust := func(orig, src []byte) []byte {
|
||||
// Remove the package clause.
|
||||
src = src[len(prefix):]
|
||||
return matchSpace(orig, src)
|
||||
}
|
||||
return file, adjust, nil
|
||||
}
|
||||
// If the error is that the source file didn't begin with a
|
||||
// declaration, fall through to try as a statement list.
|
||||
// Stop and return on any other error.
|
||||
if !strings.Contains(err.Error(), "expected declaration") {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// If this is a statement list, make it a source file
|
||||
// by inserting a package clause and turning the list
|
||||
// into a function body. This handles expressions too.
|
||||
// Insert using a ;, not a newline, so that the line numbers
|
||||
// in fsrc match the ones in src.
|
||||
fsrc := append(append([]byte("package p; func _() {"), src...), '}')
|
||||
file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
|
||||
if err == nil {
|
||||
adjust := func(orig, src []byte) []byte {
|
||||
// Remove the wrapping.
|
||||
// Gofmt has turned the ; into a \n\n.
|
||||
src = src[len("package p\n\nfunc _() {"):]
|
||||
src = src[:len(src)-len("}\n")]
|
||||
// Gofmt has also indented the function body one level.
|
||||
// Remove that indent.
|
||||
src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
|
||||
return matchSpace(orig, src)
|
||||
}
|
||||
return file, adjust, nil
|
||||
}
|
||||
|
||||
// Failed, and out of options.
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// containsMainFunc checks if a file contains a function declaration with the
|
||||
// function signature 'func main()'
|
||||
func containsMainFunc(file *ast.File) bool {
|
||||
for _, decl := range file.Decls {
|
||||
if f, ok := decl.(*ast.FuncDecl); ok {
|
||||
if f.Name.Name != "main" {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(f.Type.Params.List) != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if f.Type.Results != nil && len(f.Type.Results.List) != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func cutSpace(b []byte) (before, middle, after []byte) {
|
||||
i := 0
|
||||
for i < len(b) && (b[i] == ' ' || b[i] == '\t' || b[i] == '\n') {
|
||||
i++
|
||||
}
|
||||
j := len(b)
|
||||
for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
|
||||
j--
|
||||
}
|
||||
if i <= j {
|
||||
return b[:i], b[i:j], b[j:]
|
||||
}
|
||||
return nil, nil, b[j:]
|
||||
}
|
||||
|
||||
// matchSpace reformats src to use the same space context as orig.
|
||||
// 1) If orig begins with blank lines, matchSpace inserts them at the beginning of src.
|
||||
// 2) matchSpace copies the indentation of the first non-blank line in orig
|
||||
// to every non-blank line in src.
|
||||
// 3) matchSpace copies the trailing space from orig and uses it in place
|
||||
// of src's trailing space.
|
||||
func matchSpace(orig []byte, src []byte) []byte {
|
||||
before, _, after := cutSpace(orig)
|
||||
i := bytes.LastIndex(before, []byte{'\n'})
|
||||
before, indent := before[:i+1], before[i+1:]
|
||||
|
||||
_, src, _ = cutSpace(src)
|
||||
|
||||
var b bytes.Buffer
|
||||
b.Write(before)
|
||||
for len(src) > 0 {
|
||||
line := src
|
||||
if i := bytes.IndexByte(line, '\n'); i >= 0 {
|
||||
line, src = line[:i+1], line[i+1:]
|
||||
} else {
|
||||
src = nil
|
||||
}
|
||||
if len(line) > 0 && line[0] != '\n' { // not blank
|
||||
b.Write(indent)
|
||||
}
|
||||
b.Write(line)
|
||||
}
|
||||
b.Write(after)
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
var impLine = regexp.MustCompile(`^\s+(?:[\w\.]+\s+)?"(.+)"`)
|
||||
|
||||
func addImportSpaces(r io.Reader, breaks []string) ([]byte, error) {
|
||||
var out bytes.Buffer
|
||||
in := bufio.NewReader(r)
|
||||
inImports := false
|
||||
done := false
|
||||
for {
|
||||
s, err := in.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !inImports && !done && strings.HasPrefix(s, "import") {
|
||||
inImports = true
|
||||
}
|
||||
if inImports && (strings.HasPrefix(s, "var") ||
|
||||
strings.HasPrefix(s, "func") ||
|
||||
strings.HasPrefix(s, "const") ||
|
||||
strings.HasPrefix(s, "type")) {
|
||||
done = true
|
||||
inImports = false
|
||||
}
|
||||
if inImports && len(breaks) > 0 {
|
||||
if m := impLine.FindStringSubmatch(s); m != nil {
|
||||
if m[1] == breaks[0] {
|
||||
out.WriteByte('\n')
|
||||
breaks = breaks[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprint(&out, s)
|
||||
}
|
||||
return out.Bytes(), nil
|
||||
}
|
173
vendor/golang.org/x/tools/imports/mkindex.go
generated
vendored
173
vendor/golang.org/x/tools/imports/mkindex.go
generated
vendored
@ -1,173 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Command mkindex creates the file "pkgindex.go" containing an index of the Go
|
||||
// standard library. The file is intended to be built as part of the imports
|
||||
// package, so that the package may be used in environments where a GOROOT is
|
||||
// not available (such as App Engine).
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
pkgIndex = make(map[string][]pkg)
|
||||
exports = make(map[string]map[string]bool)
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Don't use GOPATH.
|
||||
ctx := build.Default
|
||||
ctx.GOPATH = ""
|
||||
|
||||
// Populate pkgIndex global from GOROOT.
|
||||
for _, path := range ctx.SrcDirs() {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
children, err := f.Readdir(-1)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
for _, child := range children {
|
||||
if child.IsDir() {
|
||||
loadPkg(path, child.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
// Populate exports global.
|
||||
for _, ps := range pkgIndex {
|
||||
for _, p := range ps {
|
||||
e := loadExports(p.dir)
|
||||
if e != nil {
|
||||
exports[p.dir] = e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Construct source file.
|
||||
var buf bytes.Buffer
|
||||
fmt.Fprint(&buf, pkgIndexHead)
|
||||
fmt.Fprintf(&buf, "var pkgIndexMaster = %#v\n", pkgIndex)
|
||||
fmt.Fprintf(&buf, "var exportsMaster = %#v\n", exports)
|
||||
src := buf.Bytes()
|
||||
|
||||
// Replace main.pkg type name with pkg.
|
||||
src = bytes.Replace(src, []byte("main.pkg"), []byte("pkg"), -1)
|
||||
// Replace actual GOROOT with "/go".
|
||||
src = bytes.Replace(src, []byte(ctx.GOROOT), []byte("/go"), -1)
|
||||
// Add some line wrapping.
|
||||
src = bytes.Replace(src, []byte("}, "), []byte("},\n"), -1)
|
||||
src = bytes.Replace(src, []byte("true, "), []byte("true,\n"), -1)
|
||||
|
||||
var err error
|
||||
src, err = format.Source(src)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Write out source file.
|
||||
err = ioutil.WriteFile("pkgindex.go", src, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
const pkgIndexHead = `package imports
|
||||
|
||||
func init() {
|
||||
pkgIndexOnce.Do(func() {
|
||||
pkgIndex.m = pkgIndexMaster
|
||||
})
|
||||
loadExports = func(dir string) map[string]bool {
|
||||
return exportsMaster[dir]
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
type pkg struct {
|
||||
importpath string // full pkg import path, e.g. "net/http"
|
||||
dir string // absolute file path to pkg directory e.g. "/usr/lib/go/src/fmt"
|
||||
}
|
||||
|
||||
var fset = token.NewFileSet()
|
||||
|
||||
func loadPkg(root, importpath string) {
|
||||
shortName := path.Base(importpath)
|
||||
if shortName == "testdata" {
|
||||
return
|
||||
}
|
||||
|
||||
dir := filepath.Join(root, importpath)
|
||||
pkgIndex[shortName] = append(pkgIndex[shortName], pkg{
|
||||
importpath: importpath,
|
||||
dir: dir,
|
||||
})
|
||||
|
||||
pkgDir, err := os.Open(dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
children, err := pkgDir.Readdir(-1)
|
||||
pkgDir.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, child := range children {
|
||||
name := child.Name()
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if c := name[0]; c == '.' || ('0' <= c && c <= '9') {
|
||||
continue
|
||||
}
|
||||
if child.IsDir() {
|
||||
loadPkg(root, filepath.Join(importpath, name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadExports(dir string) map[string]bool {
|
||||
exports := make(map[string]bool)
|
||||
buildPkg, err := build.ImportDir(dir, 0)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no buildable Go source files in") {
|
||||
return nil
|
||||
}
|
||||
log.Printf("could not import %q: %v", dir, err)
|
||||
return nil
|
||||
}
|
||||
for _, file := range buildPkg.GoFiles {
|
||||
f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0)
|
||||
if err != nil {
|
||||
log.Printf("could not parse %q: %v", file, err)
|
||||
continue
|
||||
}
|
||||
for name := range f.Scope.Objects {
|
||||
if ast.IsExported(name) {
|
||||
exports[name] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return exports
|
||||
}
|
108
vendor/golang.org/x/tools/imports/mkstdlib.go
generated
vendored
108
vendor/golang.org/x/tools/imports/mkstdlib.go
generated
vendored
@ -1,108 +0,0 @@
|
||||
// +build ignore
|
||||
|
||||
// mkstdlib generates the zstdlib.go file, containing the Go standard
|
||||
// library API symbols. It's baked into the binary to avoid scanning
|
||||
// GOPATH in the common case.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func mustOpen(name string) io.Reader {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func api(base string) string {
|
||||
return filepath.Join(runtime.GOROOT(), "api", base)
|
||||
}
|
||||
|
||||
var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
|
||||
|
||||
func main() {
|
||||
var buf bytes.Buffer
|
||||
outf := func(format string, args ...interface{}) {
|
||||
fmt.Fprintf(&buf, format, args...)
|
||||
}
|
||||
outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
|
||||
outf("package imports\n")
|
||||
outf("var stdlib = map[string]string{\n")
|
||||
f := io.MultiReader(
|
||||
mustOpen(api("go1.txt")),
|
||||
mustOpen(api("go1.1.txt")),
|
||||
mustOpen(api("go1.2.txt")),
|
||||
mustOpen(api("go1.3.txt")),
|
||||
mustOpen(api("go1.4.txt")),
|
||||
mustOpen(api("go1.5.txt")),
|
||||
mustOpen(api("go1.6.txt")),
|
||||
mustOpen(api("go1.7.txt")),
|
||||
mustOpen(api("go1.8.txt")),
|
||||
mustOpen(api("go1.9.txt")),
|
||||
mustOpen(api("go1.10.txt")),
|
||||
mustOpen(api("go1.11.txt")),
|
||||
)
|
||||
sc := bufio.NewScanner(f)
|
||||
fullImport := map[string]string{} // "zip.NewReader" => "archive/zip"
|
||||
ambiguous := map[string]bool{}
|
||||
var keys []string
|
||||
for sc.Scan() {
|
||||
l := sc.Text()
|
||||
has := func(v string) bool { return strings.Contains(l, v) }
|
||||
if has("struct, ") || has("interface, ") || has(", method (") {
|
||||
continue
|
||||
}
|
||||
if m := sym.FindStringSubmatch(l); m != nil {
|
||||
full := m[1]
|
||||
key := path.Base(full) + "." + m[2]
|
||||
if exist, ok := fullImport[key]; ok {
|
||||
if exist != full {
|
||||
ambiguous[key] = true
|
||||
}
|
||||
} else {
|
||||
fullImport[key] = full
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := sc.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if ambiguous[key] {
|
||||
outf("\t// %q is ambiguous\n", key)
|
||||
} else {
|
||||
outf("\t%q: %q,\n", key, fullImport[key])
|
||||
}
|
||||
}
|
||||
outf("\n")
|
||||
for _, sym := range [...]string{"Alignof", "ArbitraryType", "Offsetof", "Pointer", "Sizeof"} {
|
||||
outf("\t%q: %q,\n", "unsafe."+sym, "unsafe")
|
||||
}
|
||||
outf("}\n")
|
||||
fmtbuf, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
230
vendor/golang.org/x/tools/imports/sortimports.go
generated
vendored
230
vendor/golang.org/x/tools/imports/sortimports.go
generated
vendored
@ -1,230 +0,0 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Hacked up copy of go/ast/import.go
|
||||
|
||||
package imports
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// sortImports sorts runs of consecutive import lines in import blocks in f.
|
||||
// It also removes duplicate imports when it is possible to do so without data loss.
|
||||
func sortImports(fset *token.FileSet, f *ast.File) {
|
||||
for i, d := range f.Decls {
|
||||
d, ok := d.(*ast.GenDecl)
|
||||
if !ok || d.Tok != token.IMPORT {
|
||||
// Not an import declaration, so we're done.
|
||||
// Imports are always first.
|
||||
break
|
||||
}
|
||||
|
||||
if len(d.Specs) == 0 {
|
||||
// Empty import block, remove it.
|
||||
f.Decls = append(f.Decls[:i], f.Decls[i+1:]...)
|
||||
}
|
||||
|
||||
if !d.Lparen.IsValid() {
|
||||
// Not a block: sorted by default.
|
||||
continue
|
||||
}
|
||||
|
||||
// Identify and sort runs of specs on successive lines.
|
||||
i := 0
|
||||
specs := d.Specs[:0]
|
||||
for j, s := range d.Specs {
|
||||
if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
|
||||
// j begins a new run. End this one.
|
||||
specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
|
||||
i = j
|
||||
}
|
||||
}
|
||||
specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)
|
||||
d.Specs = specs
|
||||
|
||||
// Deduping can leave a blank line before the rparen; clean that up.
|
||||
if len(d.Specs) > 0 {
|
||||
lastSpec := d.Specs[len(d.Specs)-1]
|
||||
lastLine := fset.Position(lastSpec.Pos()).Line
|
||||
if rParenLine := fset.Position(d.Rparen).Line; rParenLine > lastLine+1 {
|
||||
fset.File(d.Rparen).MergeLine(rParenLine - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func importPath(s ast.Spec) string {
|
||||
t, err := strconv.Unquote(s.(*ast.ImportSpec).Path.Value)
|
||||
if err == nil {
|
||||
return t
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func importName(s ast.Spec) string {
|
||||
n := s.(*ast.ImportSpec).Name
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
return n.Name
|
||||
}
|
||||
|
||||
func importComment(s ast.Spec) string {
|
||||
c := s.(*ast.ImportSpec).Comment
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.Text()
|
||||
}
|
||||
|
||||
// collapse indicates whether prev may be removed, leaving only next.
|
||||
func collapse(prev, next ast.Spec) bool {
|
||||
if importPath(next) != importPath(prev) || importName(next) != importName(prev) {
|
||||
return false
|
||||
}
|
||||
return prev.(*ast.ImportSpec).Comment == nil
|
||||
}
|
||||
|
||||
type posSpan struct {
|
||||
Start token.Pos
|
||||
End token.Pos
|
||||
}
|
||||
|
||||
func sortSpecs(fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec {
|
||||
// Can't short-circuit here even if specs are already sorted,
|
||||
// since they might yet need deduplication.
|
||||
// A lone import, however, may be safely ignored.
|
||||
if len(specs) <= 1 {
|
||||
return specs
|
||||
}
|
||||
|
||||
// Record positions for specs.
|
||||
pos := make([]posSpan, len(specs))
|
||||
for i, s := range specs {
|
||||
pos[i] = posSpan{s.Pos(), s.End()}
|
||||
}
|
||||
|
||||
// Identify comments in this range.
|
||||
// Any comment from pos[0].Start to the final line counts.
|
||||
lastLine := fset.Position(pos[len(pos)-1].End).Line
|
||||
cstart := len(f.Comments)
|
||||
cend := len(f.Comments)
|
||||
for i, g := range f.Comments {
|
||||
if g.Pos() < pos[0].Start {
|
||||
continue
|
||||
}
|
||||
if i < cstart {
|
||||
cstart = i
|
||||
}
|
||||
if fset.Position(g.End()).Line > lastLine {
|
||||
cend = i
|
||||
break
|
||||
}
|
||||
}
|
||||
comments := f.Comments[cstart:cend]
|
||||
|
||||
// Assign each comment to the import spec preceding it.
|
||||
importComment := map[*ast.ImportSpec][]*ast.CommentGroup{}
|
||||
specIndex := 0
|
||||
for _, g := range comments {
|
||||
for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
|
||||
specIndex++
|
||||
}
|
||||
s := specs[specIndex].(*ast.ImportSpec)
|
||||
importComment[s] = append(importComment[s], g)
|
||||
}
|
||||
|
||||
// Sort the import specs by import path.
|
||||
// Remove duplicates, when possible without data loss.
|
||||
// Reassign the import paths to have the same position sequence.
|
||||
// Reassign each comment to abut the end of its spec.
|
||||
// Sort the comments by new position.
|
||||
sort.Sort(byImportSpec(specs))
|
||||
|
||||
// Dedup. Thanks to our sorting, we can just consider
|
||||
// adjacent pairs of imports.
|
||||
deduped := specs[:0]
|
||||
for i, s := range specs {
|
||||
if i == len(specs)-1 || !collapse(s, specs[i+1]) {
|
||||
deduped = append(deduped, s)
|
||||
} else {
|
||||
p := s.Pos()
|
||||
fset.File(p).MergeLine(fset.Position(p).Line)
|
||||
}
|
||||
}
|
||||
specs = deduped
|
||||
|
||||
// Fix up comment positions
|
||||
for i, s := range specs {
|
||||
s := s.(*ast.ImportSpec)
|
||||
if s.Name != nil {
|
||||
s.Name.NamePos = pos[i].Start
|
||||
}
|
||||
s.Path.ValuePos = pos[i].Start
|
||||
s.EndPos = pos[i].End
|
||||
nextSpecPos := pos[i].End
|
||||
|
||||
for _, g := range importComment[s] {
|
||||
for _, c := range g.List {
|
||||
c.Slash = pos[i].End
|
||||
nextSpecPos = c.End()
|
||||
}
|
||||
}
|
||||
if i < len(specs)-1 {
|
||||
pos[i+1].Start = nextSpecPos
|
||||
pos[i+1].End = nextSpecPos
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(byCommentPos(comments))
|
||||
|
||||
// Fixup comments can insert blank lines, because import specs are on different lines.
|
||||
// We remove those blank lines here by merging import spec to the first import spec line.
|
||||
firstSpecLine := fset.Position(specs[0].Pos()).Line
|
||||
for _, s := range specs[1:] {
|
||||
p := s.Pos()
|
||||
line := fset.File(p).Line(p)
|
||||
for previousLine := line - 1; previousLine >= firstSpecLine; {
|
||||
fset.File(p).MergeLine(previousLine)
|
||||
previousLine--
|
||||
}
|
||||
}
|
||||
return specs
|
||||
}
|
||||
|
||||
type byImportSpec []ast.Spec // slice of *ast.ImportSpec
|
||||
|
||||
func (x byImportSpec) Len() int { return len(x) }
|
||||
func (x byImportSpec) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
func (x byImportSpec) Less(i, j int) bool {
|
||||
ipath := importPath(x[i])
|
||||
jpath := importPath(x[j])
|
||||
|
||||
igroup := importGroup(ipath)
|
||||
jgroup := importGroup(jpath)
|
||||
if igroup != jgroup {
|
||||
return igroup < jgroup
|
||||
}
|
||||
|
||||
if ipath != jpath {
|
||||
return ipath < jpath
|
||||
}
|
||||
iname := importName(x[i])
|
||||
jname := importName(x[j])
|
||||
|
||||
if iname != jname {
|
||||
return iname < jname
|
||||
}
|
||||
return importComment(x[i]) < importComment(x[j])
|
||||
}
|
||||
|
||||
type byCommentPos []*ast.CommentGroup
|
||||
|
||||
func (x byCommentPos) Len() int { return len(x) }
|
||||
func (x byCommentPos) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() }
|
9970
vendor/golang.org/x/tools/imports/zstdlib.go
generated
vendored
9970
vendor/golang.org/x/tools/imports/zstdlib.go
generated
vendored
File diff suppressed because it is too large
Load Diff
7
vendor/modules.txt
vendored
7
vendor/modules.txt
vendored
@ -99,15 +99,15 @@ github.com/golangci/misspell
|
||||
github.com/golangci/prealloc
|
||||
# github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
|
||||
github.com/golangci/revgrep
|
||||
# github.com/golangci/tools v0.0.0-20180902102414-ed64e33c8c8b
|
||||
# github.com/golangci/tools v0.0.0-20180902102414-2cefd77fef9b
|
||||
github.com/golangci/tools/go/ssa
|
||||
github.com/golangci/tools/go/ssa/ssautil
|
||||
github.com/golangci/tools/imports
|
||||
github.com/golangci/tools/go/ssa/ssautil
|
||||
github.com/golangci/tools/internal/gopathwalk
|
||||
github.com/golangci/tools/go/callgraph
|
||||
github.com/golangci/tools/go/callgraph/cha
|
||||
github.com/golangci/tools/go/callgraph/rta
|
||||
github.com/golangci/tools/go/callgraph/static
|
||||
github.com/golangci/tools/internal/gopathwalk
|
||||
github.com/golangci/tools/internal/fastwalk
|
||||
# github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
|
||||
github.com/golangci/unconvert
|
||||
@ -196,7 +196,6 @@ golang.org/x/text/unicode/norm
|
||||
# golang.org/x/tools v0.0.0-20180831211245-6c7e314b6563
|
||||
golang.org/x/tools/go/loader
|
||||
golang.org/x/tools/go/packages
|
||||
golang.org/x/tools/imports
|
||||
golang.org/x/tools/go/ast/astutil
|
||||
golang.org/x/tools/go/types/typeutil
|
||||
golang.org/x/tools/go/gcexportdata
|
||||
|
Loading…
x
Reference in New Issue
Block a user