From 8e3536ea84384d6e5a85fea51e16a8716bfffe43 Mon Sep 17 00:00:00 2001 From: "Alexis H. Munsayac" Date: Fri, 19 Jun 2026 09:22:42 +0800 Subject: [PATCH] fix #2160 --- .../src/directives/remove-unused-variables.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/start/src/directives/remove-unused-variables.ts b/packages/start/src/directives/remove-unused-variables.ts index 6155bfea7..bc7bfdcc0 100644 --- a/packages/start/src/directives/remove-unused-variables.ts +++ b/packages/start/src/directives/remove-unused-variables.ts @@ -2,6 +2,20 @@ import type * as babel from "@babel/core"; import * as t from "@babel/types"; import { isPathValid } from "./paths.ts"; +function isInvalidForRemoval(path: babel.NodePath) { + if (isPathValid(path, t.isCatchClause)) { + // This case is for `catch (error)` blocks + return true; + } + + // This one is for destructured variables + let target = path; + if (isPathValid(path, t.isVariableDeclarator)) { + target = path.get('id'); + } + return isPathValid(target, t.isObjectPattern) || isPathValid(target, t.isArrayPattern); +} + export function removeUnusedVariables(program: babel.NodePath) { // TODO(Alexis): // This implementation is simple but slow @@ -24,17 +38,18 @@ export function removeUnusedVariables(program: babel.NodePath) { case "hoisted": case "module": if (binding.references === 0 && !binding.path.removed) { - if (isPathValid(binding.path.parentPath, t.isImportDeclaration)) { const parent = binding.path.parentPath; + if (isPathValid(parent, t.isImportDeclaration)) { if (parent.node.specifiers.length === 1) { parent.remove(); } else { binding.path.remove(); } - } else { + dirty = true; + } else if (!(isInvalidForRemoval(binding.path))) { binding.path.remove(); + dirty = true; } - dirty = true; } break; case "local":