Commit dfe03bb2 authored by Russ Cox's avatar Russ Cox

go/ast: fix bugs in SortImports

Tests are in gofix, since the bugs arise in rewritten ASTs.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5365043
parent 2547ad6b
...@@ -264,6 +264,90 @@ import ( ...@@ -264,6 +264,90 @@ import (
"io" "io"
"os" "os"
) )
`,
},
{
Name: "import.14",
Fn: rewriteImportFn("asn1", "encoding/asn1"),
In: `package main
import (
"asn1"
"crypto"
"crypto/rsa"
_ "crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"time"
)
var x = 1
`,
Out: `package main
import (
"crypto"
"crypto/rsa"
_ "crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"time"
)
var x = 1
`,
},
{
Name: "import.15",
Fn: rewriteImportFn("url", "net/url"),
In: `package main
import (
"bufio"
"net"
"path"
"url"
)
var x = 1 // comment on x, not on url
`,
Out: `package main
import (
"bufio"
"net"
"net/url"
"path"
)
var x = 1 // comment on x, not on url
`,
},
{
Name: "import.16",
Fn: rewriteImportFn("http", "net/http", "template", "text/template"),
In: `package main
import (
"flag"
"http"
"log"
"template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
`,
Out: `package main
import (
"flag"
"log"
"net/http"
"text/template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
`, `,
}, },
} }
...@@ -288,12 +372,15 @@ func deleteImportFn(path string) func(*ast.File) bool { ...@@ -288,12 +372,15 @@ func deleteImportFn(path string) func(*ast.File) bool {
} }
} }
func rewriteImportFn(old, new string) func(*ast.File) bool { func rewriteImportFn(oldnew ...string) func(*ast.File) bool {
return func(f *ast.File) bool { return func(f *ast.File) bool {
if imports(f, old) { fixed := false
rewriteImport(f, old, new) for i := 0; i < len(oldnew); i += 2 {
return true if imports(f, oldnew[i]) {
rewriteImport(f, oldnew[i], oldnew[i+1])
fixed = true
} }
return false }
return fixed
} }
} }
...@@ -67,7 +67,12 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) { ...@@ -67,7 +67,12 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) {
// Record positions for specs. // Record positions for specs.
pos := make([]posSpan, len(specs)) pos := make([]posSpan, len(specs))
for i, s := range specs { for i, s := range specs {
pos[i] = posSpan{s.Pos(), s.End()} // Cannot use s.End(), because it looks at len(s.Path.Value),
// and that string might have gotten longer or shorter.
// Instead, use s.Pos()+1, which is guaranteed to be > s.Pos()
// and still before the original end of the string, since any
// string literal must be at least 2 characters ("" or ``).
pos[i] = posSpan{s.Pos(), s.Pos() + 1}
} }
// Identify comments in this range. // Identify comments in this range.
...@@ -107,6 +112,9 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) { ...@@ -107,6 +112,9 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) {
sort.Sort(byImportPath(specs)) sort.Sort(byImportPath(specs))
for i, s := range specs { for i, s := range specs {
s := s.(*ImportSpec) s := s.(*ImportSpec)
if s.Name != nil {
s.Name.NamePos = pos[i].Start
}
s.Path.ValuePos = pos[i].Start s.Path.ValuePos = pos[i].Start
s.EndPos = pos[i].End s.EndPos = pos[i].End
for _, g := range importComment[s] { for _, g := range importComment[s] {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment