1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:34:48 -07:00

update tests and add comments

This commit is contained in:
Mateusz Poliwczak 2024-10-02 11:05:17 +02:00
parent b04affa7e6
commit af9eb3ec03
2 changed files with 13 additions and 7 deletions

View File

@ -207,6 +207,8 @@ func sortSpecs(fset *token.FileSet, f *File, d *GenDecl, specs []Spec) []Spec {
deduped = append(deduped, s) deduped = append(deduped, s)
} else { } else {
p := s.Pos() p := s.Pos()
// This function is exited early when len(specs) <= 1,
// so d.Rparen must be populated, d.Rparen.IsValid() return true.
if l := lineAt(fset, p); l != lineAt(fset, d.Rparen) { if l := lineAt(fset, p); l != lineAt(fset, d.Rparen) {
fset.File(p).MergeLine(l) fset.File(p).MergeLine(l)
} }

View File

@ -83,12 +83,12 @@ import (
"a"//a "a"//a
"a") "a")
` `
fs := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fs, "test.go", src, parser.ParseComments|parser.SkipObjectResolution) f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ast.SortImports(fs, f) // should not panic ast.SortImports(fset, f) // should not panic
} }
func TestSortImportsSameLastLine(t *testing.T) { func TestSortImportsSameLastLine(t *testing.T) {
@ -99,14 +99,18 @@ import (
func a() {} func a() {}
` `
fs := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fs, "test.go", src, parser.ParseComments|parser.SkipObjectResolution) f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ast.SortImports(fs, f) ast.SortImports(fset, f)
fd := f.Decls[1].(*ast.FuncDecl) fd := f.Decls[1].(*ast.FuncDecl)
fdPos := fs.Position(fd.Pos())
// After SortImports, the Position of the func, should still be at Column == 1.
// This is related to the issue: https://go.dev/issue/69183, we were merging lines
// incorectly, which caused the position to be Column = 6, Line = 4.
fdPos := fset.Position(fd.Pos())
if fdPos.Column != 1 { if fdPos.Column != 1 {
t.Errorf("invalid fdPos.Column = %v; want = 1", fdPos.Column) t.Errorf("invalid fdPos.Column = %v; want = 1", fdPos.Column)
} }