Babel helper for ensuring that access to a given value is performed through simple accesses
npx @tessl/cli install tessl/npm-babel--helper-simple-access@7.27.0Babel helper for ensuring that access to a given value is performed through simple accesses. This utility transforms complex assignment and update expressions (like +=, ++, etc.) into simpler basic assignment expressions, which is crucial for maintaining correct semantics when code is transpiled or transformed by Babel plugins.
npm install @babel/helper-simple-accessimport simplifyAccess from "@babel/helper-simple-access";For CommonJS:
const simplifyAccess = require("@babel/helper-simple-access");import simplifyAccess from "@babel/helper-simple-access";
import type { NodePath } from "@babel/traverse";
// Within a Babel plugin visitor
export default function myBabelPlugin() {
return {
visitor: {
Program(path: NodePath) {
// Simplify access to variables 'foo' and 'bar'
const bindingNames = new Set(['foo', 'bar']);
simplifyAccess(path, bindingNames);
}
}
};
}Transforms complex assignment and update expressions into simpler forms to ensure predictable variable access patterns in transformed code.
/**
* Transforms complex assignment and update expressions into simpler forms
* @param path - The AST node path to traverse and transform
* @param bindingNames - Set of variable names that should be simplified
* @param includeUpdateExpression - (Babel 7 only) Whether to transform update expressions (++, --). Accessed via arguments[2], defaults to true in Babel 7
*/
export default function simplifyAccess(
path: NodePath,
bindingNames: Set<string>
): void;
// Note: In Babel 7, a third parameter can be passed but is not part of the TypeScript signature
// Usage: simplifyAccess(path, bindingNames, includeUpdateExpression)Key Features:
+=, -=, *=, etc.) into basic assignments&&=, ||=, ??=) into conditional expressions++, --) into assignment expressionsbindingNames set and respects scope boundariesAssignment Expression Transformations:
// Compound assignment operators
foo += bar → foo = foo + bar
foo -= bar → foo = foo - bar
foo *= bar → foo = foo * bar
foo /= bar → foo = foo / bar
// Logical assignment operators
foo &&= bar → foo && (foo = bar)
foo ||= bar → foo || (foo = bar)
foo ??= bar → foo ?? (foo = bar)Update Expression Transformations (Babel 7 only):
// Prefix operations
++foo → foo = (+foo) + 1
--foo → foo = (+foo) - 1
// Postfix operations (when used as expression)
foo++ → (_old = (+foo), foo = _old + 1, _old)
foo-- → (_old = (+foo), foo = _old - 1, _old)
// Simple statements
foo++; → foo += 1;
++foo; → foo += 1;import type { NodePath } from "@babel/traverse";
// The function uses these imported types from @babel/traverse
// No additional types are exported by this packagebindingNames set+=, -=, etc.). Update expressions (++, --) are not transformedincludeUpdateExpression controls update expression transformation (defaults to true)WeakSet to track already-processed nodes and prevent duplicate transformationsThis helper is typically used within Babel transformation plugins when:
Example Plugin Usage:
export default function trackExportsPlugin() {
return {
visitor: {
Program(path) {
// Get all exported variable names
const exportedNames = getExportedNames(path);
// Simplify access to these variables
simplifyAccess(path, exportedNames);
// Now add export tracking after simplification
addExportTracking(path, exportedNames);
}
}
};
}