Skip to content

Instantly share code, notes, and snippets.

@chestone
Last active May 11, 2017 23:01
Show Gist options
  • Save chestone/88bb93e918037fdae39d1f116b9ad880 to your computer and use it in GitHub Desktop.
Save chestone/88bb93e918037fdae39d1f116b9ad880 to your computer and use it in GitHub Desktop.
Add 'prop-types' imports & rewrite your PropTypes statements.
module.exports = function(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
let importPropTypes = false;
const modifyPropTypes = (path) => {
if (!importPropTypes) importPropTypes = true;
return {
type: 'Identifier',
name: 'PropTypes',
};
};
const propTypesImport = {
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportDefaultSpecifier',
local: {
type: 'Identifier',
name: 'PropTypes',
},
},
],
source: {
type: 'Literal',
value: 'prop-types',
},
};
root.find(j.MemberExpression,
{
object: {type: 'Identifier', name: 'React'},
property: {type: 'Identifier', name: 'PropTypes'},
})
.replaceWith(modifyPropTypes);
if (importPropTypes) {
root.find(j.ImportDeclaration,
{
source: {
value: 'react',
},
})
.insertAfter(propTypesImport);
}
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment