Skip to content

Commit 5c62e5f

Browse files
committed
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 <[email protected]>
1 parent 29ffa4b commit 5c62e5f

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

src/cmd/fix/gotypes.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"go/ast"
9+
"strconv"
10+
)
11+
12+
func init() {
13+
register(gotypesFix)
14+
}
15+
16+
var gotypesFix = fix{
17+
"gotypes",
18+
"2015-07-16",
19+
gotypes,
20+
`Change imports of golang.org/x/tools/go/{exact,types} to go/{constant,types}`,
21+
}
22+
23+
func gotypes(f *ast.File) bool {
24+
truth := fixGoTypes(f)
25+
if fixGoExact(f) {
26+
truth = true
27+
}
28+
return truth
29+
}
30+
31+
func fixGoTypes(f *ast.File) bool {
32+
return rewriteImport(f, "golang.org/x/tools/go/types", "go/types")
33+
}
34+
35+
func fixGoExact(f *ast.File) bool {
36+
// This one is harder because the import name changes.
37+
// First find the import spec.
38+
var importSpec *ast.ImportSpec
39+
walk(f, func(n interface{}) {
40+
if importSpec != nil {
41+
return
42+
}
43+
spec, ok := n.(*ast.ImportSpec)
44+
if !ok {
45+
return
46+
}
47+
path, err := strconv.Unquote(spec.Path.Value)
48+
if err != nil {
49+
return
50+
}
51+
if path == "golang.org/x/tools/go/exact" {
52+
importSpec = spec
53+
}
54+
55+
})
56+
if importSpec == nil {
57+
return false
58+
}
59+
60+
// We are about to rename exact.* to constant.*, but constant is a common
61+
// name. See if it will conflict. This is a hack but it is effective.
62+
exists := renameTop(f, "constant", "constant")
63+
suffix := ""
64+
if exists {
65+
suffix = "_"
66+
}
67+
// Now we need to rename all the uses of the import. RewriteImport
68+
// affects renameTop, but not vice versa, so do them in this order.
69+
renameTop(f, "exact", "constant"+suffix)
70+
rewriteImport(f, "golang.org/x/tools/go/exact", "go/constant")
71+
// renameTop will also rewrite the imported package name. Fix that;
72+
// we know it should be missing.
73+
importSpec.Name = nil
74+
return true
75+
}

src/cmd/fix/gotypes_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
func init() {
8+
addTestCases(gotypesTests, gotypes)
9+
}
10+
11+
var gotypesTests = []testCase{
12+
{
13+
Name: "gotypes.0",
14+
In: `package main
15+
16+
import "golang.org/x/tools/go/types"
17+
import "golang.org/x/tools/go/exact"
18+
19+
var _ = exact.Kind
20+
21+
func f() {
22+
_ = exact.MakeBool(true)
23+
}
24+
`,
25+
Out: `package main
26+
27+
import "go/types"
28+
import "go/constant"
29+
30+
var _ = constant.Kind
31+
32+
func f() {
33+
_ = constant.MakeBool(true)
34+
}
35+
`,
36+
},
37+
{
38+
Name: "gotypes.1",
39+
In: `package main
40+
41+
import "golang.org/x/tools/go/types"
42+
import foo "golang.org/x/tools/go/exact"
43+
44+
var _ = foo.Kind
45+
46+
func f() {
47+
_ = foo.MakeBool(true)
48+
}
49+
`,
50+
Out: `package main
51+
52+
import "go/types"
53+
import "go/constant"
54+
55+
var _ = foo.Kind
56+
57+
func f() {
58+
_ = foo.MakeBool(true)
59+
}
60+
`,
61+
},
62+
{
63+
Name: "gotypes.0",
64+
In: `package main
65+
66+
import "golang.org/x/tools/go/types"
67+
import "golang.org/x/tools/go/exact"
68+
69+
var _ = exact.Kind
70+
var constant = 23 // Use of new package name.
71+
72+
func f() {
73+
_ = exact.MakeBool(true)
74+
}
75+
`,
76+
Out: `package main
77+
78+
import "go/types"
79+
import "go/constant"
80+
81+
var _ = constant_.Kind
82+
var constant = 23 // Use of new package name.
83+
84+
func f() {
85+
_ = constant_.MakeBool(true)
86+
}
87+
`,
88+
},
89+
}

0 commit comments

Comments
 (0)