mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00: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:
parent
d600f31f81
commit
8cb83b71b4
@ -101,8 +101,8 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added
|
|||||||
impDecl.TokPos = f.Decls[lastImport].End()
|
impDecl.TokPos = f.Decls[lastImport].End()
|
||||||
} else {
|
} else {
|
||||||
// There are no existing imports.
|
// There are no existing imports.
|
||||||
// Our new import goes after the package declaration and after
|
// Our new import, preceded by a blank line, goes after the package declaration
|
||||||
// the comment, if any, that starts on the same line as the
|
// and after the comment, if any, that starts on the same line as the
|
||||||
// package declaration.
|
// package declaration.
|
||||||
impDecl.TokPos = f.Package
|
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 {
|
if file.Line(c.Pos()) > pkgLine {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
impDecl.TokPos = c.End()
|
// +2 for a blank line
|
||||||
|
impDecl.TokPos = c.End() + 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.Decls = append(f.Decls, nil)
|
f.Decls = append(f.Decls, nil)
|
||||||
|
@ -367,6 +367,7 @@ type T time.Time
|
|||||||
type T time.Time
|
type T time.Time
|
||||||
`,
|
`,
|
||||||
out: `package main // comment
|
out: `package main // comment
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type T time.Time
|
type T time.Time
|
||||||
@ -399,6 +400,7 @@ type T time.Time
|
|||||||
`,
|
`,
|
||||||
out: `// comment before
|
out: `// comment before
|
||||||
package main // comment on
|
package main // comment on
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type T time.Time
|
type T time.Time
|
||||||
|
@ -1019,6 +1019,38 @@ func main() {
|
|||||||
_ errors.Frame
|
_ 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) {
|
func TestIgnoreConfiguration(t *testing.T) {
|
||||||
testConfig{
|
testConfig{
|
||||||
gopathFiles: map[string]string{
|
gopathFiles: map[string]string{
|
||||||
".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
|
".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
|
||||||
"example.net/pkg/pkg.go": "package pkg\nconst X = 1",
|
"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",
|
"otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1",
|
||||||
},
|
},
|
||||||
}.test(t, func(t *goimportTest) {
|
}.test(t, func(t *goimportTest) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user