Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Codemod to convert class methods to properties, and remove `.bind(this)` in JSX props
// Codemod to convert class methods to properties, and remove `.bind(this)` in JSX props.
// Doesn't support Flow annotations yet
export default function(file, api) {
const j = api.jscodeshift;
const convertToClassProps = p => {
const node = p.node;
if (node.key.name.indexOf('_') === 0) {
node.type = 'ClassProperty';
node.value.type = 'ArrowFunctionExpression';
}
};
const removeFunctionBind = p => {
if (p.node.expression.type === 'CallExpression' && p.node.expression.callee.property && p.node.expression.callee.property.name === 'bind') {
p.node.expression = p.node.expression.callee.object;
}
};
return j(file.source)
.find(j.MethodDefinition)
.forEach(convertToClassProps)
.find(j.JSXExpressionContainer)
.forEach(removeFunctionBind)
.toSource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.