25 lines
647 B
JavaScript

import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
export default function(babel) {
const { types: t } = babel;
return {
inherits: syntaxOptionalCatchBinding,
visitor: {
CatchClause(path) {
if (path.node.param === null || !t.isIdentifier(path.node.param)) {
return;
}
const binding = path.scope.getOwnBinding(path.node.param.name);
if (binding.constantViolations.length > 0) {
return;
}
if (!binding.referenced) {
const paramPath = path.get("param");
paramPath.remove();
}
},
},
};
}