Created
January 8, 2016 18:17
-
-
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
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
// 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