or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-syntax-throw-expressions

Babel syntax plugin that enables parsing of throw expressions in JavaScript and TypeScript

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-syntax-throw-expressions@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-syntax-throw-expressions@7.27.0

index.mddocs/

@babel/plugin-syntax-throw-expressions

@babel/plugin-syntax-throw-expressions is a Babel syntax plugin that enables parsing of throw expressions in JavaScript and TypeScript. It allows throw statements to be used as expressions rather than just statements, enabling patterns like conditional throws and inline error handling.

Package Information

  • Package Name: @babel/plugin-syntax-throw-expressions
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-syntax-throw-expressions

Core Imports

The plugin is used through Babel configuration and does not export any runtime APIs for direct import. However, for programmatic usage, you can import the plugin function:

import syntaxThrowExpressions from "@babel/plugin-syntax-throw-expressions";

For CommonJS:

const syntaxThrowExpressions = require("@babel/plugin-syntax-throw-expressions");

Configuration usage:

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

Basic Usage

Once installed and configured, the plugin enables parsing of throw expressions syntax:

// Conditional throw expressions
const result = condition ? value : throw new Error('Invalid condition');

// Throw expressions in logical operators
const data = input || throw new Error('Input required');

// Throw expressions in function parameters
function process(data = throw new Error('Data parameter required')) {
  return data.toUpperCase();
}

// Arrow functions with throw expressions
const validate = (x) => x > 0 ? x : throw new Error('Must be positive');

Note: This plugin only enables parsing of throw expressions. To actually transform the syntax for environments that don't support it, you'll need a corresponding transform plugin.

Architecture

This is a minimal Babel syntax plugin that:

  • Extends the Babel parser to recognize throw expressions
  • Adds the throwExpressions parser plugin to Babel's parser options
  • Does not perform any code transformations
  • Integrates with Babel's plugin architecture through @babel/helper-plugin-utils

Capabilities

Plugin Function

The main and only export of this package is a Babel plugin function created using the declare helper from @babel/helper-plugin-utils.

/**
 * Babel syntax plugin that enables parsing of throw expressions
 * Uses @babel/helper-plugin-utils declare function for plugin creation
 */
export default declare((api: PluginAPI) => PluginObject);

/**
 * The declare function from @babel/helper-plugin-utils
 * @param builder - Function that receives API and returns plugin object
 * @returns Babel plugin function
 */
function declare<State = object, Option = object>(
  builder: (api: PluginAPI, options: Option, dirname: string) => PluginObject<State>
): (api: PluginAPI, options: Option, dirname: string) => PluginObject<State>;

/**
 * REQUIRED_VERSION macro for asserting Babel version compatibility
 * @param version - Minimum required major version number
 * @returns Version range string for api.assertVersion
 */
function REQUIRED_VERSION(version: number): number | string;
function REQUIRED_VERSION(version: string): string;

The plugin implementation:

  • Uses api.assertVersion(REQUIRED_VERSION(7)) to validate Babel 7+ compatibility
  • Returns a plugin object with name "syntax-throw-expressions"
  • Implements manipulateOptions to add "throwExpressions" to parser plugins
  • Created via the declare helper for consistent plugin architecture

Babel Configuration Usage:

// babel.config.js - Object configuration
module.exports = {
  plugins: ["@babel/plugin-syntax-throw-expressions"]
};

// babel.config.js - With options (none available for syntax plugins)
module.exports = {
  plugins: [
    ["@babel/plugin-syntax-throw-expressions"]
  ]
};

// .babelrc.json
{
  "plugins": ["@babel/plugin-syntax-throw-expressions"]
}

Programmatic Usage:

const babel = require('@babel/core');
const syntaxThrowExpressions = require('@babel/plugin-syntax-throw-expressions');

// The plugin is a function that needs to be called by Babel
const result = babel.transformSync(code, {
  plugins: [syntaxThrowExpressions]
});

TypeScript Usage:

import { transformSync } from '@babel/core';
import syntaxThrowExpressions from '@babel/plugin-syntax-throw-expressions';

const result = transformSync(code, {
  plugins: [syntaxThrowExpressions]
});

Types

/**
 * Babel Plugin API interface (from @babel/core)
 */
interface PluginAPI {
  /** Babel version string */
  version: string;
  /** Assert minimum Babel version requirement */
  assertVersion(range: number | string): void;
  /** Additional API methods available in Babel 7+ */
  [key: string]: any;
}

/**
 * Babel Plugin Object interface (from @babel/core)
 */
interface PluginObject<State = object> {
  /** Plugin identifier name */
  name: string;
  /** Function to manipulate parser options before parsing */
  manipulateOptions?(opts: any, parserOpts: ParserOptions): void;
  /** Pre/post visitor methods (not used in syntax plugins) */
  visitor?: any;
}

/**
 * Babel Parser Options interface
 */
interface ParserOptions {
  /** Array of parser plugin names to enable */
  plugins: string[];
  /** Additional parser options */
  [key: string]: any;
}

/**
 * Plugin builder function type used with declare
 */
type PluginBuilder<State = object, Option = object> = (
  api: PluginAPI,
  options: Option,
  dirname: string
) => PluginObject<State>;