This plugin transforms checks for a private property in an object
npx @tessl/cli install tessl/npm-babel--plugin-transform-private-property-in-object@7.27.0@babel/plugin-transform-private-property-in-object is a Babel plugin that transforms checks for private properties in objects (#property in obj syntax) into runtime-safe equivalents. It generates WeakSet-based brand checking mechanisms to detect private fields and methods, enabling modern JavaScript private field syntax across different JavaScript engines and versions.
npm install --save-dev @babel/plugin-transform-private-property-in-objectThe plugin is used in Babel configuration, not imported directly in application code:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-transform-private-property-in-object", { loose: false }]
]
};Or in package.json with babel preset:
{
"babel": {
"plugins": [
["@babel/plugin-transform-private-property-in-object", { "loose": true }]
]
}
}The plugin automatically transforms private property in object checks during the Babel compilation process:
Input JavaScript:
class MyClass {
#privateField = 42;
#privateMethod() { return "secret"; }
checkInstance(obj) {
// These expressions get transformed by the plugin
return #privateField in obj && #privateMethod in obj;
}
}Transformed Output (loose: false):
var _privateField = new WeakSet();
var _privateMethod = new WeakSet();
class MyClass {
constructor() {
_privateField.add(this);
_privateMethod.add(this);
}
checkInstance(obj) {
return _privateField.has(obj) && _privateMethod.has(obj);
}
}The main export is a Babel plugin factory function created using the declare helper from @babel/helper-plugin-utils.
/**
* Default export: Babel plugin factory function
* Created using declare() helper for transforming private property in object checks
* @param api - Babel plugin API with types, template, and version utilities
* @param options - Plugin configuration options with loose mode setting
* @returns Babel plugin configuration object with visitor pattern
*/
const plugin: (
api: PluginAPI,
options: Options
) => PluginObject;
export default plugin;Configuration interface for customizing plugin behavior.
/**
* Configuration options for the private property in object transform plugin
*/
interface Options {
/**
* Enable loose mode for better performance but different semantics
* - loose: true - Uses simple property checks (faster, less spec-compliant)
* - loose: false - Uses WeakSet-based brand checking (slower, spec-compliant)
* @default false
*/
loose?: boolean;
}The plugin returns a standard Babel plugin configuration object with visitor pattern.
interface PluginObject {
/** Plugin identifier name */
name: "transform-private-property-in-object";
/** Optional parser manipulation - adds "privateIn" plugin for legacy environments (disabled in Babel 8) */
manipulateOptions?: (opts: any, parser: ParserOptions) => void;
/** Pre-processing hook to enable features using helper-create-class-features-plugin */
pre(): void;
/** AST visitor configuration for transforming BinaryExpression nodes */
visitor: {
BinaryExpression(path: NodePath<BinaryExpression>, state: PluginPass): void;
};
}The plugin uses standard Babel plugin interfaces from @babel/core:
import type { PluginAPI, PluginObject, NodePath, PluginPass } from "@babel/core";
interface PluginAPI {
/** Babel core type definitions */
types: typeof t;
/** Template string utilities for generating AST nodes */
template: Template;
/** Assert minimum Babel version compatibility */
assertVersion(version: number | string): void;
}
interface PluginPass {
/** Current file being processed */
file: File;
}
interface ParserOptions {
/** Parser plugins to enable */
plugins: string[];
}The plugin imports helper functions from @babel/helper-create-class-features-plugin:
/** Enable feature flag for privateIn transformation */
function enableFeature(file: File, feature: number, loose: boolean): void;
/** FEATURES constant from helper plugin */
const FEATURES: {
privateIn: number;
};
/** Build check for right-hand side of 'in' expressions */
function buildCheckInRHS(node: Expression, file: File): Expression;
/** Version requirement helper for assertVersion() */
function REQUIRED_VERSION(major: number): number | string;The plugin handles different types of private property checks:
Instance Private Fields: Transformed to WeakSet-based brand checking
// Input: #field in obj
// Output: _field_brandCheck.has(obj)Static Private Methods: Transformed to class identity checking
// Input: #staticMethod in obj
// Output: MyClass === objInstance Private Methods: Transformed to WeakSet-based brand checking
// Input: #method in obj
// Output: _class_brandCheck.has(obj)The plugin automatically:
loose: false): Uses WeakSet-based brand checking for full spec complianceloose: true): May use simpler checks for better performance but different semantics@babel/helper-plugin-utils: Provides declare() function for creating Babel plugins@babel/helper-create-class-features-plugin: Provides enableFeature(), FEATURES, buildCheckInRHS(), and injectInitialization() helpers@babel/helper-annotate-as-pure: Provides annotateAsPure() for marking expressions as pure for optimizationThe plugin handles various edge cases during transformation: