Historical Babel plugin that validated ES2015 const variable reassignments at compile time
npx @tessl/cli install tessl/npm-babel--plugin-check-constants@7.0.0@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.
npm install --save-dev @babel/plugin-check-constants (historical)// CommonJS
const checkConstants = require("@babel/plugin-check-constants");
// ES6 (as default export)
import checkConstants from "@babel/plugin-check-constants";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.jsThe 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;
}The plugin uses Babel's visitor pattern to traverse Abstract Syntax Tree (AST) scopes:
Scope Visitor:
const variable bindings within each scopereadOnlyError helper for error throwingThe plugin handles three types of const violations:
Assignment Expressions (const a = 1; a = 2;):
readOnlyError helper before original assignmentUpdate Expressions (const a = 1; a++;):
readOnlyError call followed by original updateFor-X Statements (const a = 1; for (a in obj) {}):
readOnlyError call to loop body/**
* 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.
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
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.
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.