Skip to content

Instantly share code, notes, and snippets.

@bjyoungblood
Created February 22, 2020 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjyoungblood/d4009cf37ee8999f764811cac95e9722 to your computer and use it in GitHub Desktop.
Save bjyoungblood/d4009cf37ee8999f764811cac95e9722 to your computer and use it in GitHub Desktop.
module.exports = function(babel) {
const { types: t } = babel;
return {
visitor: {
ClassDeclaration(path, parent) {
if (path.isClassDeclaration()) {
const className = path.node.id.name;
if (!path.node.superClass || path.node.superClass.name !== 'Error') {
return;
}
path.insertAfter(
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(
t.memberExpression(
t.identifier(className),
t.identifier('prototype'),
),
t.identifier('name'),
),
t.stringLiteral(className),
),
),
);
path.insertAfter(
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.identifier('Object'),
t.identifier('defineProperty'),
),
[
t.memberExpression(
t.identifier(className),
t.identifier('prototype'),
),
t.stringLiteral('name'),
t.objectExpression([
t.objectProperty(
t.identifier('enumerable'),
t.booleanLiteral(false),
),
]),
],
),
),
);
}
},
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment