Babel plugin that compiles exponentiation operator (**) to ES5-compatible Math.pow() calls
npx @tessl/cli install tessl/npm-babel-plugin-transform-exponentiation-operator@6.24.0babel-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.
npm install --save-dev babel-plugin-transform-exponentiation-operatorThis 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.jsThe 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);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
};
}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;
}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[];
}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]
);
}This plugin depends on:
The plugin performs the following transformations:
Binary Exponentiation (a ** b):
** operatorMath.pow(a, b) function callAssignment Exponentiation (a **= b):
**= operatora = Math.pow(a, b) assignmentType Compatibility:
** operator are preservedThe plugin inherits error handling from Babel's parser and transformation pipeline. Invalid syntax or AST nodes will result in standard Babel compilation errors.