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

Automatic import management with dst #1

Merged
merged 1 commit into from
Dec 18, 2018
Merged

Automatic import management with dst #1

merged 1 commit into from
Dec 18, 2018

Conversation

dave
Copy link
Contributor

@dave dave commented Dec 16, 2018

Hey @cixel - I saw you were using dst... Thought I'd show you how to use the automatic import handling... Let me know what you think!

@cixel
Copy link
Owner

cixel commented Dec 18, 2018

Hey, thanks for the suggestions. This is great! This is my first time dealing with AST rewriting where I couldn't just throw away unimportant comments, and I found dst from your post. It's really useful.

So, a lot of this was borrowed from other projects where I'm doing all of this with go/ast and astutil. astutil has an AddImport function which did the work for me. Initially, I just search/replaced ast --> dst. Since dstutil.AddImport doesn't exist, my instinct was just to create a drop-in replacement for what I needed myself. I had thoughts about what happens if the name newrelic is taken but decided "meh, that's a dumb name and their fault". 😛

@cixel
Copy link
Owner

cixel commented Dec 18, 2018

FYI: I'm doing this for a meetup talk on some of the uses of AST rewriting and the transformations I wanted to focus on are injecting the init function and wrapping http.HandlerFunc (still TODO). It's unfortunate that preserving comments is by far the most involved part of the process---it kinda obscures the plot a bit.

That said, it's pretty unavoidable when talking about AST manipulation. Even if you can throw away 'lossy' comments, you still need to do a bit to re-attach comments with //go:* directives, so I don't think there's any avoiding it in my talk.

I think what I'll do is show the manipulations using ast and astutil. Then, I'll talk about the comments problem and a bit on how you're handling it with dst. Are there any key ideas you think I should present about your approach?

@cixel cixel merged commit d24c91b into cixel:master Dec 18, 2018
@cixel cixel requested review from cixel and removed request for cixel December 18, 2018 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants