Created
August 13, 2016 14:37
-
-
Save jbellsey/0fef4c161ec324fce1015d0f4f47fe21 to your computer and use it in GitHub Desktop.
Strip propTypes from components created with React.creactClass()
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
Show hidden characters
{ | |
"presets": [ | |
"es2015", | |
"stage-1", | |
"react" | |
], | |
"plugins": [ | |
"./path-to/babel-plugin-remove-proptypes.js" | |
] | |
} |
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
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