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

Use pointers to allow the AST to be modified. Implement visitor support. #1114

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

matthewmueller
Copy link

@matthewmueller matthewmueller commented Mar 29, 2025

This PR:

  • Adjusts the AST to use pointers, so it can be modified in place
  • Implements a Visitor interface that allows the AST to be traversed and rewritten

The goal of this PR is to allow the community to create compile-time plugins for Templ. Some examples could include:

We could also generalize a few patterns:

  • Generating Go code could be a visitor
  • Formatting with imports could be another visitor

This PR ended up a bit larger than I would have liked. While you could implement the Visitor pattern without making the AST pointers, it effectively makes it so the AST is read-only. Being able to modify the AST opens up a lot more possibilities.

Let me know if this is a direction you're interested in heading. Tests pass, but there's a few more tests I'd like to contribute before merging.

Also in the #templ channel in the Gophers Slack if you'd like to chat further first.

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.

1 participant