Skip to content
Open
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
21 changes: 18 additions & 3 deletions packages/start/src/directives/remove-unused-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<t.Program>) {
// TODO(Alexis):
// This implementation is simple but slow
Expand All @@ -24,17 +38,18 @@ export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
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":
Expand Down
Loading