-
Notifications
You must be signed in to change notification settings - Fork 18k
text/template/parse: Unexpected Behaviour of ActionNode.Position in text/template - Potential Bug? #66423
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
Comments
The action starts inside the delimiter, so the position field is correct. The String method includes the delimiter for ease of use. So yes, they are inconsistent and perhaps should be consistent, but it's more likely to break things than improve them to try to change this. Plus, you'd need to decide what change to make. I think it can stay as is. |
Thanks for replying, I understanded. May I ask why the position does not contains "{{ if" and other things, without these, it is nearly impossible to map nodes range to the original text for highlighter, and is not like other common ast parsers. |
As I said, the action starts inside the delimiter, that's why. |
Sorry I may not have made it clear, I mean if there should be a node wraps the action node but contains the delimiter and prefix such as "if" and "range" to make the AST nodes ranges cover all the original text. |
It's impractical to change the parse tree given the compatibility promise. You'll just have to code around it, which shouldn't be too inconvenient. |
Got it, thanks. |
Go version
go version go1.21.4 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/U8UabjbhJsN
What did you see happen?
I am currently developing a highlighter for
text/template
scripts. To accomplish this, I require an Abstract Syntax Tree (AST) for the provided template. During this process, I encountered an unexpected behaviour with thePosition
method of theActionNode
.Contrary to my expectations, the
Position
of theActionNode
does not include the delimiter ('{{'). However, theString
method does return the full{{.locale}}
, inclusive of the delimiter. This discrepancy has prompted me to question whether there is a bug, or if I have misunderstood the purpose of thePosition
function in theAction
interface.According to the given documentation comment,
Position() Pos
should represent the 'byte position of the start of node in full original input string'. This, in my understanding, should include the delimiter.Could you please clarify this discrepancy, or confirm if this is indeed a bug? Thank you for your time and assistance.
What did you expect to see?
The text was updated successfully, but these errors were encountered: