diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 7418db2..0fd7a0e 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -146,7 +146,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err // order by section list for _, s := range cfg.Sections { if len(result[s.String()]) > 0 { - if body != nil && len(body) > 0 { + if len(body) > 0 { body = append(body, utils.Linebreak) } for _, d := range result[s.String()] { @@ -161,7 +161,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err tail := make([]byte, len(src)-tailStart) copy(tail, src[tailStart:]) - head = append(head, utils.Linebreak) // ensure C if cStart != 0 { head = append(head, src[cStart:cEnd]...) diff --git a/pkg/gci/internal/testdata/comment-before-import.cfg.yaml b/pkg/gci/internal/testdata/comment-before-import.cfg.yaml new file mode 120000 index 0000000..b0db9e7 --- /dev/null +++ b/pkg/gci/internal/testdata/comment-before-import.cfg.yaml @@ -0,0 +1 @@ +common.cfg.yaml \ No newline at end of file diff --git a/pkg/gci/internal/testdata/comment-before-import.in.go b/pkg/gci/internal/testdata/comment-before-import.in.go new file mode 100644 index 0000000..e409ca1 --- /dev/null +++ b/pkg/gci/internal/testdata/comment-before-import.in.go @@ -0,0 +1,9 @@ +package main + +// comment +import ( + "fmt" + "os" + + "github.com/daixiang0/gci" +) diff --git a/pkg/gci/internal/testdata/comment-before-import.out.go b/pkg/gci/internal/testdata/comment-before-import.out.go new file mode 100644 index 0000000..e409ca1 --- /dev/null +++ b/pkg/gci/internal/testdata/comment-before-import.out.go @@ -0,0 +1,9 @@ +package main + +// comment +import ( + "fmt" + "os" + + "github.com/daixiang0/gci" +)