or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-syntax-async-functions

Babel syntax plugin that enables parsing of async functions in JavaScript code

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-syntax-async-functions@6.13.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-syntax-async-functions@6.13.0

index.mddocs/

babel-plugin-syntax-async-functions

babel-plugin-syntax-async-functions is a lightweight Babel syntax plugin that enables parsing of async functions in JavaScript code. The plugin extends Babel's parser to recognize async function syntax (introduced in ES2017), allowing other Babel plugins and presets to transform or analyze async/await code patterns. Without this syntax plugin, Babel would not be able to parse async functions and would throw syntax errors.

Package Information

  • Package Name: babel-plugin-syntax-async-functions
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-plugin-syntax-async-functions

Core Imports

// CommonJS
const syntaxAsyncFunctions = require("babel-plugin-syntax-async-functions");
// ES Modules  
import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions";

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["syntax-async-functions"]
}

Via CLI

babel --plugins syntax-async-functions script.js

Via Node API

const babel = require("babel-core");

babel.transform("code", {
  plugins: ["syntax-async-functions"]
});

Architecture

babel-plugin-syntax-async-functions follows Babel's plugin architecture as a syntax plugin. Syntax plugins are the first stage in Babel's processing pipeline:

  1. Parser Stage: Syntax plugins extend Babel's parser (Babylon) to recognize new syntax patterns
  2. Plugin Registration: The plugin registers "asyncFunctions" with the parser's plugins array
  3. Syntax Recognition: Enables the parser to create proper AST nodes for async functions
  4. Downstream Processing: Other transform plugins can then process the parsed async function nodes

This plugin is purely for syntax recognition - it doesn't transform code. It's typically used as a dependency by other plugins that need to process async functions, or included in presets like @babel/preset-env when targeting environments that support async/await.

Capabilities

Syntax Plugin Function

The main export is a Babel plugin factory function that returns a plugin object to enable async function parsing.

/**
 * Default export: Creates a Babel plugin that enables async function syntax parsing
 * @returns {Object} Babel plugin object with manipulateOptions method
 */
export default function (): BabelPlugin;

// Note: TypeScript interfaces below are provided for documentation clarity.
// The actual source code is JavaScript.
interface BabelPlugin {
  /**
   * Babel plugin hook that modifies parser options to enable async function parsing
   * @param {Object} opts - Babel transformation options
   * @param {Object} parserOpts - Babel parser options (modified to include "asyncFunctions" plugin)
   */
  manipulateOptions(opts: any, parserOpts: ParserOptions): void;
}

interface ParserOptions {
  plugins: string[];
}

Usage Pattern:

The plugin works by adding "asyncFunctions" to the Babel parser's plugins array, which tells the parser to recognize async function syntax:

// What the plugin does internally:
parserOpts.plugins.push("asyncFunctions");

This enables the parser to correctly handle code like:

// These async function patterns will be parsed correctly:
async function fetchData() {
  return await fetch('/api/data');
}

const asyncArrow = async () => {
  return await someOperation();
};

class MyClass {
  async method() {
    return await this.process();
  }
}