-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.ts
76 lines (66 loc) 路 2.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { declare } from "@babel/helper-plugin-utils";
import syntaxFunctionBind from "@babel/plugin-syntax-function-bind";
import { types as t } from "@babel/core";
import type { Scope } from "@babel/traverse";
export default declare(api => {
api.assertVersion(7);
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 getStaticContext(bind: t.BindExpression, scope: Scope) {
const object =
bind.object ||
// @ts-ignore Fixme: should check bind.callee type first
bind.callee.object;
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 {
// @ts-ignore Fixme: should check bind.callee type first
bind.callee.object = t.assignmentExpression(
"=",
tempId,
// @ts-ignore Fixme: should check bind.callee type first
bind.callee.object,
);
}
return t.cloneNode(tempId);
}
return {
name: "proposal-function-bind",
inherits: syntaxFunctionBind,
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],
),
);
},
},
};
});