Skip to content

Instantly share code, notes, and snippets.

@satya164
Created January 8, 2016 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save satya164/e5320d924a3688bf2a34 to your computer and use it in GitHub Desktop.
Save satya164/e5320d924a3688bf2a34 to your computer and use it in GitHub Desktop.
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