Babel syntax plugin that enables parsing of the function.sent meta property. This plugin extends Babel's parser capabilities to recognize the function.sent syntax without performing any transformations, allowing developers to use this JavaScript language feature proposal in their code.
npm install --save-dev @babel/plugin-syntax-function-sent// CommonJS
const functionSentPlugin = require("@babel/plugin-syntax-function-sent");ES Module:
import functionSentPlugin from "@babel/plugin-syntax-function-sent";Configure in your Babel configuration:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-syntax-function-sent"]
};Or with options:
// .babelrc.json
{
"plugins": ["@babel/plugin-syntax-function-sent"]
}After configuration, you can use the function.sent syntax in your JavaScript code:
function* generatorFunction() {
const value = function.sent;
console.log("Received:", value);
yield "response";
}
const gen = generatorFunction();
gen.next();
gen.next("hello"); // Will log "Received: hello"This plugin follows Babel's standard syntax plugin architecture:
declare helperThe main plugin factory function that creates a Babel syntax plugin.
/**
* Default export: Babel plugin factory function
* Creates a plugin that enables function.sent syntax parsing
*/
export default declare((api: PluginAPI) => PluginObject);
/**
* Plugin factory implementation (conceptual representation)
* @param api - Babel plugin API object with version and utilities
* @returns Babel plugin object with syntax extension capabilities
*/
function pluginFactory(api: PluginAPI): PluginObject {
api.assertVersion("^7.0.0-0"); // REQUIRED_VERSION(7) is a build-time macro
return {
name: "syntax-function-sent",
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("functionSent");
}
};
}The plugin returns an object with the following properties:
interface PluginObject {
/** Plugin identifier name */
name: "syntax-function-sent";
/**
* Parser options manipulator
* Adds "functionSent" plugin to Babel's parser
* @param opts - Babel plugin options object
* @param parserOpts - Parser-specific options with plugins array
*/
manipulateOptions(opts: any, parserOpts: { plugins: string[] }): void;
}/**
* @babel/helper-plugin-utils - Plugin creation utilities
* Provides the declare helper function for plugin factory creation
*/
import { declare } from "@babel/helper-plugin-utils";
/**
* REQUIRED_VERSION build-time macro (provided by Babel build system)
* Transforms version requirements at compile time
* @param version - Required Babel major version number
* @returns Version constraint string (transformed at build time)
* @note This is a compile-time macro, not a runtime function
*/
declare function REQUIRED_VERSION(version: number): string;
/**
* PluginAPI interface from @babel/core
* Provides utilities for Babel plugin development
*/
interface PluginAPI {
/** Babel version string */
version: string;
/** Assert that the current Babel version matches requirements */
assertVersion(range: string | number): void;
}The plugin includes built-in version validation:
api.assertVersion()This plugin enables parsing of the function.sent meta property, which is part of a JavaScript language proposal. The function.sent property provides access to the value passed to a generator function's next() method:
function* example() {
console.log(function.sent); // undefined on first call
const received = yield "first";
console.log(function.sent); // value passed to next()
}
const gen = example();
gen.next(); // Logs: undefined
gen.next("hello"); // Logs: "hello"Note: This plugin only enables parsing of the syntax. To actually use function.sent functionality, you would need a corresponding transform plugin or native browser support.