CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--helper-simple-access

Babel helper for ensuring that access to a given value is performed through simple accesses

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Babel Helper Simple Access

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

Package Information

  • Package Name: @babel/helper-simple-access
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @babel/helper-simple-access

Core Imports

import simplifyAccess from "@babel/helper-simple-access";

For CommonJS:

const simplifyAccess = require("@babel/helper-simple-access");

Basic Usage

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

Capabilities

Simple Access Transformation

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:

  • Assignment Expression Simplification: Converts compound assignments (+=, -=, *=, etc.) into basic assignments
  • Logical Assignment Handling: Transforms logical assignments (&&=, ||=, ??=) into conditional expressions
  • Update Expression Transformation (Babel 7 only): Converts increment/decrement operators (++, --) into assignment expressions
  • Scope-Aware: Only transforms variables in the provided bindingNames set and respects scope boundaries
  • Duplicate Prevention: Uses internal tracking to prevent multiple transformations of the same node

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

Types

import type { NodePath } from "@babel/traverse";

// The function uses these imported types from @babel/traverse
// No additional types are exported by this package

Behavior Notes

Scope Handling

  • Only transforms variables that exist in the provided bindingNames set
  • Respects scope boundaries - won't transform variables that have been redeclared in inner scopes
  • Compares binding references to ensure correct scope resolution

Version Differences

  • Babel 8: Only handles assignment expressions (+=, -=, etc.). Update expressions (++, --) are not transformed
  • Babel 7: Handles both assignment and update expressions. The third parameter includeUpdateExpression controls update expression transformation (defaults to true)

Error Prevention

  • Uses a WeakSet to track already-processed nodes and prevent duplicate transformations
  • Validates node types before transformation to ensure safe operations
  • Generates unique temporary variable names when needed for postfix operations

Common Use Cases

This helper is typically used within Babel transformation plugins when:

  1. Variable Export Tracking: Ensuring that all modifications to exported variables are captured
  2. Code Instrumentation: Simplifying expressions before adding instrumentation code
  3. Optimization Passes: Converting complex expressions to simpler forms for easier analysis
  4. Transpilation: Maintaining semantic correctness when targeting older JavaScript environments

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);
      }
    }
  };
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-simple-access@7.27.x
Publish Source
CLI
Badge
tessl/npm-babel--helper-simple-access badge