-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx2mp-loader): hackRegeneratorRuntimeFunction in ali miniapp of…
… copyNpm
- Loading branch information
Showing
2 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/jsx2mp-loader/src/babel-plugin-handle-regeneratorRuntime.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @Author 阿劭 [email protected] | ||
* @Date 2023-04-10 18:21:49 | ||
* @LastEditors 阿劭 [email protected] | ||
* @LastEditTime 2023-04-10 21:33:21 | ||
* @Description 小程序严格模式下报错 `Function(...) is not function` 兼容 | ||
* https://aliyuque.antfin.com/tianjie.stj/nivfgf/wi0gk43l6otk3lam?singleDoc#tnMFv | ||
*/ | ||
|
||
module.exports = function visitor({ types: t }) { | ||
|
||
/** | ||
* 判断节点是否是 Function("r", "regeneratorRuntime = r")(runtime); | ||
* @param {*} node | ||
*/ | ||
function isFunctionRegeneratorRuntime (node) { | ||
return ( | ||
t.isCallExpression(node.callee) && | ||
node.callee.callee.name === 'Function' && | ||
node.callee.arguments.length === 2 && | ||
t.isStringLiteral(node.callee.arguments[0]) && | ||
node.callee.arguments[0].value === 'r' && | ||
t.isStringLiteral(node.callee.arguments[1]) && | ||
node.callee.arguments[1].value === 'regeneratorRuntime = r' && | ||
node.arguments.length === 1 && | ||
t.isIdentifier(node.arguments[0]) && | ||
node.arguments[0].name === 'runtime' | ||
); | ||
} | ||
|
||
return { | ||
visitor: { | ||
CallExpression(path, state) { | ||
const { node } = path; | ||
if ( | ||
t.isCallExpression(node.callee) && | ||
node.callee.callee.name === 'Function' | ||
) { | ||
if (isFunctionRegeneratorRuntime(node)) { | ||
if (path.parentPath.node && t.isExpressionStatement(path.parentPath.node)) { | ||
path.parentPath.remove(); | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters