/
index.ts
65 lines (52 loc) 路 1.75 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
import { declare } from "@babel/helper-plugin-utils";
import syntaxFunctionSent from "@babel/plugin-syntax-function-sent";
import wrapFunction from "@babel/helper-wrap-function";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
const isFunctionSent = node =>
t.isIdentifier(node.meta, { name: "function" }) &&
t.isIdentifier(node.property, { name: "sent" });
const hasBeenReplaced = (node, sentId) =>
t.isAssignmentExpression(node) &&
t.isIdentifier(node.left, { name: sentId });
const yieldVisitor = {
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",
inherits: syntaxFunctionSent,
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"));
},
},
};
});