or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

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

A Babel syntax plugin that enables parsing of do expressions in JavaScript code

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

To install, run

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

index.mddocs/

@babel/plugin-syntax-do-expressions

@babel/plugin-syntax-do-expressions is a Babel syntax plugin that enables parsing of do expressions in JavaScript code. This is a minimal syntax-only plugin that does not transform code - it only enables the Babel parser to recognize and parse do expression syntax.

Do expressions are a proposed JavaScript language feature that allow using block statements as expressions, where the value of the last statement in the block becomes the value of the expression.

Package Information

  • Package Name: @babel/plugin-syntax-do-expressions
  • Package Type: npm (Babel plugin)
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-syntax-do-expressions

Core Imports

// ES modules (default export)
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";
// CommonJS
const syntaxDoExpressions = require("@babel/plugin-syntax-do-expressions");

Basic Usage

This plugin is typically used in Babel configuration files rather than being imported directly in application code:

{
  "plugins": ["@babel/plugin-syntax-do-expressions"]
}

With programmatic Babel API:

import { transform } from "@babel/core";
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";

const result = transform(code, {
  plugins: [syntaxDoExpressions]
});

Capabilities

Plugin Function

The main export is a Babel plugin function created by the declare() helper from @babel/helper-plugin-utils.

/**
 * Default export - Babel plugin function for syntax-do-expressions
 * This is the result of calling declare() from @babel/helper-plugin-utils
 * @param api - Babel plugin API providing version assertion and utilities
 * @param options - Plugin options (not used by this plugin)
 * @param dirname - Directory name (not used by this plugin)
 * @returns Babel plugin configuration object
 */
declare const syntaxDoExpressions: (
  api: PluginAPI,
  options?: object,
  dirname?: string
) => BabelPlugin;

export default syntaxDoExpressions;

interface PluginAPI {
  assertVersion(range: number | string): void;
  // Additional Babel API methods available but not used by this plugin
}

interface BabelPlugin {
  name: string;
  manipulateOptions(opts: any, parserOpts: ParserOptions): void;
}

interface ParserOptions {
  plugins: string[];
  // Additional parser options available
}

The plugin object returned contains:

  • name: "syntax-do-expressions" - Plugin identifier
  • manipulateOptions: Function that adds "doExpressions" to the parser plugins array

Plugin Lifecycle Method

The plugin's manipulateOptions method is called during Babel's parsing phase.

/**
 * Babel plugin lifecycle method that modifies parser options
 * @param opts - Babel transformation options (unused by this plugin)
 * @param parserOpts - Parser configuration object to modify
 */
manipulateOptions(opts: any, parserOpts: ParserOptions): void;

This method adds the "doExpressions" string to the parserOpts.plugins array, enabling the Babel parser to recognize do expression syntax.

What Do Expressions Enable

Once this plugin is enabled, the Babel parser can recognize do expression syntax like:

// Do expression syntax that becomes parseable
const value = do {
  if (condition) {
    "success";
  } else {
    "failure";
  }
};

const result = do {
  const temp = compute();
  temp * 2;
};

Integration Notes

  • Peer Dependency: Requires @babel/core version ^7.0.0-0
  • Runtime Dependency: Uses @babel/helper-plugin-utils for plugin creation utilities
  • Node.js Support: Requires Node.js >=6.9.0
  • Module Format: Distributed as ES modules with CommonJS compatibility
  • TypeScript: Full TypeScript support with type definitions included

Error Handling

This plugin does not throw custom errors. Any errors that occur are typically:

  • Version assertion errors from @babel/core compatibility checks
  • Parser errors if do expression syntax is malformed (handled by Babel parser)

Usage in Build Pipelines

Commonly used with other Babel plugins and presets:

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    "@babel/plugin-syntax-do-expressions",
    "@babel/plugin-proposal-do-expressions"
  ]
}

Note: This syntax plugin only enables parsing. To actually transform do expressions for older JavaScript environments, use @babel/plugin-proposal-do-expressions alongside this plugin.