Last active
May 11, 2017 23:01
-
-
Save chestone/88bb93e918037fdae39d1f116b9ad880 to your computer and use it in GitHub Desktop.
Add 'prop-types' imports & rewrite your PropTypes statements.
This file contains hidden or 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(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