Helper function to build binary assignment operator visitors for Babel transformations
npx @tessl/cli install tessl/npm-babel-helper-builder-binary-assignment-operator-visitor@6.24.0Babel Helper Builder Binary Assignment Operator Visitor is a utility function for building AST (Abstract Syntax Tree) visitors that handle binary assignment operators in Babel transformations. It creates visitors that can transform expressions like a += b, a *= b, etc. by decomposing complex left-hand side expressions and rebuilding them as standard assignment expressions.
npm install babel-helper-builder-binary-assignment-operator-visitorimport builderFunction from "babel-helper-builder-binary-assignment-operator-visitor";For CommonJS:
const builderFunction = require("babel-helper-builder-binary-assignment-operator-visitor");import builderFunction from "babel-helper-builder-binary-assignment-operator-visitor";
import * as t from "babel-types";
// Create a visitor for += operator
const visitor = builderFunction({
operator: "+",
build: function(left, right) {
// Return the binary expression that replaces the assignment
// For a += b, this would typically return a + b
return t.binaryExpression("+", left, right);
}
});
// Use the visitor in a Babel plugin
export default function myPlugin() {
return {
visitor: visitor
};
}The package provides a factory function that generates specialized visitor objects for Babel's AST traversal system. It integrates with:
The generated visitors handle three types of AST nodes:
ExpressionStatement: Binary assignments in statement positionAssignmentExpression: Binary assignments within expressionsBinaryExpression: Binary operations that match the specified operatorCreates a visitor object with methods for transforming binary assignment operators in AST nodes.
/**
* Creates a visitor object for transforming binary assignment operators
* @param opts - Configuration object with operator and build function
* @returns Visitor object with ExpressionStatement, AssignmentExpression, and BinaryExpression methods
*/
function builderFunction(opts: {
build: (left: Node, right: Node) => Node;
operator: string;
}): VisitorObject;
interface VisitorObject {
/** Handles binary assignment operators within expression statements */
ExpressionStatement: (path: NodePath, file: File) => void;
/** Handles binary assignment expressions that are part of larger expressions */
AssignmentExpression: (path: NodePath, file: File) => void;
/** Handles binary expressions that match the specified operator */
BinaryExpression: (path: NodePath) => void;
}The factory function accepts a configuration object with two required properties.
interface BuilderOptions {
/** Binary operator to transform (e.g., "+", "-", "*", "/", "%", "**", etc.) */
operator: string;
/** Function that builds the replacement expression from left and right operands */
build: (left: Node, right: Node) => Node;
}Usage Examples:
import builderFunction from "babel-helper-builder-binary-assignment-operator-visitor";
import * as t from "babel-types";
// Example 1: Addition assignment (+=)
const additionVisitor = builderFunction({
operator: "+",
build: function(left, right) {
return t.binaryExpression("+", left, right);
}
});
// Example 2: Multiplication assignment (*=)
const multiplicationVisitor = builderFunction({
operator: "*",
build: function(left, right) {
return t.binaryExpression("*", left, right);
}
});
// Example 3: Exponentiation assignment (**=)
const exponentiationVisitor = builderFunction({
operator: "**",
build: function(left, right) {
return t.binaryExpression("**", left, right);
}
});The generated visitor object contains three methods that handle different contexts where binary assignment operators can appear.
Handles binary assignment operators within expression statements (standalone statements).
/**
* Handles binary assignment operators within expression statements
* @param path - Babel NodePath for the ExpressionStatement
* @param file - Babel File object containing scope and metadata
*/
ExpressionStatement: (path: NodePath, file: File) => void;Transforms expressions like a += b; where the assignment is the entire statement.
Handles binary assignment expressions that are part of larger expressions.
/**
* Handles binary assignment expressions within larger expressions
* @param path - Babel NodePath for the AssignmentExpression
* @param file - Babel File object containing scope and metadata
*/
AssignmentExpression: (path: NodePath, file: File) => void;Transforms expressions like x = (a += b) where the assignment is part of a larger expression.
Handles binary expressions that match the specified operator (for direct binary operations).
/**
* Handles binary expressions that match the specified operator
* @param path - Babel NodePath for the BinaryExpression
*/
BinaryExpression: (path: NodePath) => void;Transforms expressions like a + b when the operator matches the configured operator.
// Core types used by the visitor system
interface NodePath {
node: Node;
scope: Scope;
isCompletionRecord(): boolean;
replaceWith(node: Node): void;
replaceWithMultiple(nodes: Node[]): void;
}
interface File {
// Babel file context containing scope and transformation metadata
}
interface Node {
// Base AST node type from babel-types
}
interface Scope {
// Babel scope object for variable tracking
}