|
| 1 | +/** |
| 2 | + |
| 3 | + * @Date 2023-04-10 18:21:49 |
| 4 | + * @LastEditors 阿劭 [email protected] |
| 5 | + * @LastEditTime 2023-04-10 21:33:21 |
| 6 | + * @Description 小程序严格模式下报错 `Function(...) is not function` 兼容 |
| 7 | + * https://aliyuque.antfin.com/tianjie.stj/nivfgf/wi0gk43l6otk3lam?singleDoc#tnMFv |
| 8 | + */ |
| 9 | + |
| 10 | +module.exports = function visitor({ types: t }) { |
| 11 | + |
| 12 | + /** |
| 13 | + * 判断节点是否是 Function("r", "regeneratorRuntime = r")(runtime); |
| 14 | + * @param {*} node |
| 15 | + */ |
| 16 | + function isFunctionRegeneratorRuntime (node) { |
| 17 | + return ( |
| 18 | + t.isCallExpression(node.callee) && |
| 19 | + node.callee.callee.name === 'Function' && |
| 20 | + node.callee.arguments.length === 2 && |
| 21 | + t.isStringLiteral(node.callee.arguments[0]) && |
| 22 | + node.callee.arguments[0].value === 'r' && |
| 23 | + t.isStringLiteral(node.callee.arguments[1]) && |
| 24 | + node.callee.arguments[1].value === 'regeneratorRuntime = r' && |
| 25 | + node.arguments.length === 1 && |
| 26 | + t.isIdentifier(node.arguments[0]) && |
| 27 | + node.arguments[0].name === 'runtime' |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + return { |
| 32 | + visitor: { |
| 33 | + CallExpression(path, state) { |
| 34 | + const { node } = path; |
| 35 | + if ( |
| 36 | + t.isCallExpression(node.callee) && |
| 37 | + node.callee.callee.name === 'Function' |
| 38 | + ) { |
| 39 | + if (isFunctionRegeneratorRuntime(node)) { |
| 40 | + if (path.parentPath.node && t.isExpressionStatement(path.parentPath.node)) { |
| 41 | + path.parentPath.remove(); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + } |
| 47 | + }; |
| 48 | +}; |
0 commit comments