or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-proposal-function-sent

Compile the function.sent meta property to valid ES2015 code

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-proposal-function-sent@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-proposal-function-sent@7.27.0

index.mddocs/

@babel/plugin-proposal-function-sent

@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.

Package Information

  • Package Name: @babel/plugin-proposal-function-sent
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-proposal-function-sent

Core Imports

import functionSentPlugin from "@babel/plugin-proposal-function-sent";

For CommonJS:

const functionSentPlugin = require("@babel/plugin-proposal-function-sent");

Basic Usage

Babel Configuration

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/Output Example

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);
}

Capabilities

Babel Plugin

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;
  };
};

Plugin Behavior

The plugin performs these transformations:

  1. Syntax Parsing: Enables parsing of function.sent meta property by adding "functionSent" to parser plugins
  2. Generator Detection: Validates that function.sent is used only within generator functions
  3. Variable Creation: Generates a unique identifier for each function scope using function.sent
  4. AST Transformation:
    • Replaces function.sent references with the unique identifier
    • Transforms yield expressions to assignment expressions
    • Injects a variable declaration at the beginning of the generator function
    • Wraps the generator function with a helper for proper execution

Error Handling

The 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");

Dependencies

The plugin has these dependencies:

  • @babel/helper-plugin-utils: Plugin utility helpers for plugin creation
  • @babel/helper-wrap-function: Function wrapping utilities for generator transformation

Peer dependencies:

  • @babel/core: ^7.0.0-0 - Babel core functionality for AST manipulation

Usage Patterns

Basic Function.sent Usage

let 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)

Multiple Function.sent References

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]

Multiple Usage in One Generator

(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];
}());

Types

// 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