Skip to content

Instantly share code, notes, and snippets.

@latentflip
Created March 1, 2016 12:18
Show Gist options
  • Save latentflip/c476ddee24d09405a6e9 to your computer and use it in GitHub Desktop.
Save latentflip/c476ddee24d09405a6e9 to your computer and use it in GitHub Desktop.
codemod
module.exports = function transformer(file, api) {
const j = api.jscodeshift;
const update = path => {
if (path.node.expression) {
const body = j.blockStatement([j.expressionStatement(path.node.body)]);
return j.functionExpression(path.node.id, path.node.params, body, true, false)
}
return j.functionExpression(path.node.id, path.node.params, path.node.body, true, false)
};
const isLab = node => {
return node.declarations[0].id.name === 'lab';
}
const toYield = path => {
return j.yieldExpression(path.node.argument);
}
const root = j(file.source);
root.find(j.Identifier, { name: 'promiseLab' })
.replaceWith(j.identifier('coLab'))
//root.find(j.VariableDeclaration, isLab)
// .insertAfter("const Co = require('co');")
root.find(j.ArrowFunctionExpression, { async: true })
.replaceWith(update)
root.find(j.AwaitExpression)
.replaceWith(toYield)
return root.toSource({quote: 'single'});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment