mirror of
https://github.com/golang/go
synced 2024-11-18 21:44:45 -07:00
go/ast/astutil: fix DeleteImport SEGV when Rparen is invalid
Updates #36383 Change-Id: I04b33810c16f4fb7871f5a6a8207b1c159cbc65f GitHub-Last-Rev: 791fb862709c9a210bedde524555c5a6a92d9c51 GitHub-Pull-Request: golang/tools#196 Reviewed-on: https://go-review.googlesource.com/c/tools/+/214340 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
39095c1d17
commit
43d5027782
@ -275,9 +275,10 @@ func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (del
|
||||
|
||||
// We deleted an entry but now there may be
|
||||
// a blank line-sized hole where the import was.
|
||||
if line-lastLine > 1 {
|
||||
if line-lastLine > 1 || !gen.Rparen.IsValid() {
|
||||
// There was a blank line immediately preceding the deleted import,
|
||||
// so there's no need to close the hole.
|
||||
// so there's no need to close the hole. The right parenthesis is
|
||||
// invalid after AddImport to an import statement without parenthesis.
|
||||
// Do nothing.
|
||||
} else if line != fset.File(gen.Rparen).LineCount() {
|
||||
// There was no blank line. Close the hole.
|
||||
|
@ -1684,6 +1684,19 @@ func TestDeleteImport(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteImportAfterAddImport(t *testing.T) {
|
||||
file := parse(t, "test", `package main
|
||||
|
||||
import "os"
|
||||
`)
|
||||
if got, want := AddImport(fset, file, "fmt"), true; got != want {
|
||||
t.Errorf("AddImport: got: %v, want: %v", got, want)
|
||||
}
|
||||
if got, want := DeleteImport(fset, file, "fmt"), true; got != want {
|
||||
t.Errorf("DeleteImport: got: %v, want: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
type rewriteTest struct {
|
||||
name string
|
||||
srcPkg string
|
||||
|
Loading…
Reference in New Issue
Block a user