]> Cypherpunks repositories - gostls13.git/commitdiff
go/ast: fix bugs in SortImports
authorRuss Cox <rsc@golang.org>
Mon, 7 Nov 2011 19:44:06 +0000 (14:44 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 7 Nov 2011 19:44:06 +0000 (14:44 -0500)
Tests are in gofix, since the bugs arise in rewritten ASTs.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5365043

src/cmd/gofix/import_test.go
src/pkg/go/ast/import.go

index 4a9259f40929b2acadd1dbc93f986f0c3a8ae7cb..a06dc821fb31fa3c5aa4346898a1fbbcf1a5a374 100644 (file)
@@ -264,6 +264,90 @@ import (
        "io"
        "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 {
        }
 }
 
-func rewriteImportFn(old, new string) func(*ast.File) bool {
+func rewriteImportFn(oldnew ...string) func(*ast.File) bool {
        return func(f *ast.File) bool {
-               if imports(f, old) {
-                       rewriteImport(f, old, new)
-                       return true
+               fixed := false
+               for i := 0; i < len(oldnew); i += 2 {
+                       if imports(f, oldnew[i]) {
+                               rewriteImport(f, oldnew[i], oldnew[i+1])
+                               fixed = true
+                       }
                }
-               return false
+               return fixed
        }
 }
index c64e9bbdc6bfaf6d6eae6772187c9c16b4fa2ed3..894fecdaa7e8dd4ffea9dee9e90666ee87ea3d8d 100644 (file)
@@ -67,7 +67,12 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) {
        // Record positions for specs.
        pos := make([]posSpan, len(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.
@@ -107,6 +112,9 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) {
        sort.Sort(byImportPath(specs))
        for i, s := range specs {
                s := s.(*ImportSpec)
+               if s.Name != nil {
+                       s.Name.NamePos = pos[i].Start
+               }
                s.Path.ValuePos = pos[i].Start
                s.EndPos = pos[i].End
                for _, g := range importComment[s] {