From 8cb83b71b42ccf5fe279fa8a24a6a8f65507dc9c Mon Sep 17 00:00:00 2001 From: LE Manh Cuong Date: Tue, 10 Jul 2018 02:50:39 +0700 Subject: [PATCH] imports: fix lost line between package and import statement Fixes golang/go#26290 Change-Id: Ide797a46bf1d0d6070940fb5b9db3e76502bc528 Reviewed-on: https://go-review.googlesource.com/122736 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- go/ast/astutil/imports.go | 7 ++++--- go/ast/astutil/imports_test.go | 2 ++ imports/fix_test.go | 36 ++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/go/ast/astutil/imports.go b/go/ast/astutil/imports.go index 83f196cd5e..04ad6795d9 100644 --- a/go/ast/astutil/imports.go +++ b/go/ast/astutil/imports.go @@ -101,8 +101,8 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added impDecl.TokPos = f.Decls[lastImport].End() } else { // There are no existing imports. - // Our new import goes after the package declaration and after - // the comment, if any, that starts on the same line as the + // Our new import, preceded by a blank line, goes after the package declaration + // and after the comment, if any, that starts on the same line as the // package declaration. impDecl.TokPos = f.Package @@ -112,7 +112,8 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added if file.Line(c.Pos()) > pkgLine { break } - impDecl.TokPos = c.End() + // +2 for a blank line + impDecl.TokPos = c.End() + 2 } } f.Decls = append(f.Decls, nil) diff --git a/go/ast/astutil/imports_test.go b/go/ast/astutil/imports_test.go index 8bc3480873..da775ef4c2 100644 --- a/go/ast/astutil/imports_test.go +++ b/go/ast/astutil/imports_test.go @@ -367,6 +367,7 @@ type T time.Time type T time.Time `, out: `package main // comment + import "time" type T time.Time @@ -399,6 +400,7 @@ type T time.Time `, out: `// comment before package main // comment on + import "time" type T time.Time diff --git a/imports/fix_test.go b/imports/fix_test.go index f9bac4610b..0d413e4304 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -1019,6 +1019,38 @@ func main() { _ errors.Frame ) } +`, + }, + + { + name: "issue #26290 1", + in: `package p // comment + +import "math" + +var _ = fmt.Printf +`, + out: `package p // comment + +import "fmt" + +var _ = fmt.Printf +`, + }, + + { + name: "issue #26290 2", + in: `package p + +import "math" + +var _ = fmt.Printf +`, + out: `package p + +import "fmt" + +var _ = fmt.Printf `, }, } @@ -1800,8 +1832,8 @@ func TestImportPathToNameGoPathParse(t *testing.T) { func TestIgnoreConfiguration(t *testing.T) { testConfig{ gopathFiles: map[string]string{ - ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming - "example.net/pkg/pkg.go": "package pkg\nconst X = 1", + ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming + "example.net/pkg/pkg.go": "package pkg\nconst X = 1", "otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1", }, }.test(t, func(t *goimportTest) {