@babel/plugin-proposal-function-sent
Version:
Compile the function.sent meta property to valid ES2015 code
53 lines (50 loc) • 1.75 kB
JavaScript
import { declare } from '@babel/helper-plugin-utils';
import wrapFunction from '@babel/helper-wrap-function';
import { types } from '@babel/core';
const index = declare(api => {
api.assertVersion("^7.0.0-0 || ^8.0.0");
const isFunctionSent = node => types.isIdentifier(node.meta, {
name: "function"
}) && types.isIdentifier(node.property, {
name: "sent"
});
const hasBeenReplaced = (node, sentId) => types.isAssignmentExpression(node) && types.isIdentifier(node.left, {
name: sentId
});
const yieldVisitor = {
Function(path) {
path.skip();
},
YieldExpression(path) {
if (!hasBeenReplaced(path.parent, this.sentId)) {
path.replaceWith(types.assignmentExpression("=", types.identifier(this.sentId), path.node));
}
},
MetaProperty(path) {
if (isFunctionSent(path.node)) {
path.replaceWith(types.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
});
fnPath.node.body.body.unshift(types.variableDeclaration("let", [types.variableDeclarator(types.identifier(sentId), types.yieldExpression())]));
wrapFunction(fnPath, state.addHelper("skipFirstGeneratorNext"));
}
}
};
});
export { index as default };
//# sourceMappingURL=index.js.map