Skip to content

Commit

Permalink
add try-catch,能多hook一点是一点
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Jan 18, 2021
1 parent bfce7cd commit b59240d
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions src/components/global-assign-hook-component/core/inject-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ function injectHook(jsCode) {
varName = generator.default(variableDeclarator.id).code;
}

const hookFunctionArguments = [
types.stringLiteral(varName),
variableDeclarator.init,
types.stringLiteral("var-init")
];
variableDeclarator.init = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
try {
const hookFunctionArguments = [
types.stringLiteral(varName),
variableDeclarator.init,
types.stringLiteral("var-init")
];
variableDeclarator.init = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
} catch (e) {
console.error(e);
}
}
},

Expand All @@ -47,12 +51,16 @@ function injectHook(jsCode) {
varName = generator.default(node.left).code;
}

const hookFunctionArguments = [
types.stringLiteral(varName),
node.right,
types.stringLiteral("assign")
];
node.right = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
try {
const hookFunctionArguments = [
types.stringLiteral(varName),
node.right,
types.stringLiteral("assign")
];
node.right = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
} catch (e) {
console.error(e);
}
},

// 对象表达式
Expand All @@ -79,13 +87,18 @@ function injectHook(jsCode) {
objectKey = types.stringLiteral(objectKey.name);
}

const hookFunctionArguments = [
objectKey,
propertyValue,
types.stringLiteral("object-key-init")
];
objectProperty.value = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
try {
const hookFunctionArguments = [
objectKey,
propertyValue,
types.stringLiteral("object-key-init")
];
objectProperty.value = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
} catch (e) {
console.error(e);
}
}

},

// 函数的形参
Expand All @@ -97,15 +110,19 @@ function injectHook(jsCode) {
const params = node.params;
if (types.isBlockStatement(node.body)) {
// 函数体是个代码块的,则在代码块最前面插入Hook,检查参数的值
for(let i=params.length-1; i>=0; i--) {
const paramName = params[i];
const hookFunctionArguments = [
types.stringLiteral(generator.default(paramName).code),
paramName,
types.stringLiteral("function-parameter")
];
const hookFunction = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
node.body.body.unshift(types.expressionStatement(hookFunction));
for (let i = params.length - 1; i >= 0; i--) {
try {
const paramName = params[i];
const hookFunctionArguments = [
types.stringLiteral(generator.default(paramName).code),
paramName,
types.stringLiteral("function-parameter")
];
const hookFunction = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
node.body.body.unshift(types.expressionStatement(hookFunction));
} catch (e) {
console.error(e);
}
}
}
}
Expand Down

0 comments on commit b59240d

Please sign in to comment.