This plugin transforms all the ES2015 'instanceof' methods
npx @tessl/cli install tessl/npm-babel--plugin-transform-instanceof@7.27.0@babel/plugin-transform-instanceof is a Babel plugin that transforms ES2015 instanceof operator expressions into helper function calls. This transformation ensures consistent instanceof behavior across different JavaScript environments by replacing native instanceof operations with a helper function that provides standardized implementation.
npm install --save-dev @babel/plugin-transform-instanceofimport plugin from "@babel/plugin-transform-instanceof";For CommonJS:
const plugin = require("@babel/plugin-transform-instanceof");Configure the plugin in your Babel configuration:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-transform-instanceof"]
};Or with options object:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-transform-instanceof", {}]
]
};Input code:
a instanceof b;Transformed output:
babelHelpers.instanceof(a, b);The plugin is built as a standard Babel transformation plugin:
declare from @babel/helper-plugin-utilsinstanceof expressionsinstanceof helper functionThe main export that creates the Babel plugin instance.
/**
* Creates a Babel plugin that transforms instanceof expressions
* @param api - Babel plugin API provided by Babel core
* @param options - Plugin configuration options (unused by this plugin)
* @param dirname - Directory name (provided by Babel)
* @returns Babel plugin object with visitor and metadata
*/
declare function default(
api: PluginAPI,
options: object,
dirname: string
): PluginObject<PluginPass>;
interface PluginObject<State> {
name: string;
visitor: Visitor<State>;
}
interface Visitor<State> {
BinaryExpression?(
path: NodePath<t.BinaryExpression>,
state: State & PluginPass
): void;
}Transforms instanceof binary expressions into helper function calls.
/**
* Visitor method that processes BinaryExpression AST nodes
* @param path - Babel NodePath for the binary expression
*/
BinaryExpression(path: NodePath<t.BinaryExpression>): void;The transformation logic:
instanceof operatorinstanceof helper function to the current scopebabelHelpers.instanceof(left, right)// From @babel/core
interface PluginAPI {
version: string;
assertVersion(version: number | string): void;
}
interface PluginPass {
// Babel plugin pass state
}
// From @babel/types
interface BinaryExpression extends Node {
type: "BinaryExpression";
operator: BinaryOperator;
left: Expression | PrivateName;
right: Expression;
}
type BinaryOperator = "instanceof" | "in" | "==" | "!=" | /* ... other operators */;The plugin requires the instanceof helper function to be available in the transformed code. This helper is automatically injected by Babel's helper system when the plugin is used.
The helper function implementation (from @babel/helpers):
/**
* Runtime helper for instanceof operations
* @minVersion 7.0.0-beta.0
* @param left - Left operand of instanceof
* @param right - Right operand (constructor function)
* @returns Boolean result of instanceof check
*/
function _instanceof(left: any, right: Function): boolean;// Input
class Animal {}
class Dog extends Animal {}
const pet = new Dog();
if (pet instanceof Animal) {
console.log("pet is an animal");
}
// Output (transformed)
class Animal {}
class Dog extends Animal {}
const pet = new Dog();
if (babelHelpers.instanceof(pet, Animal)) {
console.log("pet is an animal");
}The transformation works correctly with custom Symbol.hasInstance implementations:
// Input
function CustomClass() {}
CustomClass[Symbol.hasInstance] = function(instance) {
return instance.customProp === true;
};
const obj = { customProp: true };
console.log(obj instanceof CustomClass);
// Output (transformed)
function CustomClass() {}
CustomClass[Symbol.hasInstance] = function(instance) {
return instance.customProp === true;
};
const obj = { customProp: true };
console.log(babelHelpers.instanceof(obj, CustomClass));// Input
const result = (a instanceof Array) && (b instanceof Object);
// Output (transformed)
const result = babelHelpers.instanceof(a, Array) && babelHelpers.instanceof(b, Object);