rn-blockmonitor
Version:
block monitor
173 lines (164 loc) • 7.01 kB
JavaScript
const types = require('@babel/types');
const generator = require('@babel/generator');
const traverse = require('@babel/traverse');
const template = require('@babel/template');
const Path = require('path');
function getAstCode(name, params) {
const globalCallStack = types.memberExpression(types.identifier('global'), types.identifier('callStack'));
const globalCallStackPush = types.memberExpression(globalCallStack, types.identifier('push'));
const timestamp = types.callExpression(types.memberExpression(types.newExpression(types.identifier('Date'), []), types.identifier('getTime')), []);
const objectExpression = types.objectExpression([
types.objectProperty(types.identifier('timeStamp'), timestamp),
types.objectProperty(types.identifier('stackName'), types.stringLiteral(name))
]);
const 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) {
// console.log(`\ncan not find ${fun}-${str1}\n`);
} else {
// console.log(`\ncan find ${fun}-${str1}\n`);
}
}
function execute(ast, tPath, params) {
traverse.default(ast, {
ClassMethod(path) {
if (path.node.body.type === 'BlockStatement') {
if (filter(path.node.key.name)) {
const pp = path.node.key;
const cc = path.node.key.name;
const fun = 'ClassMethod';
logError(fun, pp);
logError(fun, cc);
// path.node.body.body.unshift(getAstCode(tPath + path.node.key.name));
path.node.body.body = getAstCode(
tPath + path.node.key.name,
params
)
.concat(path.node.body.body);
}
}
},
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') {
const stack = getCallStack(path);
const pp = stack;
const cc = stack;
const fun = 'ArrowFunctionExpression';
logError(fun, pp);
logError(fun, cc);
// path.node.body.body.unshift(getAstCode(tPath + stack));
path.node.body.body = getAstCode(
tPath + stack,
params
)
.concat(path.node.body.body);
}
},
FunctionDeclaration(path) {
if (path.node.id && path.node.id.name && filter(path.node.id.name)) {
const pp = path.node.id;
const cc = path.node.id.name;
const fun = 'FunctionDeclaration';
logError(fun, pp);
logError(fun, cc);
// path.node.body.body.unshift(getAstCode(tPath + path.node.id.name));
path.node.body.body = getAstCode(
tPath + path.node.id.name,
params
)
.concat(path.node.body.body);
}
}
});
}
function getVisitor(
tPath,
params
) {
const v = {
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);
const { showLocalTerminalCodeByFileName } = params;
if (showLocalTerminalCodeByFileName && showLocalTerminalCodeByFileName.length) {
if (state.file.opts.filename.match(showLocalTerminalCodeByFileName)) {
console.log(`\nfilename-code-begin:${showLocalTerminalCodeByFileName}↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n`);
console.log(generator.default(path.node).code);
console.log(`\nfilename-code-end:${showLocalTerminalCodeByFileName}↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n`);
}
} else if (params.showLocalTerminalCodeByAllFile) {
console.log(generator.default(path.node).code);
}
}
}
};
return v;
}
module.exports = function (babel, params, path) {
const reparams = params;
reparams.recordLength = params.recordLength ? params.recordLength : 20;
const visitor = getVisitor(
Path.join(path, (params.root ? params.root : '')),
reparams
);
return { visitor };
};