davidbonnet / astring

🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.

Home Page:https://david.bonnet.cc/astring/demo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Individual TemplateElements are not supported

magiblot opened this issue · comments

Hello,

The structure of TemplateLiteral is as follows:

imatge

The elements of quasis are TemplateElement nodes. When parsing TemplateLiteral nodes, astring accesses the quasis' content directly:

state.write(quasis[i].value.raw)

This is OK unless you ask astring to parse the TemplateElement node individually. In that case, astring will crash as it doesn't have a specific method for this node type.

I wonder if this was an oversight or if it is intentional. Should I just write my own generator for TemplateElement?

Thank you very much.

Thanks for reporting this. You're right, Astring should render any Estree node. Failing to do so should be considered a bug. Feel free to submit a PR.

It doesn't seem too hard, though I don't know what to do with the EXPRESSIONS_PRECEDENCE values. What should the value for TemplateElement be?

No EXPRESSIONS_PRECEDENCE required. All there is to do is to add an extra handler called TemplateElement that does state.write(quasis[i].value.raw).