CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-object-assign

Replace Object.assign with an inline helper

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

index.mddocs/

babel-plugin-transform-object-assign

A Babel plugin that replaces Object.assign calls with an inline helper function for improved compatibility with older JavaScript environments. This plugin performs build-time transformations, eliminating the need for runtime polyfills while maintaining the same API.

Package Information

  • Package Name: babel-plugin-transform-object-assign
  • Package Type: npm
  • Language: JavaScript (ES6 modules)
  • Installation: npm install --save-dev babel-plugin-transform-object-assign

Core Imports

This is a Babel plugin, so it's not imported directly in your code. Instead, it's configured in your Babel setup:

Via .babelrc (Recommended):

{
  "plugins": ["transform-object-assign"]
}

Via Node API:

require("babel-core").transform("code", {
  plugins: ["transform-object-assign"]
});

Basic Usage

The plugin automatically transforms Object.assign calls during compilation:

Input:

Object.assign(a, b);
Object.assign({}, defaults, options);
Object['assign'](target, source);

Output:

var _extends = /* inline helper function */;

_extends(a, b);
_extends({}, defaults, options);
_extends(target, source);

Configuration Methods

.babelrc Configuration

Configure the plugin in your .babelrc file:

{
  "plugins": ["transform-object-assign"]
}

CLI Usage

Use the plugin via the Babel CLI:

babel --plugins transform-object-assign script.js

Programmatic API

Use the plugin programmatically with babel-core:

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

const result = babel.transform("Object.assign(a, b);", {
  plugins: ["transform-object-assign"]
});

Capabilities

Plugin Factory Function

The main export function that creates the Babel plugin configuration.

/**
 * Creates a Babel plugin that transforms Object.assign calls
 * @returns {Object} Babel plugin configuration object
 */
export default function(): BabelPlugin;

interface BabelPlugin {
  visitor: {
    CallExpression: (path: BabelPath, file: BabelFile) => void;
  };
}

AST Transformation

The plugin uses Babel's AST visitor pattern to transform Object.assign calls:

/**
 * Visitor method that processes CallExpression nodes
 * @param {BabelPath} path - Babel AST path object for the CallExpression
 * @param {BabelFile} file - Babel file object with helper methods  
 */
CallExpression(path: BabelPath, file: BabelFile): void;

Transformation Logic:

  • Identifies calls matching Object.assign or Object['assign'] patterns
  • Replaces the callee with Babel's extends helper function
  • The helper function is automatically injected into the transformed code

Supported Patterns

The plugin transforms these patterns:

// Direct property access
Object.assign(target, source);

// Bracket notation
Object['assign'](target, source);

// Multiple arguments
Object.assign({}, defaults, options, overrides);

Unsupported Patterns

These patterns are not transformed:

// Destructured assignment
const { assign } = Object;
assign(target, source);

// Variable assignment
const assign = Object.assign;
assign(target, source);

// Method call on variable
const obj = Object;
obj.assign(target, source);

Dependencies

Runtime Dependencies

interface Dependencies {
  "babel-runtime": "^6.22.0";
}

The plugin requires babel-runtime for the injected helper functions.

Implementation Details

Transformation Mechanism

  1. AST Traversal: The plugin visits all CallExpression nodes in the AST
  2. Pattern Matching: Uses path.get("callee").matchesPattern("Object.assign") to identify target calls
  3. Helper Injection: Replaces the callee with file.addHelper("extends")
  4. Code Generation: Babel generates the final code with the inline helper

Generated Helper

The plugin injects an _extends helper function that provides the same functionality as Object.assign:

var _extends = Object.assign || function (target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = arguments[i];
    for (var key in source) {
      if (Object.prototype.hasOwnProperty.call(source, key)) {
        target[key] = source[key];
      }
    }
  }
  return target;
};

Error Handling

The plugin operates at build-time and doesn't throw runtime errors. Potential issues:

  • Invalid Syntax: Babel will report syntax errors during compilation
  • Missing Dependencies: Missing babel-runtime will cause build failures
  • Unsupported Patterns: Unsupported Object.assign patterns are left untransformed

Use Cases

Library Development

Ideal for libraries that need to support older browsers:

// Source code
function mergeOptions(defaults, options) {
  return Object.assign({}, defaults, options);
}

// Transformed output (compatible with older browsers)
function mergeOptions(defaults, options) {
  return _extends({}, defaults, options);
}

Legacy Browser Support

Enables use of Object.assign syntax while maintaining IE compatibility:

// Write modern code
const config = Object.assign({
  timeout: 5000,
  retries: 3
}, userOptions);

// Automatically transformed for legacy support
const config = _extends({
  timeout: 5000,
  retries: 3
}, userOptions);

Types

interface BabelPath {
  get(property: string): BabelPath;
  matchesPattern(pattern: string): boolean;
  node: ASTNode;
}

interface BabelFile {
  addHelper(name: string): ASTNode;
}

interface ASTNode {
  callee: ASTNode;
  type: string;
}

docs

index.md

tile.json