Allow parsing of async generator functions
npx @tessl/cli install tessl/npm-babel--plugin-syntax-async-generators@7.8.0A 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.
npm install --save-dev @babel/plugin-syntax-async-generatorsimport syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";For CommonJS:
const syntaxAsyncGenerators = require("@babel/plugin-syntax-async-generators");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
}
};
});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);
};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;
}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;
}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.
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
}
}
}
};
});Included automatically in modern Babel presets:
// babel.config.js
module.exports = {
presets: [
["@babel/preset-env", {
// async generators syntax automatically included
}]
]
};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
]
};interface Dependencies {
"@babel/helper-plugin-utils": "^7.8.0";
}
interface PeerDependencies {
"@babel/core": "^7.0.0-0";
}The plugin adds the "asyncGenerators" parser plugin to Babel's parser, which enables recognition of:
async function* declarationsasync generator methods in classes and objectsasync function* expressionsfor await loops within async generatorsyield expressions within async generator functionsyield* (yield delegation) with async iterablesIf 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