-
Notifications
You must be signed in to change notification settings - Fork 26
scriggo: add UnexpandedTransformer and rename TreeTransformer
#981
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
base: main
Are you sure you want to change the base?
Conversation
gazerro
commented
Jan 16, 2026
Introduce a new build option for templates that allows applying a transformation to the AST of each parsed file before expansion. This is particularly useful when changes need to be applied prior to expansion, such as rewriting paths. As part of this change, 'BuildOptions.TreeTransformer' has been renamed to ExpandedTransformer to better reflect when it is executed.
internal/compiler/parser_template.go
Outdated
| // Any error related to the compilation itself is returned as a CompilerError. | ||
| // | ||
| // If noParseShow is true, short show statements are not parsed. | ||
| // If a transformer is provided, invoke it with the tree before it is expanded. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite get the verb tense in this documentation. Shouldn't it be something like "it is invoked..."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in commit 931249c.
| var render *ast.Render | ||
| astutil.Inspect(tree, func(node ast.Node) bool { | ||
| if render == nil { | ||
| render, _ = node.(*ast.Render) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand this code. This particular line performs a type assertion but silently fails if the assertion fails. Is this intentional? Wouldn't it be better to fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is intentional and the code is correct. It relies on the value assigned to render, which is nil when the node is not a Render. In other words, the logic is effectively saying: "as long as render is nil, keep traversing".
Alternatively, it could be written more explicitly like this:
astutil.Inspect(tree, func(node ast.Node) bool {
if render != nil {
return false
}
var ok bool
render, ok = node.(*ast.Render)
return !ok
}However, I'm not convinced this version is actually clearer than the current implementation.
54db906 to
45533ac
Compare