Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/ast: FilterFile filters out import declarations #9248

Closed
jamesgarfield opened this issue Dec 10, 2014 · 4 comments
Closed

go/ast: FilterFile filters out import declarations #9248

jamesgarfield opened this issue Dec 10, 2014 · 4 comments
Assignees

Comments

@jamesgarfield
Copy link

@jamesgarfield jamesgarfield commented Dec 10, 2014

It seems that ast.FilterFile does not take ast.ImportSpecs into account when applying the filter function, leading to them being filtered out of the ast by default.

Example: http://play.golang.org/p/VA3iQdNIJv

I suspect that add type case for ast.ImportSpec here:
http://golang.org/src/pkg/go/ast/filter.go?s=#L158

would potentially solve this.

This behavior is exhibited in both 1.3 and 1.4rc1

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 10, 2014

Why do you need the ImportSpecs? What is the use case?

Note that this function was primarily written for filtering the AST for use with go/doc and eventually displaying it via godoc. Changing the semantics may have unintended consequences.

@jamesgarfield
Copy link
Author

@jamesgarfield jamesgarfield commented Dec 10, 2014

I'm writing a code writing tool that uses AST analysis and transformation to produce specific implementations of generic algorithms on matching types. Part of this process is analyzing what imports from the 'type provider' file need to be added to the specific implementation of the generic code in order to be correct.

E.g.

Type Provider

package main
import "go/ast"
type Decls []*ast.GenDecl

Generic Algorithm

package main
type Collection []interface
func (c Collection) Filter(fn func(interface{}) bool) (result Collection) {}

Expected Result

package main
import "go/ast"
func (c Decls) Filter(fn func(*ast.GenDecl) bool) (result Decls) {}

It honestly shouldn't be hard to roll my own, but ast.FilterFile at least presents itself as the canonical implementation of what I'm trying to do. It's ok if it can't change, but maybe the documentation should be updated to indicate the limitation if that's the case.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 10, 2014

It may actually be easier to simply filter the imports yourself. I'd rather not change/add to this code at this point (the ast.FilterFile function is one of the very oldest pieces of library code and a new implementation would look rather different).

Let's leave the issue open so that we remember to document it eventually. Thanks.

@griesemer griesemer self-assigned this Dec 10, 2014
@jamesgarfield
Copy link
Author

@jamesgarfield jamesgarfield commented Dec 10, 2014

Sounds good, thanks!

@mikioh mikioh changed the title ast.FilterFile filters out import declarations go/ast: FilterFile filters out import declarations Jan 4, 2015
@griesemer griesemer closed this in 571d02d Jan 23, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.