Support var declaration hoisting

Var declarations were treated identical as other declarations which cause code 
relying on them getting hoisted now triggers runtime exception on TDZ. 

This diff fixed that by generating `var` for `var` so they can be hoisted as 
usual. 

commit-id:00ab02f6
This commit is contained in:
Xuan Huang (黄玄)
2022-10-05 13:23:37 -04:00
parent 896ef251b4
commit 83f3999a2e
+19 -7
View File
@@ -247,14 +247,26 @@ export class Func {
* let output1, output2;
*/
emitReactiveBlockOutputDecls(block: LIR.ReactiveBlock) {
if (block.outputDecls.size > 0) {
const vars: t.VariableDeclarator[] = [];
const lets: t.VariableDeclarator[] = [];
for (const decl of block.outputDecls) {
if (decl.binding.kind === "var") {
vars.push(t.variableDeclarator(decl.binding.identifier));
} else {
lets.push(t.variableDeclarator(decl.binding.identifier));
}
}
if (vars.length > 0) {
this.code.push(
t.variableDeclaration(
/*kind*/ "let",
/*declarators*/ [...block.outputDecls.values()].map((output) =>
t.variableDeclarator(/*id*/ output.binding.identifier)
)
)
t.variableDeclaration(/*kind*/ "var", /*declarators*/ vars)
);
}
if (lets.length > 0) {
this.code.push(
t.variableDeclaration(/*kind*/ "let", /*declarators*/ lets)
);
}
}