aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--integration/snippets/multiple-var.js10
-rw-r--r--src/js_printer.zig14
2 files changed, 23 insertions, 1 deletions
diff --git a/integration/snippets/multiple-var.js b/integration/snippets/multiple-var.js
new file mode 100644
index 000000000..cd7a84109
--- /dev/null
+++ b/integration/snippets/multiple-var.js
@@ -0,0 +1,10 @@
+var foo = true;
+
+if (true) {
+ var { foo } = { foo: false };
+}
+
+export function test() {
+ console.assert(foo === false, "foo should be false");
+ return testDone(import.meta.url);
+}
diff --git a/src/js_printer.zig b/src/js_printer.zig
index 0300630dc..2da9703fb 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -2041,7 +2041,6 @@ pub fn NewPrinter(
p.printExpr(item.value.?, .comma, ExprFlag.None());
return;
}
- const _key = item.key orelse unreachable;
if (item.flags.is_static) {
p.print("static");
@@ -2082,8 +2081,21 @@ pub fn NewPrinter(
},
else => {},
}
+
+ // If var is declared in a parent scope and var is then written via destructuring pattern, key is null
+ // example:
+ // var foo = 1;
+ // if (true) {
+ // var { foo } = { foo: 2 };
+ // }
+ if (item.key == null) {
+ p.printExpr(val, .comma, ExprFlag.None());
+ return;
+ }
}
+ const _key = item.key.?;
+
if (item.flags.is_computed) {
p.print("[");
p.printExpr(_key, .comma, ExprFlag.None());