or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

babel-plugin-transform-exponentiation-operator

babel-plugin-transform-exponentiation-operator is a Babel plugin that transforms JavaScript's exponentiation operator (**) and exponentiation assignment operator (**=) into equivalent ES5-compatible code using Math.pow() function calls. This plugin enables developers to use modern ES2016 exponentiation syntax while maintaining compatibility with older JavaScript environments.

Package Information

  • Package Name: babel-plugin-transform-exponentiation-operator
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-exponentiation-operator

Core Imports

This plugin is used through Babel's configuration system and is not directly imported in application code:

.babelrc configuration:

{
  "plugins": ["transform-exponentiation-operator"]
}

Node.js Babel API:

require("babel-core").transform("code", {
  plugins: ["transform-exponentiation-operator"]
});

CLI usage:

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

Basic Usage

The plugin automatically transforms exponentiation operators during the Babel compilation process:

Input JavaScript (ES2016+):

// Binary exponentiation
let squared = 2 ** 2;
let cubed = 2 ** 3;

// Assignment exponentiation  
let a = 2;
a **= 2;

let b = 3;
b **= 3;

Output JavaScript (ES5):

// Binary exponentiation
let squared = Math.pow(2, 2);
let cubed = Math.pow(2, 3);

// Assignment exponentiation
let a = 2;
a = Math.pow(a, 2);

let b = 3;  
b = Math.pow(b, 3);

Capabilities

Plugin Factory Function

The main export that creates the Babel plugin configuration.

/**
 * Creates a Babel plugin that transforms exponentiation operators
 * @param {Object} babel - Babel instance with helper utilities
 * @param {Object} babel.types - Babel types helper for AST manipulation
 * @returns {Object} Babel plugin configuration object
 */
function default({ types: t }) {
  return {
    inherits: require("babel-plugin-syntax-exponentiation-operator"),
    visitor: VisitorObject
  };
}

Plugin Configuration Object

The object structure returned by the plugin factory function.

interface BabelPlugin {
  /** Inherits syntax parsing from babel-plugin-syntax-exponentiation-operator */
  inherits: BabelPlugin;
  /** AST visitor object built using babel-helper-builder-binary-assignment-operator-visitor */
  visitor: Object;
}

Binary Assignment Operator Visitor

The visitor is built using babel-helper-builder-binary-assignment-operator-visitor which creates handlers for both binary and assignment expressions.

/**
 * Configuration object passed to the binary assignment operator visitor builder
 */
interface VisitorConfig {
  /** The operator to transform ("**") */
  operator: string;
  /** Function to build the replacement AST node */
  build: (left: Node, right: Node) => CallExpression;
}

/**
 * Babel AST Node types used in the plugin
 */
interface Node {
  /** AST node type */
  type: string;
}

interface CallExpression extends Node {
  /** Function being called */
  callee: Node;
  /** Arguments passed to the function */
  arguments: Node[];
}

Transform Build Function

Internal function used by the binary assignment operator visitor to create Math.pow() calls.

/**
 * Transforms exponentiation expressions into Math.pow() function calls
 * @param {Node} left - Left operand AST node
 * @param {Node} right - Right operand AST node  
 * @returns {CallExpression} Math.pow(left, right) AST node
 */
function build(left, right) {
  return t.callExpression(
    t.memberExpression(t.identifier("Math"), t.identifier("pow")),
    [left, right]
  );
}

Dependencies

This plugin depends on:

  • babel-plugin-syntax-exponentiation-operator: Provides parser support for exponentiation operator syntax
  • babel-helper-builder-binary-assignment-operator-visitor: Utility for building binary assignment operator transformations
  • babel-runtime: Babel runtime helpers for ES6+ features

Transformation Details

The plugin performs the following transformations:

Binary Exponentiation (a ** b):

  • Detects binary expressions with the ** operator
  • Transforms to Math.pow(a, b) function call
  • Preserves operator precedence and associativity

Assignment Exponentiation (a **= b):

  • Detects assignment expressions with the **= operator
  • Transforms to a = Math.pow(a, b) assignment
  • Handles complex left-hand side expressions properly

Type Compatibility:

  • All operand types supported by the original ** operator are preserved
  • Maintains JavaScript's native type coercion behavior through Math.pow()
  • Handles edge cases like negative bases and fractional exponents

Error Handling

The plugin inherits error handling from Babel's parser and transformation pipeline. Invalid syntax or AST nodes will result in standard Babel compilation errors.