Core API for retrieving and building helper ASTs with customization options. Essential for Babel plugins that need to inject runtime helpers.
Main function to retrieve and build a helper AST with customizations. This is the primary API used by Babel plugins to inject helper code.
/**
* Retrieve and build a helper AST with customizations
* @param name - Name of the helper to retrieve
* @param getDependency - Optional function to resolve helper dependencies
* @param bindingName - Optional binding name for the helper
* @param localBindings - Optional array of local binding names to avoid conflicts
* @param adjustAst - Optional function to adjust the AST after processing
* @returns Object containing AST nodes and global variables used
*/
function get(
name: string,
getDependency?: GetDependency,
bindingName?: string,
localBindings?: string[],
adjustAst?: AdjustAst
): { nodes: t.Program["body"]; globals: string[] };Usage Examples:
import { get } from "@babel/helpers";
// Basic helper retrieval
const helper = get("asyncToGenerator");
console.log(helper.nodes); // Array of AST nodes for the helper
console.log(helper.globals); // Array of global variables used
// With custom binding name
const customHelper = get("classCallCheck", undefined, "_classCallCheck");
// Helper will use _classCallCheck instead of default name
// With dependency resolver
const dependencyResolver = (name: string) => {
return t.identifier(`helper_${name}`);
};
const helperWithDeps = get("createClass", dependencyResolver);
// With local bindings to avoid conflicts
const localBindings = ["_createClass", "_defineProperty"];
const safeHelper = get("createClass", undefined, undefined, localBindings);
// Helper will rename variables to avoid conflicts with localBindingsGet the minimum Babel version required for a helper.
/**
* Get the minimum Babel version required for a helper
* @param name - Name of the helper
* @returns Minimum version string (e.g., "7.0.0", "7.18.14")
*/
function minVersion(name: string): string;Usage Examples:
import { minVersion } from "@babel/helpers";
// Check version requirements
console.log(minVersion("asyncToGenerator")); // "7.0.0-beta.0"
console.log(minVersion("OverloadYield")); // "7.18.14"
console.log(minVersion("classCallCheck")); // "7.0.0-beta.0"
// Use in plugin to check compatibility
function myBabelPlugin() {
return {
visitor: {
ClassDeclaration(path) {
const requiredVersion = minVersion("classCallCheck");
// Check if current Babel version meets requirement
if (satisfiesVersion(this.file.opts.version, requiredVersion)) {
const helper = this.addHelper("classCallCheck");
// Use helper...
}
}
}
};
}Get the list of dependencies for a helper.
/**
* Get the list of dependencies for a helper
* @param name - Name of the helper
* @returns ReadonlyArray of dependency helper names
*/
function getDependencies(name: string): ReadonlyArray<string>;Usage Examples:
import { getDependencies } from "@babel/helpers";
// Check helper dependencies
console.log(getDependencies("createClass"));
// ["defineProperty", "torepropertykey"]
console.log(getDependencies("asyncToGenerator"));
// [] (no dependencies)
console.log(getDependencies("inherits"));
// ["setPrototypeOf"]
// Use in dependency analysis
function analyzeDependencies(helperName: string): string[] {
const deps = getDependencies(helperName);
const allDeps = [...deps];
// Recursively get transitive dependencies
deps.forEach(dep => {
allDeps.push(...getDependencies(dep));
});
return [...new Set(allDeps)]; // Remove duplicates
}Check if a helper is marked as internal (not intended for public use).
/**
* Check if a helper is marked as internal
* @param name - Name of the helper
* @returns True if the helper is internal, false otherwise
*/
function isInternal(name: string): boolean;Usage Examples:
import { isInternal, list } from "@babel/helpers";
// Check if helper is internal
console.log(isInternal("asyncToGenerator")); // false
console.log(isInternal("someInternalHelper")); // true
// Filter out internal helpers
const publicHelpers = list.filter(name => !isInternal(name));
console.log(publicHelpers); // Only non-internal helpersArray of all available helper names.
/**
* Array of all available helper names (with leading underscores removed)
*/
const list: string[];Usage Examples:
import { list, isInternal, minVersion } from "@babel/helpers";
// Get all available helpers
console.log(list.length); // 122 helpers
console.log(list.slice(0, 5)); // ["OverloadYield", "applyDecoratedDescriptor", ...]
// Generate helper catalog
const helperCatalog = list.map(name => ({
name,
minVersion: minVersion(name),
isInternal: isInternal(name),
dependencies: getDependencies(name)
}));
// Find helpers by pattern
const classHelpers = list.filter(name => name.includes("class"));
console.log(classHelpers); // All class-related helpersPreload a helper to cache it (legacy CommonJS export).
/**
* Ensure a helper is loaded and cached (CommonJS builds only)
* @param name - Name of the helper to preload
* @deprecated Only available in non-ES module builds for legacy compatibility
*/
function ensure(name: string): void;Usage Examples:
// Only available in CommonJS builds (not ES modules)
const helpers = require("@babel/helpers");
// Preload a helper to cache it
helpers.ensure("asyncToGenerator");
// Helper is now cached and ready for fast retrieval
// This is equivalent to calling get() but doesn't return the result
// Mainly used for performance optimization in build tools/**
* Function type for resolving helper dependencies
* @param name - Name of the dependency helper
* @returns AST expression representing the dependency
*/
type GetDependency = (name: string) => t.Expression;
/**
* Function type for adjusting helper ASTs after processing
* @param ast - The helper AST program
* @param exportName - Name of the exported function
* @param mapExportBindingAssignments - Function to map export binding assignments
*/
type AdjustAst = (
ast: t.Program,
exportName: string,
mapExportBindingAssignments: (
map: (node: t.Expression) => t.Expression,
) => void,
) => void;
/**
* Return type for the get() function
*/
interface HelperResult {
/** Array of AST nodes that make up the helper */
nodes: t.Program["body"];
/** Array of global variables used by the helper */
globals: string[];
}