A Babel syntax plugin that enables parsing of function bind syntax (::) in JavaScript code. This plugin extends Babel's parser to recognize the function bind operator without transforming the syntax, making it suitable for parsing purposes only.
npm install --save-dev @babel/plugin-syntax-function-bind// ES6 modules
import functionBindPlugin from "@babel/plugin-syntax-function-bind";
// CommonJS
const functionBindPlugin = require("@babel/plugin-syntax-function-bind");Configure the plugin in your Babel configuration to enable parsing of function bind syntax:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-syntax-function-bind"]
};
// .babelrc
{
"plugins": ["@babel/plugin-syntax-function-bind"]
}With the plugin enabled, Babel can parse function bind syntax:
// This syntax will be parsed (but not transformed)
const boundMethod = obj::method;
const boundFunction = ::obj.method;The main export that creates a Babel plugin using the declare helper from @babel/helper-plugin-utils.
import { declare } from "@babel/helper-plugin-utils";
/**
* Default export: A Babel plugin created using the declare helper
* The plugin enables parsing of function bind syntax (::) without transformation
*/
declare const plugin: (
api: PluginAPI,
options: object,
dirname: string
) => PluginObject;
export default plugin;The plugin is implemented using Babel's declare helper:
/**
* Plugin implementation function passed to declare()
* @param api - Babel PluginAPI object containing version info and utilities
* @returns PluginObject that configures the parser to recognize function bind syntax
*/
type PluginBuilder = (api: PluginAPI) => PluginObject;
interface PluginAPI {
/** Babel version string (e.g., "7.27.1") */
version: string;
/** Assert minimum required Babel version */
assertVersion: (version: number | string) => void;
}
interface PluginObject {
/** Plugin identifier name */
name: string;
/** Function to modify parser options */
manipulateOptions: (opts: any, parserOpts: ParserOptions) => void;
}
interface ParserOptions {
/** Array of parser plugins to enable */
plugins: string[];
}The plugin implementation:
api.assertVersion(7) (validates Babel 7.x compatibility)"syntax-function-bind"manipulateOptions method that adds "functionBind" to the parser plugins arrayUsage in Babel Configuration:
// babel.config.js - Using plugin by name (recommended)
module.exports = {
plugins: ["@babel/plugin-syntax-function-bind"]
};
// .babelrc.json
{
"plugins": ["@babel/plugin-syntax-function-bind"]
}
// Direct plugin function usage (advanced)
import functionBindPlugin from "@babel/plugin-syntax-function-bind";
module.exports = {
plugins: [functionBindPlugin]
};When the plugin is active, Babel's parser will recognize the function bind operator (::) in the following forms:
// Function binding forms that can be parsed:
obj::method // Bind method to obj
::obj.method // Extract bound method from obj
obj::method() // Bind and call immediately
::obj[method] // Dynamic method binding@babel/helper-plugin-utils: ^7.0.0 (workspace dependency) - Provides the declare helper function for creating standardized Babel plugins@babel/core: ^7.0.0-0 (Required Babel core version for plugin compatibility)@babel/core: ^7.0.0 (workspace dependency) - Used for testing and development"functionBind"The plugin provides the following exports structure:
// Main export (default)
export default plugin;
// TypeScript definitions available at
// "./lib/index.d.ts"
// Package.json accessible via
// "./package.json"The function bind operator (::) is an experimental JavaScript feature that provides syntactic sugar for binding functions to objects. While not yet part of the ECMAScript standard, this syntax plugin enables Babel to parse such code for development and experimentation purposes.
Note: This plugin only enables parsing - to actually transform function bind syntax into working JavaScript, you would need a separate transformation plugin like @babel/plugin-proposal-function-bind.