diff options
author | LE Manh Cuong <cuong.manhle.vn@gmail.com> | 2018-07-09 09:40:43 +0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-07-09 17:31:08 +0000 |
commit | d600f31f815368e57b551e524b0169bd175f7edb (patch) | |
tree | f4cea81a00acad9734ec60b29130ec0d0a7619fd /imports | |
parent | 893c2b1ff5959a5b546f24ed09627c6d72e3255a (diff) | |
download | golang-x-tools-d600f31f815368e57b551e524b0169bd175f7edb.tar.gz |
imports: fix unexpected blank line after package comment
The fix in golang/go#23709 introduced a separate bug where extra blank
lines were sometimes inserted. This fixes that newly introduced bug.
Fixes golang/go#26246
Change-Id: I78131cc1d01ae246922ed9e4336ebb31d1c6cfa1
Reviewed-on: https://go-review.googlesource.com/122538
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'imports')
-rw-r--r-- | imports/fix_test.go | 104 | ||||
-rw-r--r-- | imports/sortimports.go | 11 |
2 files changed, 115 insertions, 0 deletions
diff --git a/imports/fix_test.go b/imports/fix_test.go index 68996ed2e..f9bac4610 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -917,6 +917,110 @@ func main() { } `, }, + + { + name: "issue #26246 1", + in: `package main + +import ( + _ "io" + _ "net/http" + _ "net/http/pprof" // install the pprof http handlers + _ "strings" +) + +func main() { +} +`, + out: `package main + +import ( + _ "io" + _ "net/http" + _ "net/http/pprof" // install the pprof http handlers + _ "strings" +) + +func main() { +} +`, + }, + + { + name: "issue #26246 2", + in: `package main + +import ( + _ "io" + _ "net/http/pprof" // install the pprof http handlers + _ "net/http" + _ "strings" +) + +func main() { +} +`, + out: `package main + +import ( + _ "io" + _ "net/http" + _ "net/http/pprof" // install the pprof http handlers + _ "strings" +) + +func main() { +} +`, + }, + + { + name: "issue #26246 3", + in: `package main + +import ( + "encoding/json" + "io" + "net/http" + _ "net/http/pprof" // install the pprof http handlers + "strings" + + "github.com/pkg/errors" +) + +func main() { + _ = strings.ToUpper("hello") + _ = io.EOF + var ( + _ json.Number + _ *http.Request + _ errors.Frame + ) +} +`, + out: `package main + +import ( + "encoding/json" + "io" + "net/http" + _ "net/http/pprof" // install the pprof http handlers + "strings" + + "github.com/pkg/errors" +) + +func main() { + _ = strings.ToUpper("hello") + _ = io.EOF + var ( + _ json.Number + _ *http.Request + _ errors.Frame + ) +} +`, + }, } func TestFixImports(t *testing.T) { diff --git a/imports/sortimports.go b/imports/sortimports.go index 93711565a..f3dd56c7a 100644 --- a/imports/sortimports.go +++ b/imports/sortimports.go @@ -183,6 +183,17 @@ func sortSpecs(fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec { sort.Sort(byCommentPos(comments)) + // Fixup comments can insert blank lines, because import specs are on different lines. + // We remove those blank lines here by merging import spec to the first import spec line. + firstSpecLine := fset.Position(specs[0].Pos()).Line + for _, s := range specs[1:] { + p := s.Pos() + line := fset.File(p).Line(p) + for previousLine := line - 1; previousLine >= firstSpecLine; { + fset.File(p).MergeLine(previousLine) + previousLine-- + } + } return specs } |