Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ pub fn get_hoisted_declarations<'ast>(
}
Statement::ForIn(ForInStmt { left, body, .. })
| Statement::ForOf(ForOfStmt { left, body, .. }) => {
if let ForHead::VarDecl(var_decl) = left {
if let ForHead::VarDecl(var_decl) = left
&& matches!(var_decl.kind, VarDeclKind::Var)
{
for decl in &var_decl.decls {
collect_declaration_from_pat(&decl.name, &mut declarations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ it("Shouldn't eliminate hoisted function in sloppy mode", () => {
expect(true).toBe(false);
}
});

it("Shouldn't hoist const/let from dead for-of/for-in branch", () => {
// Bug: const/let in for-of inside if(false) was incorrectly hoisted as var,
// causing a duplicate declaration with any outer let/const of the same name.
let output = "start";
if (false) {
for (const output of [1, 2, 3]) {
console.log(output);
}
}
expect(output).toBe("start");
});

it("Shouldn't hoist const/let from dead for-in branch", () => {
let key = "start";
if (false) {
for (const key in { a: 1 }) {
console.log(key);
}
}
expect(key).toBe("start");
});

if (true) {
return;
}
Expand Down
Loading