1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:54:57 -07:00

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
LE Manh Cuong 2018-07-10 02:50:39 +07:00 committed by Brad Fitzpatrick
parent d600f31f81
commit 8cb83b71b4
3 changed files with 40 additions and 5 deletions

View File

@ -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)

View File

@ -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

View File

@ -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) {