Created
April 18, 2018 02:43
-
-
Save palashkulsh/3dc6c751c48c9a6b96cefc6cf56724a1 to your computer and use it in GitHub Desktop.
jscodeshift codemod for function names in debugFlow logs
This file contains hidden or 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
//http://astexplorer.net/#/84e8ZqEAwQ/6 | |
//http://www.datasciencecentral.com/profiles/blogs/write-code-to-rewrite-your-code-jscodeshift?utm_content=buffer598df&utm_medium=social&utm_source=linkedin.com&utm_campaign=buffer | |
//https://github.com/reergymerej/jscodeshift-article | |
//https://vramana.github.io/blog/2015/12/21/codemod-tutorial/ | |
//https://github.com/benjamn/recast | |
function extractNodes(node){ | |
if (node.type === 'FunctionDeclaration') { | |
var arr=[]; | |
arr.concat(extractNodes(node.left)); | |
arr.concat(extractNodes(node.right)); | |
return arr; | |
} | |
return [ node ] | |
} | |
module.exports = function (file, api) { | |
const j = api.jscodeshift; | |
const { | |
expression, statement, statements | |
} = j.template; | |
const root = j(file.source); | |
root.find(j.FunctionDeclaration).replaceWith(function (_if) { | |
var fnCall = j.callExpression(j.identifier('debugFlow'),[j.identifier("'"+_if.node.id.name+"'")]); | |
var ret = j.expressionStatement(fnCall); | |
var stArr=[ret] | |
stArr=stArr.concat(_if.node.body.body); | |
var block = j.blockStatement(stArr); | |
_if.node.body = block; | |
return _if.node; | |
}); | |
var funcnum=1; | |
root.find(j.FunctionExpression).replaceWith(function (_if) { | |
if(!_if.node.id){ | |
_if.node.id='cb'+funcnum++; | |
} | |
var fnCall = j.callExpression(j.identifier('debugFlow'),[j.identifier("'"+_if.node.id+"'")]); | |
var ret = j.expressionStatement(fnCall); | |
var stArr=[ret] | |
stArr=stArr.concat(_if.node.body.body); | |
var block = j.blockStatement(stArr); | |
_if.node.body = block; | |
return _if.node; | |
}); | |
return root.toSource(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment