Babel Helper: Binary Assignment Operator Visitor Builder
raw JSON →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.
Common errors
error Error: Cannot find module 'babel-helper-builder-binary-assignment-operator-visitor' ↓
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. ↓
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" ↓
@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. Warnings
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. ↓
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. ↓
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. ↓
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. ↓
Install
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 Imports
- buildBinaryAssignmentOperatorVisitor wrong
const buildBinaryAssignmentOperatorVisitor = require('babel-helper-builder-binary-assignment-operator-visitor');correctimport { buildBinaryAssignmentOperatorVisitor } from '@babel/helper-builder-binary-assignment-operator-visitor'; - buildBinaryAssignmentOperatorVisitor wrong
import buildBinaryAssignmentOperatorVisitor from '@babel/helper-builder-binary-assignment-operator-visitor';correctconst { buildBinaryAssignmentOperatorVisitor } = require('@babel/helper-builder-binary-assignment-operator-visitor'); - NodePath
import { NodePath } from '@babel/traverse';
Quickstart
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
},
};
});