CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-jscript

Babel plugin to fix buggy JScript named function expressions

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-jscript

A Babel plugin that transforms named function expressions into function declarations wrapped in immediately-invoked function expressions (IIFEs) to fix problematic JScript engine bugs. This plugin ensures consistent behavior across different JavaScript engines, particularly legacy JScript implementations in older Internet Explorer versions.

Package Information

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

Core Imports

// The plugin is used via Babel configuration, not direct imports
// Plugin is automatically loaded by Babel when referenced in configuration

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-jscript"]
}

Via CLI

babel --plugins transform-jscript script.js

Via Node API

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

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

Transformation Example

Input:

var IdenticalName = function IdenticalName(x) {
  return x;
};

(function foo() {

});

Output:

var IdenticalName = function () {
  function IdenticalName(x) {
    return x;
  }

  return IdenticalName;
}();

(function () {
  function foo() {}

  return foo;
})();

Capabilities

JScript Named Function Expression Transformation

Transforms named function expressions into safer IIFE patterns to avoid JScript engine bugs.

/**
 * Babel plugin factory function that returns a plugin configuration object
 * @param {Object} babel - Babel API object
 * @param {Object} babel.types - Babel types utility for AST manipulation
 * @returns {Object} Plugin configuration with visitor methods
 */
export default function({ types: t }) {
  return {
    visitor: {
      FunctionExpression: {
        exit(path) {
          // Transform named function expressions to fix JScript bugs
        }
      }
    }
  };
}

/**
 * @typedef {Object} BabelPluginObject
 * @property {Object} visitor - Visitor object containing AST node handlers
 * @property {Object} visitor.FunctionExpression - FunctionExpression visitor configuration
 * @property {Function} visitor.FunctionExpression.exit - Exit handler for FunctionExpression nodes
 */

Transformation Logic:

  • Only processes named function expressions (ignores anonymous ones)
  • Wraps named function expressions in an IIFE that:
    1. Declares the function as a function declaration
    2. Returns the function by name
    3. Immediately invokes the wrapper function
  • Sets _ignoreUserWhitespace flag on transformed nodes

Plugin Configuration:

  • This plugin accepts no configuration options
  • Simply include it in your Babel plugins array to enable the transformation

Use Cases:

  • Cross-browser compatibility for legacy Internet Explorer
  • Fixing JScript named function expression bugs
  • Part of Babel transformation pipeline for legacy environment support

Types

/**
 * Babel API types (provided by Babel framework)
 * @typedef {Object} NodePath
 * @property {Object} node - The AST node being visited
 * @property {Function} replaceWith - Replace the current node with a new node
 */

/**
 * @typedef {Object} FunctionExpression
 * @property {Object|null} id - Function identifier (null for anonymous functions)
 * @property {Array} params - Function parameters
 * @property {Object} body - Function body block statement
 * @property {boolean} [_ignoreUserWhitespace] - Babel internal flag for whitespace handling
 */

/**
 * @typedef {Object} BabelTypes
 * @property {Function} callExpression - Creates a call expression AST node
 * @property {Function} functionExpression - Creates a function expression AST node  
 * @property {Function} blockStatement - Creates a block statement AST node
 * @property {Function} toStatement - Converts an expression to a statement
 * @property {Function} returnStatement - Creates a return statement AST node
 */

Error Handling

The plugin gracefully handles edge cases:

  • Anonymous function expressions: Ignored (no transformation applied)
  • Malformed AST nodes: Relies on Babel's built-in error handling
  • Missing function names: Returns early without transformation

Dependencies

  • Runtime: babel-runtime@^6.22.0
  • Babel Integration: Works with Babel 6.x transformation pipeline

docs

index.md

tile.json