Babel plugin that transforms ES2015 sticky regex literals to ES5-compatible RegExp constructor calls
npx @tessl/cli install tessl/npm-babel-plugin-transform-es2015-sticky-regex@6.24.0A 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.
npm install --save-dev babel-plugin-transform-es2015-sticky-regex// The plugin is imported by Babel automatically when configured
// No direct import needed in your source codePlugin configuration examples:
// .babelrc
{
"plugins": ["transform-es2015-sticky-regex"]
}// Babel API usage
const babel = require("babel-core");
babel.transform("code", {
plugins: ["transform-es2015-sticky-regex"]
});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; // unchangedThe plugin integrates into Babel's AST transformation pipeline using the visitor pattern:
RegExpLiteral nodes with sticky flagy flag, leaving others unchangedCreates 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;
}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;
}The core transformation converts sticky regex literals to RegExp constructor calls:
/pattern/y)babel-helper-regex.is(node, "y") to detect sticky flagnew RegExp(pattern, flags) using babel-typesThe plugin relies on these Babel ecosystem packages:
^6.24.1 - Utilities for regex analysis and flag detection^6.24.1 - AST node creation and manipulation utilities^6.22.0 - Runtime support for Babel transformations// 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;
}{
"plugins": ["transform-es2015-sticky-regex"]
}babel --plugins transform-es2015-sticky-regex script.jsconst babel = require("babel-core");
const result = babel.transform(sourceCode, {
plugins: ["transform-es2015-sticky-regex"]
});{
"presets": ["es2015"],
"plugins": ["transform-es2015-sticky-regex"]
}The plugin performs safe transformations:
This plugin enables sticky regex functionality in environments that support:
y flag at runtimeThe transformation itself is purely compile-time and adds no runtime dependencies.