Skip to content

Commit

Permalink
Added Restorer.Extras flag and disabled resorting Scopes and Objects …
Browse files Browse the repository at this point in the history
…by default.
  • Loading branch information
dave committed Dec 6, 2018
1 parent 9d1cc8e commit 374a06a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ if err != nil {
panic(err)
}
p := pkgs[0]
f := p.Files[0]
f := p.Syntax[0]

// Add a call expression. Note we don't have to use a SelectorExpr - just adding an Ident with
// the imported package path will do. The restorer will add SelectorExpr where appropriate when
Expand All @@ -324,7 +324,7 @@ b.List = append(b.List, &dst.ExprStmt{
// Create a restorer with the import manager enabled, and print the result. As you can see, the
// import block is automatically managed, and the Println ident is converted to a SelectorExpr:
r := decorator.NewRestorerWithImports("root", gopackages.New(dir))
if err := r.Print(p.Files[0]); err != nil {
if err := r.Print(p.Syntax[0]); err != nil {
panic(err)
}

Expand Down
7 changes: 2 additions & 5 deletions clone.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package dst

// TODO: What should we be doing here?
// After cloning a node, should it still be attached to the same object / scope? Or a cloned copy?
// Should we really have objects / scopes at all? As soon as you modify the tree, they are
// potentially invalid.

// CloneObject returns nil: After cloning a node, it should not be attached to the same object / scope.
func CloneObject(o *Object) *Object {
return nil
}

// CloneScope returns nil: After cloning a node, it should not be attached to the same object / scope.
func CloneScope(s *Scope) *Scope {
return nil
}
26 changes: 17 additions & 9 deletions decorator/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type FileRestorer struct {
*Restorer
Alias map[string]string // Map of package path -> package alias for imports
Name string // The name of the restored file in the FileSet. Can usually be left empty.
Extras bool // Resore Objects, Scopes etc. Not needed for printing the resultant AST. If set to true, Objects and Scopes must be carefully managed to avoid duplicate nodes.
file *dst.File
lines []int
comments []*ast.CommentGroup
Expand Down Expand Up @@ -144,15 +145,16 @@ func (r *FileRestorer) RestoreFile(file *dst.File) (*ast.File, error) {
panic("ff.SetLines failed")
}

// Sometimes new nodes are created here (e.g. in RangeStmt the "Object" is an AssignStmt which
// never occurs in the actual code). These shouldn't have position information but perhaps it
// doesn't matter?
// TODO: Disable all position information on these nodes?
for o, dn := range r.nodeDecl {
o.Decl = r.restoreNode(dn, "", "", "", true)
}
for o, dn := range r.nodeData {
o.Data = r.restoreNode(dn, "", "", "", true)
if r.Extras {
// Sometimes new nodes are created here (e.g. in RangeStmt the "Object" is an AssignStmt
// which never occurs in the actual code). These shouldn't have position information but
// perhaps it doesn't matter?
for o, dn := range r.nodeDecl {
o.Decl = r.restoreNode(dn, "", "", "", true)
}
for o, dn := range r.nodeData {
o.Data = r.restoreNode(dn, "", "", "", true)
}
}

return f, nil
Expand Down Expand Up @@ -693,6 +695,9 @@ func (r *FileRestorer) applySpace(space dst.SpaceType) {
}

func (r *FileRestorer) restoreObject(o *dst.Object) *ast.Object {
if !r.Extras {
return nil
}
if o == nil {
return nil
}
Expand Down Expand Up @@ -755,6 +760,9 @@ func (r *FileRestorer) restoreObject(o *dst.Object) *ast.Object {
}

func (r *FileRestorer) restoreScope(s *dst.Scope) *ast.Scope {
if !r.Extras {
return nil
}
if s == nil {
return nil
}
Expand Down

0 comments on commit 374a06a

Please sign in to comment.