or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-check-constants

Historical Babel plugin that validated ES2015 const variable reassignments at compile time

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-check-constants@7.0.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-check-constants@7.0.0

index.mddocs/

@babel/plugin-check-constants

@babel/plugin-check-constants is a historical Babel plugin that validated ES2015 const variable reassignments at compile time. The plugin detected attempts to reassign const variables and transformed them into runtime errors, providing early detection of const violations before native const support was widespread.

⚠️ Deprecated: This plugin was removed in Babel v7.0.0-beta.38 (January 2018). Its functionality was merged into @babel/plugin-transform-block-scoping for better integration and performance. Use the block-scoping transform for modern const validation needs.

Package Information

  • Package Name: @babel/plugin-check-constants
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install --save-dev @babel/plugin-check-constants (historical)
  • Last Version: 7.0.0-beta.38
  • Status: Deprecated/Removed (2018-01-26)
  • Replacement: @babel/plugin-transform-block-scoping

Core Imports

// CommonJS
const checkConstants = require("@babel/plugin-check-constants");

// ES6 (as default export)
import checkConstants from "@babel/plugin-check-constants";

Basic Usage

The plugin was used as a standard Babel plugin through configuration:

// Via Node API
require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-check-constants"]
});

// With @babel/core transform
import { transform } from "@babel/core";

const result = transform(`
  const a = 1;
  a = 2; // This would throw a runtime error
`, {
  plugins: ["@babel/plugin-check-constants"]
});

Configuration files:

// .babelrc
{
  "plugins": ["@babel/check-constants"]
}
# CLI usage
babel --plugins @babel/check-constants script.js

Capabilities

Plugin Factory Function

The main export that creates a Babel plugin instance.

/**
 * Creates a Babel plugin for validating const variable reassignments
 * @returns {BabelPlugin} Babel plugin object with visitor pattern
 */
export default function(): BabelPlugin;

// Required import from @babel/core
import { types as t } from "@babel/core";

interface BabelPlugin {
  visitor: {
    Scope(path: ScopePath, state: PluginState): void;
  };
}

interface ScopePath {
  scope: Scope;
}

interface Scope {
  bindings: {
    [variableName: string]: Binding;
  };
}

interface Binding {
  kind: string;
  constantViolations: NodePath[];
}

interface PluginState {
  addHelper(name: string): Identifier;
}

interface NodePath {
  isAssignmentExpression(): boolean;
  isUpdateExpression(): boolean;
  isForXStatement(): boolean;
  get(key: string): NodePath;
  replaceWith(node: Node): void;
  ensureBlock(): void;
  node: Node;
}

interface Node {
  body?: {
    body: Node[];
  };
}

interface Identifier {
  type: "Identifier";
  name: string;
}

Visitor Pattern Implementation

The plugin uses Babel's visitor pattern to traverse Abstract Syntax Tree (AST) scopes:

Scope Visitor:

  • Identifies const variable bindings within each scope
  • Detects reassignment violations (constantViolations)
  • Transforms violations into runtime error calls
  • Injects readOnlyError helper for error throwing

Transformation Logic

The plugin handles three types of const violations:

Assignment Expressions (const a = 1; a = 2;):

  • Wraps the right-hand side with a sequence expression
  • Calls readOnlyError helper before original assignment
  • Preserves original assignment behavior while adding error

Update Expressions (const a = 1; a++;):

  • Replaces entire update expression with sequence expression
  • Includes readOnlyError call followed by original update
  • Maintains expression semantics with error injection

For-X Statements (const a = 1; for (a in obj) {}):

  • Ensures loop body is a block statement
  • Prepends readOnlyError call to loop body
  • Triggers error before loop execution begins

Runtime Helper Integration

/**
 * Runtime helper function injected by the plugin
 * Throws a descriptive error for const violations
 * @param {string} variableName - Name of the const variable being reassigned
 * @throws {TypeError} Descriptive error about const reassignment
 */
function readOnlyError(variableName: string): never;

The readOnlyError helper is automatically injected via state.addHelper("readOnlyError") and provides consistent error messages across all violation types.

Integration Patterns

Common Plugin Combinations

The plugin was typically used alongside:

{
  "plugins": [
    "@babel/plugin-check-constants",
    "@babel/plugin-transform-block-scoping",
    "@babel/plugin-transform-flow-strip-types"
  ]
}

With Block Scoping: Essential for compiling const to var while maintaining validation With Flow: Compatible with Flow type stripping for typed codebases

Error Handling

When const violations are detected, the plugin transforms code to throw descriptive runtime errors:

// Input
const message = "Hello";
message = "World";

// Transformed output (conceptual)
const message = "Hello";
message = (function() { 
  throw new TypeError('"message" is read-only'); 
  return "World"; 
})();

The actual transformation uses Babel's helper system and AST manipulation for proper integration with the compilation pipeline.

Migration Notes

For modern usage, replace with:

{
  "plugins": ["@babel/plugin-transform-block-scoping"]
}

The block-scoping transform includes const validation functionality and provides the same runtime protection with better performance and maintenance.