CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--helper-create-regexp-features-plugin

Babel helper plugin that compiles ESNext regular expression features to ES5-compatible syntax

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/helper-create-regexp-features-plugin

@babel/helper-create-regexp-features-plugin is a Babel helper utility that creates plugins for transforming modern ECMAScript regular expression features to ES5-compatible syntax. It provides a factory function for building Babel plugins that can compile advanced regex patterns including Unicode property escapes, named capture groups, and other features not supported in older JavaScript environments.

Package Information

  • Package Name: @babel/helper-create-regexp-features-plugin
  • Package Type: npm
  • Language: JavaScript (TypeScript source)
  • Installation: npm install @babel/helper-create-regexp-features-plugin

Core Imports

import { createRegExpFeaturePlugin, type Options } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

For CommonJS:

const { createRegExpFeaturePlugin } = require("@babel/helper-create-regexp-features-plugin");
const { declare } = require("@babel/helper-plugin-utils");

Basic Usage

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

// Typical usage in a Babel plugin
export default declare((api, options) => {
  api.assertVersion("^7.0.0");
  
  return createRegExpFeaturePlugin({
    name: "transform-unicode-property-regex",
    feature: "unicodePropertyEscape",
    options: {
      useUnicodeFlag: options.useUnicodeFlag ?? true
    }
  });
});

// Direct usage (less common)
const namedGroupsPlugin = createRegExpFeaturePlugin({
  name: "transform-named-capturing-groups-regex", 
  feature: "namedCaptureGroups",
  options: {
    runtime: true
  }
});

Capabilities

Plugin Factory

Creates a Babel plugin that transforms specific regular expression features from modern ECMAScript to ES5-compatible syntax.

/**
 * Creates a Babel plugin for transforming specific regexp features
 * @param options - Configuration object for the plugin
 * @returns Babel plugin object with visitor methods
 */
function createRegExpFeaturePlugin(options: Options): PluginObject;

interface Options {
  /** Plugin name identifier */
  name: string;
  /** Specific regex feature to transform */
  feature: keyof typeof FEATURES;
  /** Optional configuration settings */
  options?: {
    /** Whether to use Unicode flag - when false, transforms Unicode flag (/u) to ES5 */
    useUnicodeFlag?: boolean;
    /** Whether to use runtime helpers for named capture groups (default: true) */
    runtime?: boolean;
  };
  /** Optional Babel plugin manipulateOptions method */
  manipulateOptions?: PluginObject["manipulateOptions"];
}

Available Features

Features are defined in the FEATURES constant:

const FEATURES = Object.freeze({
  unicodeFlag: 1 << 0,
  dotAllFlag: 1 << 1,
  unicodePropertyEscape: 1 << 2,
  namedCaptureGroups: 1 << 3,
  unicodeSetsFlag_syntax: 1 << 4, // Not used, for backward compatibility
  unicodeSetsFlag: 1 << 5,
  duplicateNamedCaptureGroups: 1 << 6,
  modifiers: 1 << 7,
});

The feature parameter accepts these feature keys:

  • "unicodeFlag" - Transforms Unicode flag (/u) syntax to ES5-compatible patterns
  • "dotAllFlag" - Transforms dot-all flag (/s) syntax to equivalent character classes
  • "unicodePropertyEscape" - Transforms Unicode property escapes (\p{...}) to character classes
  • "namedCaptureGroups" - Transforms named capture groups (?<name>...) with optional runtime helpers
  • "unicodeSetsFlag_syntax" - Legacy feature for backward compatibility (not used)
  • "unicodeSetsFlag" - Transforms Unicode sets flag (/v) syntax to Unicode flag (/u)
  • "duplicateNamedCaptureGroups" - Handles duplicate named capture groups across alternations
  • "modifiers" - Transforms inline regex modifiers (?flags:...) syntax

Types

// From @babel/core - Babel plugin object structure
interface PluginObject {
  name: string;
  manipulateOptions?: (opts: any, parserOpts: any) => void;
  pre?: (this: PluginPass) => void;
  post?: (this: PluginPass) => void;
  visitor: Visitor;
  inherits?: PluginObject | (() => PluginObject);
  parserOverride?: (code: string, opts: ParserOptions, parse: Function) => any;
  generatorOverride?: (ast: Node, opts: GeneratorOptions, code: string, generate: Function) => any;
}

// Plugin execution context
interface PluginPass {
  file: BabelFile;
  key: string;
  opts: any;
  cwd: string;
  filename: string | undefined;
  [key: string]: unknown;
}

// AST visitor pattern
interface Visitor {
  [key: string]: VisitNode<any> | VisitNode<any>[] | undefined;
}

type VisitNode<T> = VisitNodeFunction<any, T> | VisitNodeObject<any>;

interface VisitNodeFunction<S, P> {
  (this: S, path: NodePath<P>, state: S): void;
}

interface VisitNodeObject<S> {
  enter?(this: S, path: NodePath, state: S): void;
  exit?(this: S, path: NodePath, state: S): void;
}

Usage Examples

Complete Babel Plugin with Options

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

interface PluginOptions {
  useUnicodeFlag?: boolean;
}

export default declare((api, options: PluginOptions) => {
  api.assertVersion("^7.0.0");
  
  const { useUnicodeFlag = true } = options;
  if (typeof useUnicodeFlag !== "boolean") {
    throw new Error(".useUnicodeFlag must be a boolean, or undefined");
  }

  return createRegExpFeaturePlugin({
    name: "transform-unicode-property-regex",
    feature: "unicodePropertyEscape",
    options: { useUnicodeFlag },
  });
});

// Transforms: /\p{Letter}/u  →  /[compiled character class]/u

Named Capture Groups with Runtime Helper

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

interface PluginOptions {
  runtime?: boolean;
}

export default declare((api, options: PluginOptions) => {
  api.assertVersion("^7.0.0");
  
  const { runtime } = options;
  if (runtime !== undefined && typeof runtime !== "boolean") {
    throw new Error("The 'runtime' option must be boolean");
  }

  return createRegExpFeaturePlugin({
    name: "transform-named-capturing-groups-regex",
    feature: "namedCaptureGroups",
    options: { runtime },
  });
});

// Transforms: /(?<year>\d{4})/  →  runtime helper wrapper for named group access

Plugin with manipulateOptions

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
  api.assertVersion("^7.19.0");

  return createRegExpFeaturePlugin({
    name: "transform-unicode-sets-regex",
    feature: "unicodeSetsFlag",
    manipulateOptions(opts, parserOpts) {
      // Enable parser plugin for Unicode sets syntax
      parserOpts.plugins.push("regexpUnicodeSets");
    },
  });
});

// Transforms: /[\p{ASCII}&&[^\p{Cc}]]/v  →  /[compiled character class]/u

Simple Feature Plugin (No Options)

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
  api.assertVersion("^7.0.0");

  return createRegExpFeaturePlugin({
    name: "transform-dotall-regex",
    feature: "dotAllFlag"
  });
});

// Transforms: /./s  →  /[\s\S]/

Feature Transformation Details

Unicode Flag (unicodeFlag)

Transforms Unicode flag patterns to ES5-compatible syntax when options.useUnicodeFlag is false.

Input: /[𝒜]/u Output: /(?:(?:\uD835[\uDC9C-\uDCB5])|(?:\uD835[\uDCB6-\uDCCF]))/

Dot-All Flag (dotAllFlag)

Replaces the dot-all flag with equivalent character classes.

Input: /./s Output: /[\s\S]/ Input: /a.b/s Output: /a[\s\S]b/

Unicode Property Escapes (unicodePropertyEscape)

Compiles Unicode property escapes to character classes or ranges.

Input: /\p{Letter}/u Output: /[compiled character ranges]/u Input: /\p{Script=Greek}/u Output: /[\u0370-\u0373\u0375-\u0377...]/u

Named Capture Groups (namedCaptureGroups)

Transforms named capture groups with optional runtime helper for group access.

Input: /(?<year>\d{4})/ Output (runtime: false): /(\d{4})/ Input: /(?<year>\d{4})/ Output (runtime: true): _wrapRegExp(/(\d{4})/, {year: 1})

Unicode Sets Flag (unicodeSetsFlag)

Converts Unicode sets flag syntax to Unicode flag with compiled character classes.

Input: /[\p{ASCII}&&[^\p{Cc}]]/v Output: /[compiled intersection]/u

Duplicate Named Capture Groups (duplicateNamedCaptureGroups)

Handles regex patterns with duplicate named groups across alternations.

Input: /(?<n>a)|(?<n>b)/ Output: _wrapRegExp(/(a)|(b)/, {n: [1, 2]})

Regex Modifiers (modifiers)

Transforms inline modifier syntax to equivalent patterns.

Input: /(?i:hello)/ Output: /[Hh][Ee][Ll][Ll][Oo]/

Dependencies

This package depends on:

  • regexpu-core - Core regex transformation engine
  • @babel/helper-annotate-as-pure - Marks runtime helper calls as pure for optimizations
  • semver - Version comparison utilities

Error Handling

The plugin validates runtime option consistency across related features:

// This configuration will throw an error
const config = {
  plugins: [
    ["transform-named-capturing-groups-regex", { runtime: true }],
    ["transform-duplicate-named-capturing-groups-regex", { runtime: false }]
  ]
};
// Error: "The 'runtime' option must be the same for related plugins"

docs

index.md

tile.json