From 8c845a0eeb4c4aa319b7ad05292b3877bf52b448 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 2 Sep 2021 22:58:40 -0600 Subject: [PATCH 1/8] chore: re-generate with Go 1.17 Signed-off-by: Luke Shumaker --- pkg/gci/std.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/gci/std.go b/pkg/gci/std.go index ac96b55..5b000ab 100644 --- a/pkg/gci/std.go +++ b/pkg/gci/std.go @@ -1,6 +1,6 @@ package gci -// Code generated based on go1.16beta1. DO NOT EDIT. +// Code generated based on go1.17. DO NOT EDIT. var standardPackages = map[string]struct{}{ "archive/tar": {}, @@ -63,6 +63,7 @@ var standardPackages = map[string]struct{}{ "fmt": {}, "go/ast": {}, "go/build": {}, + "go/build/constraint": {}, "go/constant": {}, "go/doc": {}, "go/format": {}, From 8d1bff7ee24205fb85b5288ed6abaf2e36c362e4 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 2 Sep 2021 23:39:09 -0600 Subject: [PATCH 2/8] fix: README.md: Remove obsolete TODO item I implemented this in #33 but forgot to update README.md Signed-off-by: Luke Shumaker --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 3effd32..e3948a6 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,5 @@ import ( ## TODO -- Support multi-3rd-party packages - Support multiple lines of comment in import block - Add testcases From 9382acbe5c529418ceb26a9c93f34649daaf1515 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 2 Sep 2021 23:41:50 -0600 Subject: [PATCH 3/8] tests: add tests based on the examples in README.md Signed-off-by: Luke Shumaker --- README.md | 8 +-- go.mod | 5 +- go.sum | 10 ++++ pkg/gci/gci_test.go | 52 +++++++++++++++++++ pkg/gci/testdata/already-good.in.go | 8 +++ pkg/gci/testdata/already-good.out.go | 8 +++ pkg/gci/testdata/simple-case.in.go | 8 +++ pkg/gci/testdata/simple-case.out.go | 8 +++ .../with-above-comment-and-alias.in.go | 7 +++ .../with-above-comment-and-alias.out.go | 9 ++++ pkg/gci/testdata/with-alias.in.go | 6 +++ pkg/gci/testdata/with-alias.out.go | 8 +++ pkg/gci/testdata/with-comment-and-alias.in.go | 6 +++ .../testdata/with-comment-and-alias.out.go | 9 ++++ 14 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 pkg/gci/testdata/already-good.in.go create mode 100644 pkg/gci/testdata/already-good.out.go create mode 100644 pkg/gci/testdata/simple-case.in.go create mode 100644 pkg/gci/testdata/simple-case.out.go create mode 100644 pkg/gci/testdata/with-above-comment-and-alias.in.go create mode 100644 pkg/gci/testdata/with-above-comment-and-alias.out.go create mode 100644 pkg/gci/testdata/with-alias.in.go create mode 100644 pkg/gci/testdata/with-alias.out.go create mode 100644 pkg/gci/testdata/with-comment-and-alias.in.go create mode 100644 pkg/gci/testdata/with-comment-and-alias.out.go diff --git a/README.md b/README.md index e3948a6..5edc1d9 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ package main import ( "fmt" go "github.com/golang" - "github.com/daixiang0" + "github.com/daixiang0/gci" ) ``` @@ -82,7 +82,7 @@ package main import ( "fmt" _ "github.com/golang" // golang - "github.com/daixiang0" + "github.com/daixiang0/gci" ) ``` @@ -108,7 +108,7 @@ import ( "fmt" // golang _ "github.com/golang" - "github.com/daixiang0" + "github.com/daixiang0/gci" ) ``` @@ -129,4 +129,4 @@ import ( ## TODO - Support multiple lines of comment in import block -- Add testcases +- Add more testcases diff --git a/go.mod b/go.mod index 8eae436..a185467 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/daixiang0/gci go 1.14 -require golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394 +require ( + github.com/stretchr/testify v1.7.0 // indirect + golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394 +) diff --git a/go.sum b/go.sum index bfc445e..356499e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,10 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -22,3 +29,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/gci/gci_test.go b/pkg/gci/gci_test.go index b8644b8..bd9cbdc 100644 --- a/pkg/gci/gci_test.go +++ b/pkg/gci/gci_test.go @@ -1,8 +1,14 @@ package gci import ( + "bytes" "fmt" + "io/ioutil" + "path/filepath" + "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetPkgType(t *testing.T) { @@ -62,3 +68,49 @@ func TestGetPkgType(t *testing.T) { }) } } + +func TestRun(t *testing.T) { + fileinfos, err := ioutil.ReadDir("testdata") + if err != nil { + t.Fatal(err) + } + for _, fileinfo := range fileinfos { + inname := fileinfo.Name() + if strings.HasPrefix(inname, ".") || !strings.HasSuffix(inname, ".in.go") { + continue + } + name := strings.TrimSuffix(inname, ".in.go") + t.Run(name, func(t *testing.T) { + t.Parallel() + + expectedInput, err := ioutil.ReadFile(filepath.Join("testdata", inname)) + if err != nil { + t.Fatal(err) + } + outname := name + ".out.go" + expectedOutput, err := ioutil.ReadFile(filepath.Join("testdata", outname)) + if err != nil { + t.Fatal(err) + } + + flagSet := &FlagSet{ + LocalFlag: []string{ + "github.com/daixiang0", + "github.com/local", + }, + } + + actualInput, actualOutput, err := Run(filepath.Join("testdata", inname), flagSet) + assert.Equal(t, string(expectedInput), string(actualInput), "input") + if bytes.Equal(expectedInput, expectedOutput) { + assert.Nil(t, actualOutput, "output") + + } + if actualOutput == nil { + actualOutput = actualInput + } + assert.Equal(t, string(expectedOutput), string(actualOutput), "output") + assert.NoError(t, err) + }) + } +} diff --git a/pkg/gci/testdata/already-good.in.go b/pkg/gci/testdata/already-good.in.go new file mode 100644 index 0000000..89de7a0 --- /dev/null +++ b/pkg/gci/testdata/already-good.in.go @@ -0,0 +1,8 @@ +package main +import ( + "fmt" + + g "github.com/golang" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/already-good.out.go b/pkg/gci/testdata/already-good.out.go new file mode 100644 index 0000000..89de7a0 --- /dev/null +++ b/pkg/gci/testdata/already-good.out.go @@ -0,0 +1,8 @@ +package main +import ( + "fmt" + + g "github.com/golang" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/simple-case.in.go b/pkg/gci/testdata/simple-case.in.go new file mode 100644 index 0000000..c29b5f9 --- /dev/null +++ b/pkg/gci/testdata/simple-case.in.go @@ -0,0 +1,8 @@ +package main +import ( + "golang.org/x/tools" + + "fmt" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/simple-case.out.go b/pkg/gci/testdata/simple-case.out.go new file mode 100644 index 0000000..1c0e49f --- /dev/null +++ b/pkg/gci/testdata/simple-case.out.go @@ -0,0 +1,8 @@ +package main +import ( + "fmt" + + "golang.org/x/tools" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-above-comment-and-alias.in.go b/pkg/gci/testdata/with-above-comment-and-alias.in.go new file mode 100644 index 0000000..ab81ae9 --- /dev/null +++ b/pkg/gci/testdata/with-above-comment-and-alias.in.go @@ -0,0 +1,7 @@ +package main +import ( + "fmt" + // golang + _ "github.com/golang" + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-above-comment-and-alias.out.go b/pkg/gci/testdata/with-above-comment-and-alias.out.go new file mode 100644 index 0000000..13c89f9 --- /dev/null +++ b/pkg/gci/testdata/with-above-comment-and-alias.out.go @@ -0,0 +1,9 @@ +package main +import ( + "fmt" + + // golang + _ "github.com/golang" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-alias.in.go b/pkg/gci/testdata/with-alias.in.go new file mode 100644 index 0000000..692961d --- /dev/null +++ b/pkg/gci/testdata/with-alias.in.go @@ -0,0 +1,6 @@ +package main +import ( + "fmt" + g "github.com/golang" + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-alias.out.go b/pkg/gci/testdata/with-alias.out.go new file mode 100644 index 0000000..89de7a0 --- /dev/null +++ b/pkg/gci/testdata/with-alias.out.go @@ -0,0 +1,8 @@ +package main +import ( + "fmt" + + g "github.com/golang" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-comment-and-alias.in.go b/pkg/gci/testdata/with-comment-and-alias.in.go new file mode 100644 index 0000000..5457ded --- /dev/null +++ b/pkg/gci/testdata/with-comment-and-alias.in.go @@ -0,0 +1,6 @@ +package main +import ( + "fmt" + _ "github.com/golang" // golang + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/with-comment-and-alias.out.go b/pkg/gci/testdata/with-comment-and-alias.out.go new file mode 100644 index 0000000..13c89f9 --- /dev/null +++ b/pkg/gci/testdata/with-comment-and-alias.out.go @@ -0,0 +1,9 @@ +package main +import ( + "fmt" + + // golang + _ "github.com/golang" + + "github.com/daixiang0/gci" +) From 485f5590b49624fd7af1fbfe2d8120a7b314bb4c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Sep 2021 01:37:41 -0600 Subject: [PATCH 4/8] fix: handling of whitespace between import-spec components Signed-off-by: Luke Shumaker --- pkg/gci/gci.go | 8 ++++---- pkg/gci/testdata/whitespace-test.in.go | 7 +++++++ pkg/gci/testdata/whitespace-test.out.go | 10 ++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 pkg/gci/testdata/whitespace-test.in.go create mode 100644 pkg/gci/testdata/whitespace-test.out.go diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 7efa576..9e00e67 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -148,14 +148,14 @@ func (p *pkg) fmt() []byte { func getPkgInfo(line string, comment bool) (string, string, string) { if comment { s := strings.Split(line, commentFlag) - pkgArray := strings.Split(s[0], blank) + pkgArray := strings.Fields(s[0]) if len(pkgArray) > 1 { return pkgArray[1], pkgArray[0], fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) } else { - return strings.TrimSpace(pkgArray[0]), "", fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) + return pkgArray[0], "", fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) } } else { - pkgArray := strings.Split(line, blank) + pkgArray := strings.Fields(line) if len(pkgArray) > 1 { return pkgArray[1], pkgArray[0], "" } else { @@ -363,7 +363,7 @@ func Run(filename string, set *FlagSet) ([]byte, []byte, error) { return nil, nil, nil } end := bytes.Index(src[start:], importEndFlag) + start - + // in case import flags are part of a codegen template, or otherwise "wrong" if start+len(importStartFlag) > end { return nil, nil, nil diff --git a/pkg/gci/testdata/whitespace-test.in.go b/pkg/gci/testdata/whitespace-test.in.go new file mode 100644 index 0000000..f4a4918 --- /dev/null +++ b/pkg/gci/testdata/whitespace-test.in.go @@ -0,0 +1,7 @@ +package main + +import ( + "fmt" + "github.com/golang" // golang + alias "github.com/daixiang0/gci" +) diff --git a/pkg/gci/testdata/whitespace-test.out.go b/pkg/gci/testdata/whitespace-test.out.go new file mode 100644 index 0000000..b78c9df --- /dev/null +++ b/pkg/gci/testdata/whitespace-test.out.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + + // golang + "github.com/golang" + + alias "github.com/daixiang0/gci" +) From 0797283e3d49fcd562fb061042779f68475f8d4f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Sep 2021 00:18:09 -0600 Subject: [PATCH 5/8] style: use a 'PkgType' type instead of just an int Signed-off-by: Luke Shumaker --- pkg/gci/gci.go | 21 +++++++++++---------- pkg/gci/gci_test.go | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 9e00e67..a58f86e 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -12,13 +12,17 @@ import ( "strings" ) +type PkgType int + const ( // pkg type: standard, remote, local - standard int = iota + standard PkgType = iota // 3rd-party packages remote local +) +const ( commentFlag = "//" ) @@ -37,7 +41,7 @@ type FlagSet struct { } type pkg struct { - list map[int][]string + list map[PkgType][]string comment map[string]string alias map[string]string } @@ -51,13 +55,10 @@ func ParseLocalFlag(str string) []string { } func newPkg(data [][]byte, localFlag []string) *pkg { - listMap := make(map[int][]string) - commentMap := make(map[string]string) - aliasMap := make(map[string]string) p := &pkg{ - list: listMap, - comment: commentMap, - alias: aliasMap, + list: make(map[PkgType][]string), + comment: make(map[string]string), + alias: make(map[string]string), } formatData := make([]string, 0) @@ -113,7 +114,7 @@ func newPkg(data [][]byte, localFlag []string) *pkg { func (p *pkg) fmt() []byte { ret := make([]string, 0, 100) - for pkgType := range []int{standard, remote, local} { + for _, pkgType := range []PkgType{standard, remote, local} { sort.Strings(p.list[pkgType]) for _, s := range p.list[pkgType] { if p.comment[s] != "" { @@ -164,7 +165,7 @@ func getPkgInfo(line string, comment bool) (string, string, string) { } } -func getPkgType(line string, localFlag []string) int { +func getPkgType(line string, localFlag []string) PkgType { pkgName := strings.Trim(line, "\"\\`") for _, localPkg := range localFlag { diff --git a/pkg/gci/gci_test.go b/pkg/gci/gci_test.go index bd9cbdc..97a1d06 100644 --- a/pkg/gci/gci_test.go +++ b/pkg/gci/gci_test.go @@ -15,7 +15,7 @@ func TestGetPkgType(t *testing.T) { testCases := []struct { Line string LocalFlag string - ExpectedResult int + ExpectedResult PkgType }{ {Line: `"foo/pkg/bar"`, LocalFlag: "", ExpectedResult: remote}, {Line: `"foo/pkg/bar"`, LocalFlag: "foo", ExpectedResult: local}, From 015719d57d63aecf4a5789f4a64ec53245eae151 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Sep 2021 01:18:31 -0600 Subject: [PATCH 6/8] fix: don't insert an extra newline for leading comments Signed-off-by: Luke Shumaker --- pkg/gci/gci.go | 32 +++++++++++-------------- pkg/gci/testdata/leading-comment.in.go | 6 +++++ pkg/gci/testdata/leading-comment.out.go | 6 +++++ 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 pkg/gci/testdata/leading-comment.in.go create mode 100644 pkg/gci/testdata/leading-comment.out.go diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index a58f86e..3753727 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -112,37 +112,33 @@ func newPkg(data [][]byte, localFlag []string) *pkg { // fmt format import pkgs as expected func (p *pkg) fmt() []byte { - ret := make([]string, 0, 100) + var lines []string for _, pkgType := range []PkgType{standard, remote, local} { + if len(p.list[pkgType]) == 0 { + continue + } + if len(lines) > 0 && lines[len(lines)-1] != "" { + lines = append(lines, "") + } sort.Strings(p.list[pkgType]) for _, s := range p.list[pkgType] { if p.comment[s] != "" { - l := fmt.Sprintf("%s%s%s%s", linebreak, indent, p.comment[s], linebreak) - ret = append(ret, l) + if len(lines) > 0 && lines[len(lines)-1] != "" { + lines = append(lines, "") + } + lines = append(lines, indent+p.comment[s]) } if p.alias[s] != "" { - s = fmt.Sprintf("%s%s%s%s%s", indent, p.alias[s], blank, s, linebreak) + lines = append(lines, indent+p.alias[s]+blank+s) } else { - s = fmt.Sprintf("%s%s%s", indent, s, linebreak) + lines = append(lines, indent+s) } - - ret = append(ret, s) } - - if len(p.list[pkgType]) > 0 { - ret = append(ret, linebreak) - } - } - if len(ret) > 0 && ret[len(ret)-1] == linebreak { - ret = ret[:len(ret)-1] } - // remove duplicate empty lines - s1 := fmt.Sprintf("%s%s%s%s", linebreak, linebreak, linebreak, indent) - s2 := fmt.Sprintf("%s%s%s", linebreak, linebreak, indent) - return []byte(strings.ReplaceAll(strings.Join(ret, ""), s1, s2)) + return []byte(strings.Join(lines, linebreak) + linebreak) } // getPkgInfo assume line is a import path, and return (path, alias, comment) diff --git a/pkg/gci/testdata/leading-comment.in.go b/pkg/gci/testdata/leading-comment.in.go new file mode 100644 index 0000000..b1aa143 --- /dev/null +++ b/pkg/gci/testdata/leading-comment.in.go @@ -0,0 +1,6 @@ +package main + +import ( + // foo + "fmt" +) diff --git a/pkg/gci/testdata/leading-comment.out.go b/pkg/gci/testdata/leading-comment.out.go new file mode 100644 index 0000000..b1aa143 --- /dev/null +++ b/pkg/gci/testdata/leading-comment.out.go @@ -0,0 +1,6 @@ +package main + +import ( + // foo + "fmt" +) From 2981cd6f152711fe2af1d73a0fd268664f21ed17 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Sep 2021 01:26:03 -0600 Subject: [PATCH 7/8] fix: don't mangle comments with "//" in them Signed-off-by: Luke Shumaker --- pkg/gci/gci.go | 2 +- pkg/gci/testdata/comment-with-slashslash.in.go | 5 +++++ pkg/gci/testdata/comment-with-slashslash.out.go | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 pkg/gci/testdata/comment-with-slashslash.in.go create mode 100644 pkg/gci/testdata/comment-with-slashslash.out.go diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 3753727..fa6219a 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -144,7 +144,7 @@ func (p *pkg) fmt() []byte { // getPkgInfo assume line is a import path, and return (path, alias, comment) func getPkgInfo(line string, comment bool) (string, string, string) { if comment { - s := strings.Split(line, commentFlag) + s := strings.SplitN(line, commentFlag, 2) pkgArray := strings.Fields(s[0]) if len(pkgArray) > 1 { return pkgArray[1], pkgArray[0], fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) diff --git a/pkg/gci/testdata/comment-with-slashslash.in.go b/pkg/gci/testdata/comment-with-slashslash.in.go new file mode 100644 index 0000000..73464d9 --- /dev/null +++ b/pkg/gci/testdata/comment-with-slashslash.in.go @@ -0,0 +1,5 @@ +package main + +import ( + "fmt" // https://pkg.go.dev/fmt +) diff --git a/pkg/gci/testdata/comment-with-slashslash.out.go b/pkg/gci/testdata/comment-with-slashslash.out.go new file mode 100644 index 0000000..85cddcb --- /dev/null +++ b/pkg/gci/testdata/comment-with-slashslash.out.go @@ -0,0 +1,6 @@ +package main + +import ( + // https://pkg.go.dev/fmt + "fmt" +) From 6dae7fe9364f2c935b382159863c2ae14df31225 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Sep 2021 02:01:13 -0600 Subject: [PATCH 8/8] fix: don't interfere with others' notions of whitespace For instance, it's style with nolintlint to not have a space between the "//" and the "nolint:", yet gci puts a space there. Let other linters worry about whitespace, gci is for import order. Signed-off-by: Luke Shumaker --- pkg/gci/gci.go | 4 ++-- pkg/gci/testdata/nolint.in.go | 9 +++++++++ pkg/gci/testdata/nolint.out.go | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 pkg/gci/testdata/nolint.in.go create mode 100644 pkg/gci/testdata/nolint.out.go diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index fa6219a..9285139 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -147,9 +147,9 @@ func getPkgInfo(line string, comment bool) (string, string, string) { s := strings.SplitN(line, commentFlag, 2) pkgArray := strings.Fields(s[0]) if len(pkgArray) > 1 { - return pkgArray[1], pkgArray[0], fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) + return pkgArray[1], pkgArray[0], commentFlag + s[1] } else { - return pkgArray[0], "", fmt.Sprintf("%s%s%s", commentFlag, blank, strings.TrimSpace(s[1])) + return pkgArray[0], "", commentFlag + s[1] } } else { pkgArray := strings.Fields(line) diff --git a/pkg/gci/testdata/nolint.in.go b/pkg/gci/testdata/nolint.in.go new file mode 100644 index 0000000..96c3dec --- /dev/null +++ b/pkg/gci/testdata/nolint.in.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" + + "github.com/forbidden/pkg" //nolint:depguard + + _ "github.com/daixiang0/gci" //nolint:depguard +) diff --git a/pkg/gci/testdata/nolint.out.go b/pkg/gci/testdata/nolint.out.go new file mode 100644 index 0000000..a839e86 --- /dev/null +++ b/pkg/gci/testdata/nolint.out.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + //nolint:depguard + "github.com/forbidden/pkg" + + //nolint:depguard + _ "github.com/daixiang0/gci" +)