ventojs / vento

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

transform error with a combination of function and var declaration #63

Closed oscarotero closed 1 month ago

oscarotero commented 1 month ago

@wrapperup Hi! 👋

Due you have fixed other issues like this and are more familiarized with this part, do you mind to take a look to this one?

I have this template

{{ function echo(message) }}
    {{ message }}
{{ /function }}

{{ set foo = "It works!" }}

{{ echo(foo) }}

And it produces the following error:

error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'stack')
      this.scopes[this.globalScope].stack.push(val);
                                    ^
    at ScopeTracker.pushBinding (file:///Users/oscarotero/www/ventojs/vento/src/transformer.ts:67:37)
    at ScopeTracker.pushPatternBinding (file:///Users/oscarotero/www/ventojs/vento/src/transformer.ts:76:14)
    at ScopeTracker.pushPatternBindings (file:///Users/oscarotero/www/ventojs/vento/src/transformer.ts:109:12)
    at Object.enter (file:///Users/oscarotero/www/ventojs/vento/src/transformer.ts:174:19)
    at SyncWalker.visit (file:///Users/oscarotero/Library/Caches/deno/npm/registry.npmjs.org/estree-walker/3.0.3/src/sync.js:65:16)
    at SyncWalker.visit (file:///Users/oscarotero/Library/Caches/deno/npm/registry.npmjs.org/estree-walker/3.0.3/src/sync.js:100:19)
    at Module.walk (file:///Users/oscarotero/Library/Caches/deno/npm/registry.npmjs.org/estree-walker/3.0.3/src/index.js:20:18)
    at transformTemplateCode (file:///Users/oscarotero/www/ventojs/vento/src/transformer.ts:168:10)
    at file:///Users/oscarotero/www/ventojs/vento/test/transform.ts:5:1

The code that is trying to transform is this one:

__exports.content += "\n      ";
__pos = 7;
 function echo (message) {
let __output = "";
__output += "\n        ";
__pos = 44;
__output += (message) ?? "";
__output += "\n      ";
return __output;
}
__exports.content += "\n      ";
__pos = 86;

    it["foo"] = "It works!";
    var foo = "It works!";

__exports.content += "\n      ";
__pos = 120;
__exports.content += (echo(foo)) ?? "";
__exports.content += "\n      ";

Seems that the problem is the var foo = "It works!";. Removing this line, the code is transformed correctly.

I've added this test case reproducing the error: https://github.com/ventojs/vento/blob/main/test/with.test.ts#L30-L38

wrapperup commented 1 month ago

Thanks for finding it, I've diagnosed the issue and I'll have a PR up shortly.