Babel plugin that transforms the proposed function bind operator (::) into standard ES5 JavaScript code
npx @tessl/cli install tessl/npm-babel-plugin-transform-function-bind@6.22.0Babel plugin that transforms the proposed function bind operator (::) into standard ES5 JavaScript code. The plugin enables developers to use the experimental bind syntax that provides more concise method binding and function calling patterns.
npm install --save-dev babel-plugin-transform-function-bind// ES6 Import
import transformFunctionBind from "babel-plugin-transform-function-bind";CommonJS:
const transformFunctionBind = require("babel-plugin-transform-function-bind");.babelrc
{
"plugins": ["transform-function-bind"]
}Via CLI:
babel --plugins transform-function-bind script.jsVia Node API:
const babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-function-bind"]
});// Basic binding (obj::func -> func.bind(obj))
const bindExample = obj::func;
// Method calling (obj::func(args) -> func.call(obj, args))
obj::method(arg1, arg2);
// Auto-binding (::obj.method -> obj.method.bind(obj))
const autobound = ::obj.method;This Babel plugin follows the standard Babel plugin architecture:
Main entry point that creates the Babel plugin configuration.
/**
* Creates Babel plugin configuration for transforming function bind syntax
* @param {Object} api - Babel API object containing types and other utilities
* @param {Object} api.types - Babel types API for AST manipulation
* @returns {BabelPluginConfig} Babel plugin configuration object
*/
function transformFunctionBind({ types: t }): BabelPluginConfig;Usage Example:
const babel = require("babel-core");
const transformFunctionBind = require("babel-plugin-transform-function-bind");
// Plugin is automatically invoked by Babel during transformation
const result = babel.transform(sourceCode, {
plugins: [transformFunctionBind]
});The plugin factory returns a Babel plugin configuration object with visitor methods.
interface BabelPluginConfig {
/** Inherits syntax parsing from babel-plugin-syntax-function-bind */
inherits: BabelPlugin;
/** AST visitor methods for transforming specific node types */
visitor: {
CallExpression: VisitorFunction;
BindExpression: VisitorFunction;
};
}
/**
* Visitor function signature for AST node transformation
* @param {Object} path - Babel path object containing node and utilities
* @param {Object} path.node - AST node being visited
* @param {Object} path.scope - Scope information for variable management
*/
type VisitorFunction = (path: { node: Object, scope: Object }) => void;Transforms function bind expressions used in call contexts (e.g., obj::func(args)).
/**
* Transforms bind expressions within call expressions
* Converts obj::func(args) to func.call(obj, args)
* @param {Object} path - Babel path object
* @param {Object} path.node - CallExpression AST node
* @param {Object} path.scope - Current scope for variable management
*/
function CallExpression({ node, scope });Transformation Examples:
// Input
obj::method(arg1, arg2);
// Output
method.call(obj, arg1, arg2);// Input with complex context
ctx::ns.obj.func(value);
// Output
ns.obj.func.call(ctx, value);Transforms standalone function bind expressions (e.g., obj::func).
/**
* Transforms standalone bind expressions
* Converts obj::func to func.bind(obj)
* @param {Object} path - Babel path object containing BindExpression node
*/
function BindExpression(path);Transformation Examples:
// Input
const boundMethod = obj::func;
// Output
const boundMethod = func.bind(obj);// Input with auto-binding
const autobound = ::obj.method;
// Output
const autobound = obj.method.bind(obj);Transforms simple object-to-function binding.
// Input
obj::func
// Output
func.bind(obj)Transforms bind expressions used in function calls.
// Input
obj::func(arg1, arg2)
// Output
func.call(obj, arg1, arg2)Transforms bind expressions with implicit object binding.
// Input
::obj.method
// Output
obj.method.bind(obj)Handles complex expressions and nested property access.
// Input
complexObj::namespace.utils.method(data)
// Output
namespace.utils.method.call(complexObj, data)interface PackageDependencies {
/** Provides syntax parsing for function bind operator */
"babel-plugin-syntax-function-bind": "^6.8.0";
/** Babel runtime helpers */
"babel-runtime": "^6.22.0";
}The plugin integrates with Babel's error handling system. Common issues include:
babel-plugin-syntax-function-bind for parsingbabel-plugin-syntax-function-bind for parsing supportBindExpression and CallExpression AST nodes