/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/babel-plugin-proposal-function-bind/src/index.ts
78 строк
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 { types as t, type Scope } from "@babel/core"; export default declare(api => { api.assertVersion(REQUIRED_VERSION("^7.0.0-0 || ^8.0.0")); function getTempId(scope: Scope) { let id = scope.path.getData("functionBind"); if (id) return t.cloneNode(id); id = scope.generateDeclaredUidIdentifier("context"); return scope.path.setData("functionBind", id); } function getObject(bind: t.BindExpression) { if (t.isExpression(bind.object)) { return bind.object; } return (bind.callee as t.MemberExpression).object; } function getStaticContext(bind: t.BindExpression, scope: Scope) { const object = getObject(bind); return ( scope.isStatic(object) && (t.isSuper(object) ? t.thisExpression() : object) ); } function inferBindContext(bind: t.BindExpression, scope: Scope) { const staticContext = getStaticContext(bind, scope); if (staticContext) return t.cloneNode(staticContext); const tempId = getTempId(scope); if (bind.object) { bind.callee = t.sequenceExpression([ t.assignmentExpression("=", tempId, bind.object), bind.callee, ]); } else if (t.isMemberExpression(bind.callee)) { bind.callee.object = t.assignmentExpression( "=", tempId, // @ts-expect-error(Babel 7 vs Babel 8) TODO(Babel 8) bind.callee.object, ); } return t.cloneNode(tempId); } return { name: "proposal-function-bind", manipulateOptions: (_, parser) => parser.plugins.push("functionBind"), visitor: { CallExpression({ node, scope }) { const bind = node.callee; if (!t.isBindExpression(bind)) return; const context = inferBindContext(bind, scope); node.callee = t.memberExpression(bind.callee, t.identifier("call")); node.arguments.unshift(context); }, BindExpression(path) { const { node, scope } = path; const context = inferBindContext(node, scope); path.replaceWith( t.callExpression( t.memberExpression(node.callee, t.identifier("bind")), [context], ), ); }, }, }; });