or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

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

Babel plugin that validates ES2015 const declarations by preventing reassignment at compile time

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

To install, run

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

index.mddocs/

babel-plugin-check-es2015-constants

A Babel plugin that validates ES2015 const declarations by preventing reassignment of const variables at compile time. It provides early detection of const violations during the compilation process rather than at runtime.

Package Information

  • Package Name: babel-plugin-check-es2015-constants
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-check-es2015-constants

Core Imports

This is a Babel plugin, so it's typically used via Babel configuration rather than direct imports:

.babelrc:

{
  "plugins": ["check-es2015-constants"]
}

Via Node API:

const babel = require("babel-core");
const result = babel.transform(code, {
  plugins: ["check-es2015-constants"]
});

Via CLI:

babel --plugins check-es2015-constants script.js

Basic Usage

The plugin automatically validates const declarations during compilation. No additional configuration is required.

// Input code
const a = 1;
a = 2; // This will throw a compile-time error

// Error output:
// SyntaxError: "a" is read-only

Complex const validation:

// The plugin catches various forms of const violations
const obj = { prop: 1 };
obj = {}; // Error: "obj" is read-only

const [x, y] = [1, 2];
x = 3; // Error: "x" is read-only

// But property mutations are allowed (this is valid)
const obj2 = { prop: 1 };
obj2.prop = 2; // OK - not reassigning the const binding

Architecture

The plugin follows the standard Babel plugin architecture:

  • Plugin Factory: Returns a Babel plugin object with visitor patterns
  • AST Traversal: Uses Babel's visitor pattern to traverse the Abstract Syntax Tree
  • Scope Analysis: Examines each scope for const bindings and violations
  • Error Reporting: Throws descriptive compile-time errors with code frames

Capabilities

Plugin Factory Function

The main plugin factory function that creates the Babel plugin.

/**
 * Default export - Creates a Babel plugin for checking ES2015 const violations
 * @param {Object} context - Babel plugin context containing messages
 * @param {Object} context.messages - Babel messages object for error formatting
 * @returns {Object} Babel plugin object with visitor methods
 */
export default function ({ messages }) {
  return {
    visitor: {
      Scope({ scope }) { /* implementation */ }
    }
  };
}

Babel Plugin Object

The plugin object returned by the factory function.

interface BabelPlugin {
  /** AST visitor methods for traversing code */
  visitor: {
    /** Processes each scope to check for const violations */
    Scope(path: { scope: Scope }): void;
  };
}

Scope Visitor

Processes each scope in the AST to validate const declarations.

/**
 * Scope visitor that checks for const binding violations
 * @param {Object} path - Babel path object
 * @param {Scope} path.scope - Babel scope containing bindings information
 * @throws {Error} Throws compile-time error for const violations
 */
function Scope({ scope }) {
  // Iterates through bindings and throws errors for const violations
}

Error Handling

The plugin throws compile-time errors when const violations are detected:

  • Error Type: Code frame error built using Babel's buildCodeFrameError
  • Error Message: "<variable_name>" is read-only
  • Error Location: Points to the exact line and column of the violation
  • Error Context: Includes surrounding code context for debugging

Example error output:

repl: "a" is read-only
  1 | const a = 1;
> 2 | a = 2;
    | ^

Binding Types Checked

The plugin validates the following binding types:

  • const declarations: Variables declared with const
  • module bindings: Import declarations (also treated as constants)

Violation types detected:

  • Direct assignment (const a = 1; a = 2;)
  • Update expressions (const a = 1; a++;)
  • Assignment expressions (const a = 1; a += 1;)
  • Destructuring assignment violations

Integration Notes

  • Compilation Only: This plugin only validates - it does not transform const to var
  • Development Tool: Primarily intended for development-time error catching
  • Complementary: Works alongside transform-es2015-block-scoping for full ES2015 const support
  • Performance: Minimal overhead as it only traverses scopes once during compilation