Skip to content

Instantly share code, notes, and snippets.

@jbellsey
Created August 13, 2016 14:37
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 jbellsey/0fef4c161ec324fce1015d0f4f47fe21 to your computer and use it in GitHub Desktop.
Save jbellsey/0fef4c161ec324fce1015d0f4f47fe21 to your computer and use it in GitHub Desktop.
Strip propTypes from components created with React.creactClass()
{
"presets": [
"es2015",
"stage-1",
"react"
],
"plugins": [
"./path-to/babel-plugin-remove-proptypes.js"
]
}
function isCreateClassCall(path) {
try {
var parentPath = path.contexts[0].parentPath,
parent = parentPath.parent,
grandparent = parentPath.contexts[0].parentPath.parent,
callee = grandparent.callee,
args = grandparent.arguments,
isRender = prop => prop.key.name === 'render';
return parent.type === 'ObjectExpression' && // "propTypes" has to be a property of an object
parent.properties.length > 1 && // ... which has more than 1 sibling key
callee.type === 'MemberExpression' && // the object is an argument to an object.method call
callee.property.name === 'createClass' && // ... and whose method is "createClass"
args.length === 1 && // ... and the method call has exactly 1 argument
parent.properties.some(isRender); // and there is a "render" key on the object
}
catch(e) {
return false;
}
}
function removePropTypes() {
return {
visitor: {
Identifier(path) {
if (path.node.name === 'propTypes' && isCreateClassCall(path)) {
path.parentPath.remove();
}
}
}
};
}
module.exports = removePropTypes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment