Created
February 22, 2020 01:00
-
-
Save bjyoungblood/d4009cf37ee8999f764811cac95e9722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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