/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/babel-plugin-proposal-function-sent/src/index.ts
67 строк
2 KB
Huáng Jùnliàng
chore: inline REQUIRED_VERSION(7) special case (#17897)
17 апр 2026, 16:26
Не верифицирован
17 апр 2026, 16:26
3b1954b
Код
Авторство
О чём код?
import { declare } from "@babel/helper-plugin-utils"; import wrapFunction from "@babel/helper-wrap-function"; import { types as t, type Visitor } from "@babel/core"; export default declare(api => { api.assertVersion(REQUIRED_VERSION("^7.0.0-0 || ^8.0.0")); const isFunctionSent = (node: t.MetaProperty) => t.isIdentifier(node.meta, { name: "function" }) && t.isIdentifier(node.property, { name: "sent" }); const hasBeenReplaced = ( node: t.Node, sentId: string, ): node is t.AssignmentExpression => t.isAssignmentExpression(node) && t.isIdentifier(node.left, { name: sentId }); const yieldVisitor: Visitor<{ sentId: string }> = { Function(path) { path.skip(); }, YieldExpression(path) { if (!hasBeenReplaced(path.parent, this.sentId)) { path.replaceWith( t.assignmentExpression("=", t.identifier(this.sentId), path.node), ); } }, MetaProperty(path) { if (isFunctionSent(path.node)) { path.replaceWith(t.identifier(this.sentId)); } }, }; return { name: "proposal-function-sent", manipulateOptions: (_, parser) => parser.plugins.push("functionSent"), visitor: { MetaProperty(path, state) { if (!isFunctionSent(path.node)) return; const fnPath = path.getFunctionParent()!; if (!fnPath.node.generator) { throw new Error("Parent generator function not found"); } const sentId = path.scope.generateUid("function.sent"); fnPath.traverse(yieldVisitor, { sentId }); // @ts-expect-error A generator must not be an arrow function fnPath.node.body.body.unshift( t.variableDeclaration("let", [ t.variableDeclarator(t.identifier(sentId), t.yieldExpression()), ]), ); wrapFunction(fnPath, state.addHelper("skipFirstGeneratorNext")); }, }, }; });