mirror of
https://github.com/golang/go
synced 2024-11-18 16:54:43 -07:00
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>
This commit is contained in:
parent
893c2b1ff5
commit
d600f31f81
@ -915,6 +915,110 @@ func main() {
|
||||
x := math.MaxInt64
|
||||
fmt.Println(strings.Join(",", []string{"hi"}), x)
|
||||
}
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
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
|
||||
)
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user