Babel plugin that automatically adds a __self={this} prop to all JSX elements during compilation. This enables React DevTools and development-time debugging by providing better error reporting and component tracking capabilities.
npm install --save-dev @babel/plugin-transform-react-jsx-selfTo use the plugin programmatically:
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
import pluginTransformReactJsxSelf from "@babel/plugin-transform-react-jsx-self";For CommonJS:
const { declare } = require("@babel/helper-plugin-utils");
const { types: t } = require("@babel/core");
const pluginTransformReactJsxSelf = require("@babel/plugin-transform-react-jsx-self");For Babel configuration usage:
// babel.config.js or .babelrc
{
"plugins": ["@babel/plugin-transform-react-jsx-self"]
}The plugin works automatically when added to your Babel configuration. It transforms JSX elements by adding the __self prop:
// Input JSX
function Component() {
return <div>Hello World</div>;
}
// Output after transformation
function Component() {
return <div __self={this}>Hello World</div>;
}The plugin is built around several key components:
declare() helper for version compatibility and API consistencythis references are safe to useAutomatically adds __self={this} prop to all JSX elements for React development debugging.
/**
* Main plugin factory function that creates a Babel plugin using declare helper
* Uses Babel's declare() helper for version compatibility and API consistency
*/
export default declare((api: PluginAPI) => PluginObject);
/**
* Plugin initialization requires Babel version 7 or higher
*/
api.assertVersion(REQUIRED_VERSION(7));
interface PluginObject {
name: "transform-react-jsx-self";
visitor: {
Program(path: NodePath<Program>): void;
};
}
/**
* Constant defining the JSX attribute name added to elements
*/
const TRACE_ID = "__self";/**
* Internal visitor that handles the actual JSX transformation
*/
interface InternalVisitor {
JSXOpeningElement(path: NodePath<JSXOpeningElement>): void;
}The plugin intelligently handles edge cases:
__self in derived class constructors before super() calls to prevent runtime errorsthis context in arrow functions vs regular functionsthis is available in different scoping contextsThe plugin includes sophisticated internal logic to determine when it's safe to reference this. It analyzes the AST using several helper functions:
/**
* Finds the closest parent function that provides `this` context
* Excludes arrow functions as they don't rebind `this`
*/
function getThisFunctionParent(
path: NodePath<JSXOpeningElement>
): NodePath<Exclude<FunctionParent, ArrowFunctionExpression>> | null;
/**
* Returns whether the class has specified a superclass
*/
function isDerivedClass(classPath: NodePath<Class>): boolean;
/**
* Returns whether `this` is allowed at given JSX element path
* Performs comprehensive scope and context analysis
*/
function isThisAllowed(path: NodePath<JSXOpeningElement>): boolean;This ensures that __self={this} is only added where it won't cause runtime errors.
interface PluginAPI {
version: string;
assertVersion(range: number | string): void;
types: typeof import("@babel/types");
}
interface PluginObject {
name: string;
visitor: {
Program(path: NodePath<Program>): void;
};
}
interface NodePath<T = Node> {
node: T;
scope: Scope;
parentPath: NodePath | null;
traverse(visitor: Visitor): void;
isFunctionParent(): boolean;
isArrowFunctionExpression(): boolean;
isMethod(): boolean;
}
interface Visitor {
JSXOpeningElement?(path: NodePath<JSXOpeningElement>): void;
Program?(path: NodePath<Program>): void;
}
interface Scope {
path: NodePath;
parent: Scope | null;
}
type FunctionParent = Function | Method | ObjectMethod | ClassMethod | ArrowFunctionExpression;
type Class = ClassDeclaration | ClassExpression;This plugin accepts no configuration options. It works automatically when included in your Babel plugin list:
{
"plugins": ["@babel/plugin-transform-react-jsx-self"]
}The plugin requires Babel core version 7.0.0 or higher and is typically used only in development builds since the __self prop is primarily useful for debugging.
The plugin includes built-in safety mechanisms:
__self injection in derived class constructors where this would cause errorsthis references are valid