go-toolsmith / astcopy

Package astcopy implements Go AST deep copy operations.
MIT License
19 stars 2 forks source link

astcopy: make it possible to copy ast.Object #1

Open quasilyte opened 6 years ago

quasilyte commented 6 years ago

The problem: astcopy functions do not copy ast.Object fields because some projects may not care about them at all (go/types.Object is better and deprecates ast.Object). But if for whatever reason user must copy objects, there will be troubles.

I'm proposing this functions:

func IdentObjects(dst, src *ast.Ident) {/**/}
func ScopeObjects(dst, src *ast.Scope) {/**/}
func PackageObjects(dst, src *ast.Package) {/**/}

So, copy of *ast.Ident with objects may be done this way:

y := astcopy.Ident(x)
astcopy.IdentObjects(y, x)

This can be wrapped by user into simple function:

func copyIdent(x) *ast.Ident {
  y := astcopy.Ident(x)
  astcopy.IdentObjects(y, x)
  return y
}

This way, we avoid quite expensive copy that may be unnecessary in a first place and provide a way to copy objects if they are required.

Note that copying objects may be non-trivial. It needs some investigation and problem overview beforehand.

cristaloleg commented 6 years ago

WDYT about astcopy.IdentDeep ?

quasilyte commented 6 years ago

@cristaloleg that could work too.

This is not high priority though. go-critic will never need that.

Although it's good that we have an idea how to add this feature if someone really needs it. We are prepared. :)