aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2015-07-16 13:47:01 +1000
committerRob Pike <r@golang.org>2015-07-17 01:47:14 +0000
commit5c62e5f9da1fa0b16fa8da918b8fd1eb807b38c1 (patch)
tree01f6dc52d70eb6449fa47620493e84f654610681
parent29ffa4bd921125b73b04078fd293ecc4bdb9d7cd (diff)
downloadgo-5c62e5f9da1fa0b16fa8da918b8fd1eb807b38c1.tar.gz
go-5c62e5f9da1fa0b16fa8da918b8fd1eb807b38c1.zip
cmd/fix: add gotypes module
Adjusts for the move from golang.org/x/tools/go/types and .../go/exact to go/types and go/constant in the main repository. Change-Id: I0da7248c540939e3e9b09c915b0a296937f1be73 Reviewed-on: https://go-review.googlesource.com/12284 Reviewed-by: Andrew Gerrand <adg@golang.org>
-rw-r--r--src/cmd/fix/gotypes.go75
-rw-r--r--src/cmd/fix/gotypes_test.go89
2 files changed, 164 insertions, 0 deletions
diff --git a/src/cmd/fix/gotypes.go b/src/cmd/fix/gotypes.go
new file mode 100644
index 0000000000..8c7b46674a
--- /dev/null
+++ b/src/cmd/fix/gotypes.go
@@ -0,0 +1,75 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "go/ast"
+ "strconv"
+)
+
+func init() {
+ register(gotypesFix)
+}
+
+var gotypesFix = fix{
+ "gotypes",
+ "2015-07-16",
+ gotypes,
+ `Change imports of golang.org/x/tools/go/{exact,types} to go/{constant,types}`,
+}
+
+func gotypes(f *ast.File) bool {
+ truth := fixGoTypes(f)
+ if fixGoExact(f) {
+ truth = true
+ }
+ return truth
+}
+
+func fixGoTypes(f *ast.File) bool {
+ return rewriteImport(f, "golang.org/x/tools/go/types", "go/types")
+}
+
+func fixGoExact(f *ast.File) bool {
+ // This one is harder because the import name changes.
+ // First find the import spec.
+ var importSpec *ast.ImportSpec
+ walk(f, func(n interface{}) {
+ if importSpec != nil {
+ return
+ }
+ spec, ok := n.(*ast.ImportSpec)
+ if !ok {
+ return
+ }
+ path, err := strconv.Unquote(spec.Path.Value)
+ if err != nil {
+ return
+ }
+ if path == "golang.org/x/tools/go/exact" {
+ importSpec = spec
+ }
+
+ })
+ if importSpec == nil {
+ return false
+ }
+
+ // We are about to rename exact.* to constant.*, but constant is a common
+ // name. See if it will conflict. This is a hack but it is effective.
+ exists := renameTop(f, "constant", "constant")
+ suffix := ""
+ if exists {
+ suffix = "_"
+ }
+ // Now we need to rename all the uses of the import. RewriteImport
+ // affects renameTop, but not vice versa, so do them in this order.
+ renameTop(f, "exact", "constant"+suffix)
+ rewriteImport(f, "golang.org/x/tools/go/exact", "go/constant")
+ // renameTop will also rewrite the imported package name. Fix that;
+ // we know it should be missing.
+ importSpec.Name = nil
+ return true
+}
diff --git a/src/cmd/fix/gotypes_test.go b/src/cmd/fix/gotypes_test.go
new file mode 100644
index 0000000000..1ecb7a2222
--- /dev/null
+++ b/src/cmd/fix/gotypes_test.go
@@ -0,0 +1,89 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func init() {
+ addTestCases(gotypesTests, gotypes)
+}
+
+var gotypesTests = []testCase{
+ {
+ Name: "gotypes.0",
+ In: `package main
+
+import "golang.org/x/tools/go/types"
+import "golang.org/x/tools/go/exact"
+
+var _ = exact.Kind
+
+func f() {
+ _ = exact.MakeBool(true)
+}
+`,
+ Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = constant.Kind
+
+func f() {
+ _ = constant.MakeBool(true)
+}
+`,
+ },
+ {
+ Name: "gotypes.1",
+ In: `package main
+
+import "golang.org/x/tools/go/types"
+import foo "golang.org/x/tools/go/exact"
+
+var _ = foo.Kind
+
+func f() {
+ _ = foo.MakeBool(true)
+}
+`,
+ Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = foo.Kind
+
+func f() {
+ _ = foo.MakeBool(true)
+}
+`,
+ },
+ {
+ Name: "gotypes.0",
+ In: `package main
+
+import "golang.org/x/tools/go/types"
+import "golang.org/x/tools/go/exact"
+
+var _ = exact.Kind
+var constant = 23 // Use of new package name.
+
+func f() {
+ _ = exact.MakeBool(true)
+}
+`,
+ Out: `package main
+
+import "go/types"
+import "go/constant"
+
+var _ = constant_.Kind
+var constant = 23 // Use of new package name.
+
+func f() {
+ _ = constant_.MakeBool(true)
+}
+`,
+ },
+}