Babel Helper: Binary Assignment Operator Visitor Builder

raw JSON →
6.24.1 verified Sat Apr 25 auth: no javascript

This package, `babel-helper-builder-binary-assignment-operator-visitor`, is a low-level utility within the Babel ecosystem, specifically designed to assist plugin authors in creating AST (Abstract Syntax Tree) visitors for JavaScript's binary and assignment operators. It abstracts the common patterns required to transform expressions like `a += b` or `x &&= y` consistently across different operator types. The provided version `6.24.1` indicates it's from the Babel 6 era, which is now considered legacy. The current stable major version of Babel is `v7.x`, with `v7.29.2` being a very recent release, and `v8.x` is in release candidate (`v8.0.0-rc.3`). Babel generally maintains a rapid release cadence for patch and minor versions, with major versions introducing breaking changes every few years. Key differentiators include its specialized focus on a specific AST transformation pattern, reducing boilerplate for plugin developers who need to handle these operators, and its tight integration within the larger Babel monorepo for consistent AST manipulation.

error Error: Cannot find module 'babel-helper-builder-binary-assignment-operator-visitor'
cause Attempting to use the Babel 6 package name with a Babel 7+ setup, or the package is simply not installed.
fix
Install the correct scoped package: npm install @babel/helper-builder-binary-assignment-operator-visitor or yarn add @babel/helper-builder-binary-assignment-operator-visitor. Also, update all import paths in your code to @babel/helper-builder-binary-assignment-operator-visitor.
error TypeError: Cannot destructure property 'buildBinaryAssignmentOperatorVisitor' of 'require(...)' as it is undefined.
cause Trying to import a Babel 7+ scoped package using a CommonJS `require()` call, but the export structure is not correctly handled (e.g., trying a default import on a named export).
fix
Ensure you are using named destructuring for require(): const { buildBinaryAssignmentOperatorVisitor } = require('@babel/helper-builder-binary-assignment-operator-visitor'); or switch to import statements if using ESM.
error Requires Babel "^7.0.0-0", but was loaded with "6.x.x"
cause A Babel plugin or helper (like this one) built for Babel 7+ is being used in an environment where Babel 6 is still loaded or specified, causing version conflicts.
fix
Completely upgrade your project's Babel dependencies to version 7 or 8. This often involves updating @babel/core, @babel/cli, presets, and plugins to their scoped Babel 7/8 versions, and removing any old babel- prefixed packages. Refer to the Babel 7 and Babel 8 migration guides.
breaking Major version upgrade from Babel 6 to Babel 7 (and subsequently Babel 8) introduces a significant breaking change in package naming. `babel-helper-builder-binary-assignment-operator-visitor` was renamed to `@babel/helper-builder-binary-assignment-operator-visitor` and moved to a monorepo structure. Any project using the old unscoped package will fail with module not found errors when upgrading to Babel 7+ without updating imports.
fix Update `package.json` dependencies to `@babel/helper-builder-binary-assignment-operator-visitor` and adjust all `require()` or `import` statements to use the new scoped name.
breaking Babel 8 (currently in Release Candidate) introduces a fundamental shift to ES Modules (ESM) only. While Babel 7 largely supports both CommonJS (CJS) and ESM for plugins, Babel 8 will ship as an ESM-only package. This means plugins (and by extension, helpers) built for Babel 8 will need to be written as ESM and consumed accordingly.
fix Ensure your Babel plugins and consuming code are configured for ESM when targeting Babel 8. This may involve changes to `package.json` (`"type": "module"`) and using `import/export` syntax exclusively.
gotcha This helper is part of Babel's internal API. While exposed, it's primarily intended for core Babel plugins or highly advanced transformations. Direct usage for simple transformations might be overkill, and its API can change more frequently than public-facing Babel plugins.
fix Before using low-level helpers, consider if a higher-level Babel plugin or preset could achieve the desired transformation with less direct AST manipulation.
breaking Node.js version compatibility changes with Babel major versions. Babel 7 dropped support for Node.js 0.10, 0.12, 4, and 5. Future Babel 8 releases will likely require even newer Node.js versions.
fix Ensure your development and production environments use a supported Node.js LTS version (e.g., Node.js 14+ for Babel 7, and Node.js 20+ for Babel 8).
npm install babel-helper-builder-binary-assignment-operator-visitor
yarn add babel-helper-builder-binary-assignment-operator-visitor
pnpm add babel-helper-builder-binary-assignment-operator-visitor

This quickstart shows how to use `buildBinaryAssignmentOperatorVisitor` within a Babel plugin to create a specialized visitor for handling binary and assignment operators. It demonstrates a basic transformation of `+=` assignment expressions.

import { declare } from '@babel/helper-plugin-utils';
import { buildBinaryAssignmentOperatorVisitor } from '@babel/helper-builder-binary-assignment-operator-visitor';

export default declare((api, options) => {
  api.assertVersion(7);

  // This helper builds a visitor that handles binary and assignment operators.
  // It can simplify transformations for operators like `+=`, `-=`, `&&=`, `??=` etc.
  // The helper typically expects a 'kind' (binary or assignment) and a map of operators
  // to visitor methods. Since the README is a TODO, this is an illustrative example
  // based on common Babel plugin patterns.

  // Example: A plugin to replace all `+=` operators with a function call.
  // This specific helper might be overkill for just one operator, but shows the pattern.
  // In a real scenario, you'd define methods for multiple operators.

  const visitor = buildBinaryAssignmentOperatorVisitor({
    operator(path) {
      // `path` is a NodePath for the operator expression (e.g., BinaryExpression, AssignmentExpression).
      const { node } = path;
      if (node.operator === '+=' && node.type === 'AssignmentExpression') {
        // Transform 'a += b' into 'assignAndAdd(a, b)'
        path.replaceWith(
          api.types.callExpression(
            api.types.identifier('assignAndAdd'),
            [node.left, node.right]
          )
        );
      }
    },
    // More specific operator handlers could be defined here
    // '+=_assignment'(path) { /* ... */ },
    // '+_binary'(path) { /* ... */ }
  });

  return {
    name: 'transform-binary-assignment-operators',
    visitor: {
      // The visitor is returned directly from the helper.
      // It usually contains handlers for 'BinaryExpression' and 'AssignmentExpression'.
      ...visitor
    },
  };
});