or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

@babel/plugin-syntax-async-generators

A Babel syntax plugin that enables parsing of async generator functions. This plugin adds parser support for the async generator syntax introduced in ES2018, allowing other Babel plugins to transform or analyze code containing async generator functions.

Package Information

  • Package Name: @babel/plugin-syntax-async-generators
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev @babel/plugin-syntax-async-generators

Core Imports

import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";

For CommonJS:

const syntaxAsyncGenerators = require("@babel/plugin-syntax-async-generators");

Basic Usage

The plugin is typically used in Babel configuration or as a dependency for other plugins:

// babel.config.js
module.exports = {
  plugins: ["@babel/plugin-syntax-async-generators"]
};

// Or as a dependency in other plugins
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";

export default declare(api => {
  return {
    name: "my-transform-plugin",
    inherits: syntaxAsyncGenerators,
    visitor: {
      // transformation logic here
    }
  };
});

Enabled Syntax

Once this plugin is loaded, Babel can parse async generator functions:

// Async generator function declaration
async function* fetchData() {
  yield await fetch('/api/data1');
  yield await fetch('/api/data2');
}

// Async generator method in class
class DataProvider {
  async* getData() {
    for (const item of this.items) {
      yield await this.processItem(item);
    }
  }
}

// Async generator expression
const generator = async function* () {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
};

Capabilities

Plugin Factory Function

The main export is a Babel plugin factory function that creates the plugin configuration.

/**
 * Creates a Babel plugin that enables async generator syntax parsing
 * @param api - Babel API object containing helper functions
 * @returns Plugin configuration object
 */
function syntaxAsyncGenerators(api: BabelAPI): BabelPlugin;

interface BabelAPI {
  assertVersion(version: number): void;
  // Other Babel API methods...
}

interface BabelPlugin {
  name: string;
  manipulateOptions(opts: BabelOptions, parserOpts: ParserOptions): void;
}

Plugin Configuration

The returned plugin object contains the following properties:

interface PluginConfig {
  /** Plugin identifier */
  name: "syntax-async-generators";
  /** Function that modifies parser options to enable async generator parsing */
  manipulateOptions(opts: BabelOptions, parserOpts: ParserOptions): void;
}

Parser Options Manipulation

The manipulateOptions function enables async generator parsing by adding the parser plugin.

/**
 * Adds async generator parsing capability to Babel parser
 * @param opts - Babel transformation options
 * @param parserOpts - Parser-specific options to modify
 */
function manipulateOptions(
  opts: BabelOptions, 
  parserOpts: ParserOptions
): void;

interface ParserOptions {
  plugins: string[];
  // Other parser options...
}

interface BabelOptions {
  // Babel configuration options
}

The function adds "asyncGenerators" to the parserOpts.plugins array, enabling the parser to recognize and tokenize async generator syntax.

Integration Patterns

As Plugin Dependency

Most commonly used as a dependency in transformation plugins:

import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
  return {
    name: "transform-async-generators",
    inherits: syntaxAsyncGenerators, // Enables parsing
    visitor: {
      // Transform async generator functions
      Function(path) {
        if (path.node.async && path.node.generator) {
          // Transformation logic
        }
      }
    }
  };
});

In Babel Presets

Included automatically in modern Babel presets:

// babel.config.js
module.exports = {
  presets: [
    ["@babel/preset-env", {
      // async generators syntax automatically included
    }]
  ]
};

Direct Usage

Rarely used directly, but can be added to plugin lists:

// babel.config.js
module.exports = {
  plugins: [
    "@babel/plugin-syntax-async-generators",
    // Other plugins that might transform async generators
  ]
};

Dependencies

interface Dependencies {
  "@babel/helper-plugin-utils": "^7.8.0";
}

interface PeerDependencies {
  "@babel/core": "^7.0.0-0";
}

Parser Plugin Details

The plugin adds the "asyncGenerators" parser plugin to Babel's parser, which enables recognition of:

  • async function* declarations
  • async generator methods in classes and objects
  • async function* expressions
  • for await loops within async generators
  • yield expressions within async generator functions
  • yield* (yield delegation) with async iterables

Error Handling

If the plugin is not loaded and async generator syntax is encountered, Babel will throw a syntax error indicating the need for this plugin:

SyntaxError: Support for async generators is not enabled