Skip to content

Commit b59240d

Browse files
committed
add try-catch,能多hook一点是一点
1 parent bfce7cd commit b59240d

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/components/global-assign-hook-component/core/inject-hook.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ function injectHook(jsCode) {
2727
varName = generator.default(variableDeclarator.id).code;
2828
}
2929

30-
const hookFunctionArguments = [
31-
types.stringLiteral(varName),
32-
variableDeclarator.init,
33-
types.stringLiteral("var-init")
34-
];
35-
variableDeclarator.init = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
30+
try {
31+
const hookFunctionArguments = [
32+
types.stringLiteral(varName),
33+
variableDeclarator.init,
34+
types.stringLiteral("var-init")
35+
];
36+
variableDeclarator.init = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
37+
} catch (e) {
38+
console.error(e);
39+
}
3640
}
3741
},
3842

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

50-
const hookFunctionArguments = [
51-
types.stringLiteral(varName),
52-
node.right,
53-
types.stringLiteral("assign")
54-
];
55-
node.right = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
54+
try {
55+
const hookFunctionArguments = [
56+
types.stringLiteral(varName),
57+
node.right,
58+
types.stringLiteral("assign")
59+
];
60+
node.right = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
61+
} catch (e) {
62+
console.error(e);
63+
}
5664
},
5765

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

82-
const hookFunctionArguments = [
83-
objectKey,
84-
propertyValue,
85-
types.stringLiteral("object-key-init")
86-
];
87-
objectProperty.value = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
90+
try {
91+
const hookFunctionArguments = [
92+
objectKey,
93+
propertyValue,
94+
types.stringLiteral("object-key-init")
95+
];
96+
objectProperty.value = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
97+
} catch (e) {
98+
console.error(e);
99+
}
88100
}
101+
89102
},
90103

91104
// 函数的形参
@@ -97,15 +110,19 @@ function injectHook(jsCode) {
97110
const params = node.params;
98111
if (types.isBlockStatement(node.body)) {
99112
// 函数体是个代码块的,则在代码块最前面插入Hook,检查参数的值
100-
for(let i=params.length-1; i>=0; i--) {
101-
const paramName = params[i];
102-
const hookFunctionArguments = [
103-
types.stringLiteral(generator.default(paramName).code),
104-
paramName,
105-
types.stringLiteral("function-parameter")
106-
];
107-
const hookFunction = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
108-
node.body.body.unshift(types.expressionStatement(hookFunction));
113+
for (let i = params.length - 1; i >= 0; i--) {
114+
try {
115+
const paramName = params[i];
116+
const hookFunctionArguments = [
117+
types.stringLiteral(generator.default(paramName).code),
118+
paramName,
119+
types.stringLiteral("function-parameter")
120+
];
121+
const hookFunction = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
122+
node.body.body.unshift(types.expressionStatement(hookFunction));
123+
} catch (e) {
124+
console.error(e);
125+
}
109126
}
110127
}
111128
}

0 commit comments

Comments
 (0)