CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-function-bind

Babel plugin that transforms the proposed function bind operator (::) into standard ES5 JavaScript code

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Babel Plugin Transform Function Bind

Babel 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.

Package Information

  • Package Name: babel-plugin-transform-function-bind
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-function-bind

Core Imports

// ES6 Import
import transformFunctionBind from "babel-plugin-transform-function-bind";

CommonJS:

const transformFunctionBind = require("babel-plugin-transform-function-bind");

Basic Usage

Babel Configuration

.babelrc

{
  "plugins": ["transform-function-bind"]
}

Via CLI:

babel --plugins transform-function-bind script.js

Via Node API:

const babel = require("babel-core");

const result = babel.transform(code, {
  plugins: ["transform-function-bind"]
});

Function Bind Syntax Examples

// 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;

Architecture

This Babel plugin follows the standard Babel plugin architecture:

  • Plugin Factory: Main export function that receives Babel's types API
  • Plugin Configuration: Returns object with visitor methods and inheritance
  • AST Visitors: Transform specific node types during compilation
  • Context Inference: Manages binding contexts and temporary variables

Capabilities

Plugin Factory Function

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]
});

Plugin Configuration Object

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;

CallExpression Visitor

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);

BindExpression Visitor

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);

Supported Transformation Patterns

Basic Binding Pattern

Transforms simple object-to-function binding.

// Input
obj::func

// Output
func.bind(obj)

Method Call Pattern

Transforms bind expressions used in function calls.

// Input
obj::func(arg1, arg2)

// Output
func.call(obj, arg1, arg2)

Auto-binding Pattern

Transforms bind expressions with implicit object binding.

// Input
::obj.method

// Output
obj.method.bind(obj)

Complex Context Handling

Handles complex expressions and nested property access.

// Input
complexObj::namespace.utils.method(data)

// Output
namespace.utils.method.call(complexObj, data)

Dependencies

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";
}

Error Handling

The plugin integrates with Babel's error handling system. Common issues include:

  • Syntax Errors: Require babel-plugin-syntax-function-bind for parsing
  • Scope Issues: Plugin manages temporary variables to avoid naming conflicts
  • AST Transformation: Maintains proper AST node structure during transformation

Integration Requirements

  • Babel Version: Compatible with Babel 6.x
  • Syntax Plugin: Requires babel-plugin-syntax-function-bind for parsing support
  • Execution Order: Should be applied after syntax parsing but before other transformations
  • Node Types: Processes BindExpression and CallExpression AST nodes
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-function-bind@6.22.x
Publish Source
CLI
Badge
tessl/npm-babel-plugin-transform-function-bind badge