or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-syntax-exponentiation-operator

A Babel syntax plugin that enables parsing of the JavaScript exponentiation operator (**)

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

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-syntax-exponentiation-operator@6.13.0

index.mddocs/

Babel Plugin Syntax Exponentiation Operator

A Babel syntax plugin that enables parsing of the JavaScript exponentiation operator (**). This plugin extends Babel's parser to recognize exponentiation expressions without transforming them, serving as a prerequisite for other transformations or analysis tools that need to process exponentiation syntax.

Package Information

  • Package Name: babel-plugin-syntax-exponentiation-operator
  • Package Type: npm
  • Language: JavaScript (ES6 modules)
  • Installation: npm install babel-plugin-syntax-exponentiation-operator

Core Imports

// ES6 module import
import syntaxExponentiationOperator from "babel-plugin-syntax-exponentiation-operator";

For CommonJS:

const syntaxExponentiationOperator = require("babel-plugin-syntax-exponentiation-operator");

Basic Usage

This plugin is designed to be used through Babel's standard configuration methods:

// .babelrc
{
  "plugins": ["syntax-exponentiation-operator"]
}

CLI usage:

babel --plugins syntax-exponentiation-operator script.js

Programmatic usage:

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

const result = babel.transform("const result = 2 ** 3;", {
  plugins: ["syntax-exponentiation-operator"]
});

Architecture

This is a minimal syntax plugin that operates during Babel's parsing phase:

  • Plugin Type: Syntax extension (parser configuration only)
  • Transformation: None (syntax recognition only)
  • Parser Integration: Adds "exponentiationOperator" to parser plugins
  • Hook: Uses manipulateOptions to modify parser configuration before parsing begins

The plugin enables Babel to parse expressions like 2 ** 3 or base ** exponent without syntax errors, making the parsed AST available to other plugins for further processing.

Capabilities

Plugin Factory Function

The main exported function that creates a Babel plugin instance.

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

interface BabelPlugin {
  manipulateOptions(opts: Object, parserOpts: ParserOptions): void;
}

interface ParserOptions {
  plugins: string[];
}

Usage Example:

import syntaxExponentiationOperator from "babel-plugin-syntax-exponentiation-operator";

// The plugin factory returns a plugin object
const plugin = syntaxExponentiationOperator();

// Plugin object contains manipulateOptions method
// This is called automatically by Babel during configuration

Parser Configuration Method

The manipulateOptions method that configures Babel's parser to recognize exponentiation syntax.

/**
 * Babel plugin hook that modifies parser options to enable exponentiation operator parsing
 * @param {Object} opts - Babel transformation options (unused by this plugin)
 * @param {ParserOptions} parserOpts - Babel parser options object that gets modified
 */
manipulateOptions(opts: Object, parserOpts: ParserOptions): void;

Behavior:

  • Adds "exponentiationOperator" string to the parserOpts.plugins array
  • Enables Babel parser to recognize ** exponentiation operator syntax (e.g., 2 ** 3)
  • Does not transform the syntax - leaves exponentiation expressions as-is in the AST
  • Runs before parsing begins, during Babel's configuration phase

Integration Points:

  • Compatible with Babel 6.x plugin system
  • Works with all standard Babel configuration methods (.babelrc, CLI, programmatic API)
  • Can be combined with other Babel plugins that process or transform exponentiation expressions
  • Suitable for JavaScript environments supporting ES2016+ exponentiation operator

Error Handling

This plugin does not implement explicit error handling as it only modifies parser configuration. Error handling is delegated to Babel's core parser system. If the exponentiation operator is used in an environment that doesn't support it, the error will be reported by the JavaScript runtime, not by this plugin.

Supported Syntax

Once this plugin is enabled, Babel can parse the following exponentiation expressions:

// Basic exponentiation
const result = 2 ** 3; // 8

// With variables
const base = 5;
const exponent = 2;
const power = base ** exponent; // 25

// In expressions
const complex = (2 ** 3) * (4 ** 2); // 8 * 16 = 128

// Chained exponentiation (right-associative)
const chained = 2 ** 3 ** 2; // 2 ** (3 ** 2) = 2 ** 9 = 512

Note: This plugin only enables parsing - it does not transform the syntax for older JavaScript environments. For transformation, use babel-plugin-transform-exponentiation-operator alongside this plugin.