Babel helper plugin that provides essential functionality for transforming modern JavaScript class features to ES6-compatible code
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core feature enablement and configuration system that manages the bit flag-based feature system and ensures consistency across multiple class feature plugins.
Defines the available class features that can be transformed.
/**
* Feature flags for different class transformations
* Each feature is a power of 2 to allow bitwise operations
*/
const FEATURES: {
readonly fields: number; // 1 << 1 (2) - Class properties/fields
readonly privateMethods: number; // 1 << 2 (4) - Private methods
readonly decorators: number; // 1 << 3 (8) - Class decorators
readonly privateIn: number; // 1 << 4 (16) - Private property in checks
readonly staticBlocks: number; // 1 << 5 (32) - Static class blocks
};Usage Examples:
import { FEATURES } from "@babel/helper-create-class-features-plugin";
// Single feature
const fieldFeature = FEATURES.fields;
// Multiple features using bitwise OR
const combinedFeatures = FEATURES.fields | FEATURES.privateMethods;
// Check if specific feature is included
const hasFields = combinedFeatures & FEATURES.fields; // truthy
const hasDecorators = combinedFeatures & FEATURES.decorators; // 0 (falsy)Enables a feature in the Babel file state with loose mode configuration.
/**
* Enables a feature with loose mode configuration
* Ensures consistency across multiple plugins using the same features
* @param file - Babel file instance
* @param feature - Feature flag to enable
* @param loose - Whether to use loose mode for this feature
*/
function enableFeature(file: File, feature: number, loose: boolean): void;Usage Examples:
import { enableFeature, FEATURES } from "@babel/helper-create-class-features-plugin";
// Enable fields in strict mode
enableFeature(file, FEATURES.fields, false);
// Enable private methods in loose mode
enableFeature(file, FEATURES.privateMethods, true);
// Enable multiple features
enableFeature(file, FEATURES.fields | FEATURES.privateIn, options.loose);Checks if a feature is configured in loose mode.
/**
* Checks if a feature is in loose mode
* @param file - Babel file instance
* @param feature - Feature flag to check
* @returns true if the feature is in loose mode
*/
function isLoose(file: File, feature: number): boolean;Usage Examples:
import { isLoose, FEATURES } from "@babel/helper-create-class-features-plugin";
// Check if fields are in loose mode
const fieldsLoose = isLoose(file, FEATURES.fields);
// Conditional logic based on loose mode
if (isLoose(file, FEATURES.privateMethods)) {
// Use symbol-based private method implementation
} else {
// Use WeakSet-based private method implementation
}Determines if a class should be transformed based on enabled features and class contents.
/**
* Determines if a class path should be transformed
* Validates that required features are enabled for the class contents
* @param path - Class node path to analyze
* @param file - Babel file instance
* @returns true if the class should be transformed
* @throws Error if required features are not enabled
*/
function shouldTransform(path: NodePath<t.Class>, file: File): boolean;Usage Examples:
import { shouldTransform } from "@babel/helper-create-class-features-plugin";
// In a Babel plugin visitor
visitor: {
Class(path, { file }) {
if (!shouldTransform(path, file)) return;
// Proceed with transformation
transformClass(path, file);
}
}The feature system enforces consistency across multiple plugins:
// These plugins must have matching loose mode settings
const plugins = [
["@babel/plugin-transform-class-properties", { loose: true }],
["@babel/plugin-transform-private-methods", { loose: true }],
["@babel/plugin-transform-private-property-in-object", { loose: true }]
];
// Mismatched settings will throw an error
const invalidPlugins = [
["@babel/plugin-transform-class-properties", { loose: true }],
["@babel/plugin-transform-private-methods", { loose: false }], // Error!
];The system validates that required features are enabled for class contents:
// Class with private methods requires FEATURES.privateMethods
class Example {
#privateMethod() {} // Requires privateMethods feature
}
// Class with decorators requires FEATURES.decorators
@decorator
class Decorated {} // Requires decorators feature
// Class with static blocks requires FEATURES.staticBlocks
class WithStaticBlock {
static {} // Requires staticBlocks feature
}The system uses internal keys to track feature state:
"@babel/plugin-class-features/featuresKey" - Enabled features bitmask"@babel/plugin-class-features/looseKey" - Loose mode bitmask"@babel/plugin-class-features/looseLowPriorityKey" - Preset-env loose mode trackingThe system provides detailed error messages for common configuration issues: