@babel/plugin-proposal-function-sent is a Babel plugin that compiles the function.sent meta property to valid ES2015 code. It transforms the proposed function.sent syntax in generator functions into standard JavaScript that works in current environments.
npm install --save-dev @babel/plugin-proposal-function-sentimport functionSentPlugin from "@babel/plugin-proposal-function-sent";For CommonJS:
const functionSentPlugin = require("@babel/plugin-proposal-function-sent");Add the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-proposal-function-sent"]
}Or programmatically:
import * as babel from "@babel/core";
import functionSentPlugin from "@babel/plugin-proposal-function-sent";
const result = babel.transform(code, {
plugins: [functionSentPlugin]
});Input (using function.sent):
function* gen() {
let sent = function.sent;
}Output (compiled):
function gen() {
return _gen.apply(this, arguments);
}
function _gen() {
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
let sent = _functionSent;
});
return _gen.apply(this, arguments);
}The main export is a Babel plugin function that transforms function.sent meta properties in generator functions.
/**
* Babel plugin that transforms function.sent meta properties
*/
export default function(api: any): {
name: "proposal-function-sent";
manipulateOptions: (options: any, parser: { plugins: string[] }) => void;
visitor: {
MetaProperty: (path: any, state: any) => void;
};
};The plugin performs these transformations:
function.sent meta property by adding "functionSent" to parser pluginsfunction.sent is used only within generator functionsfunction.sentfunction.sent references with the unique identifieryield expressions to assignment expressionsThe plugin throws an error when function.sent is used outside of a generator function:
// Error thrown when function.sent is used in non-generator function
throw new Error("Parent generator function not found");The plugin has these dependencies:
Peer dependencies:
^7.0.0-0 - Babel core functionality for AST manipulationlet sent, yielded;
function* gen() {
sent = function.sent;
yielded = yield;
}
const it = gen();
it.next(1); // Start generator with value 1
it.next(2); // Send 2 to the yield expression
// sent === 1 (the value sent to start the generator)
// yielded === 2 (the value sent to the yield expression)const values = [];
function* gen() {
values.push(function.sent); // First sent value
values.push(function.sent); // Same value as above
values.push(yield "foo"); // Value sent after first yield
values.push(function.sent); // Same as yield value
values.push(yield); // Value sent after second yield
values.push(function.sent); // Same as second yield value
values.push(function.sent); // Still the same value
}
const it = gen();
it.next(1); // values: [1, 1]
it.next(2); // values: [1, 1, 2, 2]
it.next(3); // values: [1, 1, 2, 2, 3, 3, 3](function* () {
const a = function.sent;
const b = function.sent;
yield 4;
const c = function.sent;
const d = yield;
const e = function.sent;
return [a, b, c, d, e];
}());// The plugin is created using declare() from @babel/helper-plugin-utils
// and uses wrapFunction from @babel/helper-wrap-function
// No additional types are exported by this plugin