Created
March 1, 2016 12:18
-
-
Save latentflip/c476ddee24d09405a6e9 to your computer and use it in GitHub Desktop.
codemod
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
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