rn-blockmonitor
Version:
block monitor
1 lines • 6.11 kB
JavaScript
var types=require('@babel/types');var generator=require('@babel/generator');var traverse=require('@babel/traverse');var template=require('@babel/template');var Path=require('path');function getAstCode(name,params){var globalCallStack=types.memberExpression(types.identifier('global'),types.identifier('callStack'));var globalCallStackPush=types.memberExpression(globalCallStack,types.identifier('push'));var timestamp=types.callExpression(types.memberExpression(types.newExpression(types.identifier('Date'),[]),types.identifier('getTime')),[]);var objectExpression=types.objectExpression([types.objectProperty(types.identifier('timeStamp'),timestamp),types.objectProperty(types.identifier('stackName'),types.stringLiteral(name))]);var safeCheck=types.ifStatement(globalCallStack,types.blockStatement([types.expressionStatement(types.callExpression(globalCallStackPush,[objectExpression]))]));return[safeCheck];}function filter(s){return s!=='_wrapComponent'&&s!=='default_1'&&s!=='_component';}function getCallStack(path,stack){if(!stack){stack=[];}if(path.parentPath){if(path.parentPath.node.callee&&path.parentPath.node.callee.property&&path.parentPath.node.callee.property.name){if(filter(path.parentPath.node.callee.property.name)){stack.unshift(path.parentPath.node.callee.property.name);}}else if(path.parentPath.node.callee&&path.parentPath.node.callee.name){if(filter(path.parentPath.node.callee.name)){stack.unshift(path.parentPath.node.callee.name);}}else if(path.parentPath.node.name&&path.parentPath.node.name.name){if(filter(path.parentPath.node.name.name)){stack.unshift(path.parentPath.node.name.name);}}else if(path.parentPath.node.type==='AssignmentExpression'&&path.parentPath.node.left.property&&path.parentPath.node.left.property.name){if(filter(path.parentPath.node.left.property.name)){stack.unshift(path.parentPath.node.left.property.name);}}else if(path.parentPath.node.type==='VariableDeclarator'&&path.parentPath.node.id&&path.parentPath.node.id.name){if(filter(path.parentPath.node.id.name)){stack.unshift(path.parentPath.node.id.name);}}else if(path.parentPath.node.type==='FunctionDeclaration'&&path.parentPath.node.id&&path.parentPath.node.id.name){if(filter(path.parentPath.node.id.name)){stack.unshift(path.parentPath.node.id.name);}}else if(path.parentPath.node.type==='ClassProperty'&&path.parentPath.node.key&&path.parentPath.node.key.name){if(filter(path.parentPath.node.key.name)){stack.unshift(path.parentPath.node.key.name);}}getCallStack(path.parentPath,stack);}return stack.join('.');}function logError(fun,str1){if(!str1||!str1.length){}else{}}function execute(ast,tPath,params){traverse.default(ast,{ClassMethod:function ClassMethod(path){if(path.node.body.type==='BlockStatement'){if(filter(path.node.key.name)){var pp=path.node.key;var cc=path.node.key.name;var fun='ClassMethod';logError(fun,pp);logError(fun,cc);path.node.body.body=getAstCode(tPath+path.node.key.name,params).concat(path.node.body.body);}}},ArrowFunctionExpression:function ArrowFunctionExpression(path){if(path.node.body.type==='CallExpression'||path.node.body.type==='JSXElement'){path.node.body=types.blockStatement([types.returnStatement(path.node.body)]);}if(path.node.body.type==='BlockStatement'){var stack=getCallStack(path);var pp=stack;var cc=stack;var fun='ArrowFunctionExpression';logError(fun,pp);logError(fun,cc);path.node.body.body=getAstCode(tPath+stack,params).concat(path.node.body.body);}},FunctionDeclaration:function FunctionDeclaration(path){if(path.node.id&&path.node.id.name&&filter(path.node.id.name)){var pp=path.node.id;var cc=path.node.id.name;var fun='FunctionDeclaration';logError(fun,pp);logError(fun,cc);path.node.body.body=getAstCode(tPath+path.node.id.name,params).concat(path.node.body.body);}}});}function getVisitor(tPath,params){var v={Program:function Program(path,state){if(state.file.opts.filename.indexOf(tPath)>-1&&!state.file.opts.filename.match('BMJSEventLoopWatchdog')&&!state.file.opts.filename.match('BMLinkList')&&!state.file.opts.filename.match('BMMonitor')){console.log('house-plugin-call-stack-recorder handling:'+state.file.opts.filename);execute(path.parent,state.file.opts.filename+'--',params);var showLocalTerminalCodeByFileName=params.showLocalTerminalCodeByFileName;if(showLocalTerminalCodeByFileName&&showLocalTerminalCodeByFileName.length){if(state.file.opts.filename.match(showLocalTerminalCodeByFileName)){console.log('\nfilename-code-begin:'+showLocalTerminalCodeByFileName+'\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\n');console.log(generator.default(path.node).code);console.log('\nfilename-code-end:'+showLocalTerminalCodeByFileName+'\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\u2191\n');}}else if(params.showLocalTerminalCodeByAllFile){console.log(generator.default(path.node).code);}}}};return v;}module.exports=function(babel,params,path){var reparams=params;reparams.recordLength=params.recordLength?params.recordLength:20;var visitor=getVisitor(Path.join(path,params.root?params.root:''),reparams);return{visitor:visitor};};