or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Babel Helper Builder Binary Assignment Operator Visitor

Babel 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.

Package Information

  • Package Name: babel-helper-builder-binary-assignment-operator-visitor
  • Package Type: npm
  • Language: JavaScript (ES6 modules)
  • Installation: npm install babel-helper-builder-binary-assignment-operator-visitor

Core Imports

import builderFunction from "babel-helper-builder-binary-assignment-operator-visitor";

For CommonJS:

const builderFunction = require("babel-helper-builder-binary-assignment-operator-visitor");

Basic Usage

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
  };
}

Architecture

The package provides a factory function that generates specialized visitor objects for Babel's AST traversal system. It integrates with:

  • babel-helper-explode-assignable-expression: For safely handling complex assignable expressions
  • babel-types: For AST node construction and manipulation
  • Babel Plugin System: As a utility for plugin developers

The generated visitors handle three types of AST nodes:

  • ExpressionStatement: Binary assignments in statement position
  • AssignmentExpression: Binary assignments within expressions
  • BinaryExpression: Binary operations that match the specified operator

Capabilities

Visitor Factory Function

Creates 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;
}

Options Configuration

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);
  }
});

Visitor Methods

The generated visitor object contains three methods that handle different contexts where binary assignment operators can appear.

ExpressionStatement Visitor

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.

AssignmentExpression Visitor

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.

BinaryExpression Visitor

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.

Types

// 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
}