or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-transform-es2015-sticky-regex

Babel plugin that transforms ES2015 sticky regex literals to ES5-compatible RegExp constructor calls

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-es2015-sticky-regex@6.24.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-transform-es2015-sticky-regex@6.24.0

index.mddocs/

babel-plugin-transform-es2015-sticky-regex

A Babel transformation plugin that converts ES2015 sticky regex literals (using the y flag) into ES5-compatible RegExp constructor calls. This enables sticky regex functionality in older JavaScript environments while preserving runtime behavior.

Package Information

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

Core Imports

// The plugin is imported by Babel automatically when configured
// No direct import needed in your source code

Plugin configuration examples:

// .babelrc
{
  "plugins": ["transform-es2015-sticky-regex"]
}
// Babel API usage
const babel = require("babel-core");
babel.transform("code", {
  plugins: ["transform-es2015-sticky-regex"]
});

Basic Usage

This plugin automatically transforms sticky regex literals during Babel compilation:

Input (ES2015):

var stickyRegex = /o+/y;
var normalRegex = /pattern/g;

Output (ES5-compatible):

var stickyRegex = new RegExp("o+", "y");
var normalRegex = /pattern/g; // unchanged

Architecture

The plugin integrates into Babel's AST transformation pipeline using the visitor pattern:

  • Plugin Factory: Default export function that returns Babel plugin configuration
  • Visitor Object: Contains transformation rules for specific AST node types
  • RegExp Transformation: Specifically targets RegExpLiteral nodes with sticky flag
  • Selective Processing: Only transforms regex literals with the y flag, leaving others unchanged

Capabilities

Plugin Factory Function

Creates a Babel plugin configuration object with visitor pattern transformations.

/**
 * Default export function that creates the Babel plugin
 * @returns {Object} Babel plugin configuration object
 */
export default function(): BabelPlugin;

interface BabelPlugin {
  visitor: BabelVisitor;
}

Babel Visitor Object

Contains the transformation logic for RegExp literal nodes.

interface BabelVisitor {
  /**
   * Visitor method for RegExp literal AST nodes
   * @param {NodePath} path - Babel NodePath representing a RegExp literal
   */
  RegExpLiteral(path: NodePath): void;
}

Transformation Logic

The core transformation converts sticky regex literals to RegExp constructor calls:

  • Target: RegExp literals with sticky flag (/pattern/y)
  • Check: Uses babel-helper-regex.is(node, "y") to detect sticky flag
  • Transform: Replaces with new RegExp(pattern, flags) using babel-types
  • Preserve: Non-sticky regex literals remain unchanged

Dependencies

The plugin relies on these Babel ecosystem packages:

  • babel-helper-regex: ^6.24.1 - Utilities for regex analysis and flag detection
  • babel-types: ^6.24.1 - AST node creation and manipulation utilities
  • babel-runtime: ^6.22.0 - Runtime support for Babel transformations

Types

// NodePath interface (from Babel)
interface NodePath {
  node: RegExpLiteral;
  replaceWith(newNode: ASTNode): void;
}

// RegExp literal AST node structure
interface RegExpLiteral {
  type: "RegExpLiteral";
  pattern: string;
  flags: string;
}

// Generic AST node type (from Babel)
interface ASTNode {
  type: string;
  [key: string]: any;
}

Configuration Examples

Via .babelrc

{
  "plugins": ["transform-es2015-sticky-regex"]
}

Via CLI

babel --plugins transform-es2015-sticky-regex script.js

Via Node API

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

const result = babel.transform(sourceCode, {
  plugins: ["transform-es2015-sticky-regex"]
});

With Babel Preset

{
  "presets": ["es2015"],
  "plugins": ["transform-es2015-sticky-regex"]
}

Error Handling

The plugin performs safe transformations:

  • Only processes RegExp literals (not RegExp constructor calls)
  • Checks for sticky flag presence before transformation
  • Preserves original pattern and flags exactly
  • No runtime errors introduced by transformation

Browser Compatibility

This plugin enables sticky regex functionality in environments that support:

  • RegExp constructor with flags parameter
  • ES5+ JavaScript engines
  • Modern browsers and Node.js versions that support the y flag at runtime

The transformation itself is purely compile-time and adds no runtime dependencies.