1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
9 |
|
10 | var _pluginSyntaxFunctionBind = _interopRequireDefault(require("@babel/plugin-syntax-function-bind"));
|
11 |
|
12 | var _core = require("@babel/core");
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | var _default = (0, _helperPluginUtils.declare)(api => {
|
17 | api.assertVersion(7);
|
18 |
|
19 | function getTempId(scope) {
|
20 | let id = scope.path.getData("functionBind");
|
21 | if (id) return id;
|
22 | id = scope.generateDeclaredUidIdentifier("context");
|
23 | return scope.path.setData("functionBind", id);
|
24 | }
|
25 |
|
26 | function getStaticContext(bind, scope) {
|
27 | const object = bind.object || bind.callee.object;
|
28 | return scope.isStatic(object) && object;
|
29 | }
|
30 |
|
31 | function inferBindContext(bind, scope) {
|
32 | const staticContext = getStaticContext(bind, scope);
|
33 | if (staticContext) return _core.types.cloneNode(staticContext);
|
34 | const tempId = getTempId(scope);
|
35 |
|
36 | if (bind.object) {
|
37 | bind.callee = _core.types.sequenceExpression([_core.types.assignmentExpression("=", tempId, bind.object), bind.callee]);
|
38 | } else {
|
39 | bind.callee.object = _core.types.assignmentExpression("=", tempId, bind.callee.object);
|
40 | }
|
41 |
|
42 | return tempId;
|
43 | }
|
44 |
|
45 | return {
|
46 | name: "proposal-function-bind",
|
47 | inherits: _pluginSyntaxFunctionBind.default,
|
48 | visitor: {
|
49 | CallExpression({
|
50 | node,
|
51 | scope
|
52 | }) {
|
53 | const bind = node.callee;
|
54 | if (!_core.types.isBindExpression(bind)) return;
|
55 | const context = inferBindContext(bind, scope);
|
56 | node.callee = _core.types.memberExpression(bind.callee, _core.types.identifier("call"));
|
57 | node.arguments.unshift(context);
|
58 | },
|
59 |
|
60 | BindExpression(path) {
|
61 | const {
|
62 | node,
|
63 | scope
|
64 | } = path;
|
65 | const context = inferBindContext(node, scope);
|
66 | path.replaceWith(_core.types.callExpression(_core.types.memberExpression(node.callee, _core.types.identifier("bind")), [context]));
|
67 | }
|
68 |
|
69 | }
|
70 | };
|
71 | });
|
72 |
|
73 | exports.default = _default; |
\ | No newline at end of file |