From 1887cca1cae2650b1e39ee1c1c6e6238005adfa6 Mon Sep 17 00:00:00 2001 From: Loong Dai Date: Wed, 16 Nov 2022 16:35:22 +0800 Subject: [PATCH] fix first comment after import block missing (#124) * fix first comment missing Signed-off-by: Loong * bump up Signed-off-by: Loong --- pkg/gci/internal/testdata/multiple-imports.in.go | 1 + pkg/gci/internal/testdata/multiple-imports.out.go | 1 + pkg/parse/parse.go | 12 +++--------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/gci/internal/testdata/multiple-imports.in.go b/pkg/gci/internal/testdata/multiple-imports.in.go index d0143ca..930ab12 100644 --- a/pkg/gci/internal/testdata/multiple-imports.in.go +++ b/pkg/gci/internal/testdata/multiple-imports.in.go @@ -13,5 +13,6 @@ import ( import "math" +// main func main() { } diff --git a/pkg/gci/internal/testdata/multiple-imports.out.go b/pkg/gci/internal/testdata/multiple-imports.out.go index da6a5c9..f136a04 100644 --- a/pkg/gci/internal/testdata/multiple-imports.out.go +++ b/pkg/gci/internal/testdata/multiple-imports.out.go @@ -9,5 +9,6 @@ import ( "github.com/daixiang0/test" ) +// main func main() { } diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go index 7540d07..ffb9580 100644 --- a/pkg/parse/parse.go +++ b/pkg/parse/parse.go @@ -86,15 +86,14 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) { headEnd int // tailStart means the end + 1 of import block tailStart int - // lastImportStart means the start of last import block - lastImportStart int - data ImportList + data ImportList ) - for i, d := range f.Decls { + for _, d := range f.Decls { switch d.(type) { case *ast.GenDecl: dd := d.(*ast.GenDecl) + if dd.Tok == token.IMPORT { // there are two cases, both end with linebreak: // 1. @@ -107,15 +106,10 @@ func ParseFile(src []byte, filename string) (ImportList, int, int, error) { headEnd = int(d.Pos()) - 1 } tailStart = int(d.End()) - lastImportStart = i } } } - if len(f.Decls) > lastImportStart+1 { - tailStart = int(f.Decls[lastImportStart+1].Pos() - 1) - } - for _, imp := range f.Imports { if imp.Path.Value == C { if imp.Comment != nil {