Babel syntax plugin that enables parsing of async functions in JavaScript code
npx @tessl/cli install tessl/npm-babel-plugin-syntax-async-functions@6.13.0babel-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.
npm install babel-plugin-syntax-async-functions// CommonJS
const syntaxAsyncFunctions = require("babel-plugin-syntax-async-functions");// ES Modules
import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions";{
"plugins": ["syntax-async-functions"]
}babel --plugins syntax-async-functions script.jsconst babel = require("babel-core");
babel.transform("code", {
plugins: ["syntax-async-functions"]
});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:
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.
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();
}
}