clean up the function that walks the syntax tree

Avoiding a type switch for the entire node prevents an indentation
level.

We can obtain obj and pkg early, and return early as well if either is
uninteresting. That means less nil checks later on, which means even
less indentation and complexity.
pull/22/head
Daniel Martí 5 years ago
parent f0a609c7fc
commit 3617013cd1

@ -454,8 +454,10 @@ func transformGo(file *ast.File, info *types.Info) *ast.File {
} }
pre := func(cursor *astutil.Cursor) bool { pre := func(cursor *astutil.Cursor) bool {
switch node := cursor.Node().(type) { node, ok := cursor.Node().(*ast.Ident)
case *ast.Ident: if !ok {
return true
}
if node.Name == "_" { if node.Name == "_" {
return true // unnamed remains unnamed return true // unnamed remains unnamed
} }
@ -463,11 +465,24 @@ func transformGo(file *ast.File, info *types.Info) *ast.File {
return true // don't mess with cgo-generated code return true // don't mess with cgo-generated code
} }
obj := info.ObjectOf(node) obj := info.ObjectOf(node)
if obj == nil {
switch cursor.Parent().(type) {
case *ast.AssignStmt:
// symbolic var v in v := expr.(type)
node.Name = hashWith(buildInfo.buildID, node.Name)
}
return true
}
pkg := obj.Pkg()
if pkg == nil {
return true // universe scope
}
// log.Printf("%#v %T", node, obj) // log.Printf("%#v %T", node, obj)
switch x := obj.(type) { switch x := obj.(type) {
case *types.Var: case *types.Var:
if x.Embedded() { if x.Embedded() {
obj = objOf(obj.Type()) obj = objOf(obj.Type())
pkg = obj.Pkg()
} else if x.IsField() && x.Exported() { } else if x.IsField() && x.Exported() {
// might be used for reflection, e.g. // might be used for reflection, e.g.
// encoding/json without struct tags // encoding/json without struct tags
@ -490,22 +505,10 @@ func transformGo(file *ast.File, info *types.Info) *ast.File {
if strings.HasPrefix(node.Name, "Test") && isTestSignature(sign) { if strings.HasPrefix(node.Name, "Test") && isTestSignature(sign) {
return true // don't break tests return true // don't break tests
} }
case nil:
switch cursor.Parent().(type) {
case *ast.AssignStmt:
// symbolic var v in v := expr.(type)
default:
return true
}
default: default:
return true // we only want to rename the above return true // we only want to rename the above
} }
buildID := buildInfo.buildID buildID := buildInfo.buildID
if obj != nil {
pkg := obj.Pkg()
if pkg == nil {
return true // universe scope
}
path := pkg.Path() path := pkg.Path()
if !isPrivate(path) { if !isPrivate(path) {
return true // only private packages are transformed return true // only private packages are transformed
@ -522,11 +525,9 @@ func transformGo(file *ast.File, info *types.Info) *ast.File {
} }
buildID = id buildID = id
} }
}
// orig := node.Name // orig := node.Name
node.Name = hashWith(buildID, node.Name) node.Name = hashWith(buildID, node.Name)
// log.Printf("%q hashed with %q to %q", orig, buildID, node.Name) // log.Printf("%q hashed with %q to %q", orig, buildID, node.Name)
}
return true return true
} }
return astutil.Apply(file, pre, nil).(*ast.File) return astutil.Apply(file, pre, nil).(*ast.File)

Loading…
Cancel
Save