ventojs / vento

🌬 A template engine for Deno & Node
https://vento.js.org/
MIT License
209 stars 12 forks source link

Improve error messaging #85

Open uncenter opened 5 hours ago

uncenter commented 5 hours ago

From my experience so far with Vento, the experience has generally been great but errors seem to be a pain point. Because of how Vento translates to JavaScript and then feeds that to meriyah, I'm often seeing errors along the lines of [2:34-2:40]: Expected ')' (via SyntaxError) coming from meriyah that is caused by some other issue (i.e. https://github.com/noelforte/eleventy-plugin-vento/issues/69). Would love to see clearer messaging here.

oscarotero commented 3 hours ago

Yep, generating clear errors is probably the hardest work because the template code is not entirely checked by Vento. The javascript parts are not parsed but executed by the runtime and it's not easy to map the position of the error produced in the final javascript code with the original template code. To do this right, we should generate a source map, but this makes the template engine much more complex.

The way this is implementing is updating the __pos variable before every tag. For example, the following template:

Text 1
{{ 1 }}
Text 2
{{ 2 }}

Generates the following javascript code (modified for clarity):

__exports.content += "Text 1\n";
__pos = 7;
__exports.content += 1;
__exports.content += "\nText 2\n";
__pos = 22;
__exports.content += 2;

The __pos variable indicates the position of the next tag in the original template code, so Vento knows the point in the template where the error has originated by looking the value of the __pos variable, that was updated just before executing the tag. This number is used to show the code of the tag when the error is thrown.

But the code transformed by meriyah doesn't use the __pos variable, the position of the error is in the final JavaScript code. To mitigate this, I just implemented this logic:

Let me know if this works before releasing a new version.