or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-syntax-unicode-sets-regex

Babel plugin that enables parsing of regular expressions with the unicodeSets (v) flag for enhanced Unicode property matching.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-syntax-unicode-sets-regex@7.18.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-syntax-unicode-sets-regex@7.18.0

index.mddocs/

@babel/plugin-syntax-unicode-sets-regex

@babel/plugin-syntax-unicode-sets-regex is a Babel syntax plugin that enables parsing of regular expressions using the unicodeSets (v) flag. This flag provides enhanced Unicode property matching capabilities in JavaScript regular expressions, including intersection, subtraction, and string literals within character classes.

Package Information

  • Package Name: @babel/plugin-syntax-unicode-sets-regex
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-syntax-unicode-sets-regex

Core Imports

// Default import (most common)
import plugin from "@babel/plugin-syntax-unicode-sets-regex";

For CommonJS:

const plugin = require("@babel/plugin-syntax-unicode-sets-regex");

Basic Usage

This plugin is typically configured in your Babel configuration file to enable parsing of unicode sets regex syntax:

// babel.config.js
module.exports = {
  plugins: [
    "@babel/plugin-syntax-unicode-sets-regex"
  ]
};

With this plugin enabled, Babel can parse regular expressions with the v flag:

// These regex patterns become parseable with the plugin
const regex1 = /[[0-7]&&[5-9]]/v;  // Intersection
const regex2 = /[[a-z]--[aeiou]]/v; // Subtraction  
const regex3 = /[\p{ASCII}--\q{abc|def}]/v; // String literals

Architecture

This plugin is a syntax-only plugin that:

  • Extends Babel's parser to recognize the v flag in regular expressions
  • Adds the "regexpUnicodeSets" parser plugin to enable unicodeSets syntax
  • Does not transform the regex patterns (use alongside transformation plugins for runtime support)
  • Leverages Babel's regexp features plugin system for consistent behavior

Capabilities

Plugin Factory Function

Main plugin factory that creates a Babel plugin for unicodeSets regex syntax parsing.

/**
 * Default export function that creates a Babel plugin for parsing unicodeSets regex syntax
 * @param api - Babel's plugin API providing version checking and utilities
 * @returns Babel plugin object configured for unicodeSets syntax support
 */
export default declare((api: PluginAPI) => PluginObject);

interface PluginAPI {
  version: string;
  assertVersion(version: number | string): void;
  cache: Map<any, any>;
  env: (envName?: string) => string;
  caller: (callback: (caller: any) => boolean) => boolean;
}

interface PluginObject {
  name: string;
  manipulateOptions: (opts: any, parserOpts: ParserOptions) => void;
}

The plugin function:

  • Calls api.assertVersion(7) to ensure Babel 7+ compatibility
  • Uses createRegExpFeaturePlugin() from @babel/helper-create-regexp-features-plugin
  • Configures the plugin with name "syntax-unicode-sets-regex" and feature "unicodeSetsFlag_syntax"
  • Adds "regexpUnicodeSets" to the parser plugins via manipulateOptions

Parser Integration

The plugin integrates with Babel's parser through the manipulateOptions function:

/**
 * Babel plugin manipulation options function that adds regexpUnicodeSets parser support
 * @param opts - Babel compilation options
 * @param parserOpts - Parser-specific options including plugins array
 */
manipulateOptions(opts: any, parserOpts: ParserOptions): void;

interface ParserOptions {
  plugins: string[];
  sourceType?: "module" | "script";
  allowImportExportEverywhere?: boolean;
  allowReturnOutsideFunction?: boolean;
}

This function adds "regexpUnicodeSets" to parserOpts.plugins array, enabling the parser to recognize and process regex literals with the v flag.

Types

/**
 * Babel Plugin API interface providing core plugin development utilities
 */
interface PluginAPI {
  /** Babel version string */
  version: string;
  /** Assert that Babel version meets requirements */
  assertVersion(version: number | string): void;
  /** Plugin cache for storing data between compilation runs */
  cache: Map<any, any>;
  /** Get current environment (development, production, test) */
  env: (envName?: string) => string;
  /** Check properties of the build tool calling Babel */
  caller: (callback: (caller: any) => boolean) => boolean;
}

/**
 * Babel Plugin Object structure returned by plugin factory functions
 */
interface PluginObject {
  /** Plugin name for debugging and error reporting */
  name: string;
  /** Function to modify parser options before parsing begins */
  manipulateOptions: (opts: any, parserOpts: ParserOptions) => void;
}

/**
 * Parser options that control how Babel parses source code
 */
interface ParserOptions {
  /** Array of parser plugins to enable during parsing */
  plugins: string[];
  /** Type of source code being parsed */
  sourceType?: "module" | "script";
  /** Allow import/export statements anywhere in the code */
  allowImportExportEverywhere?: boolean;
  /** Allow return statements outside of functions */
  allowReturnOutsideFunction?: boolean;
}

Unicode Sets Regex Features

When this plugin is enabled, the following Unicode Sets regex syntax becomes available:

Intersection

/[[0-7]&&[5-9]]/v  // Matches: 5, 6, 7

Subtraction

/[[a-z]--[aeiou]]/v  // Matches consonants only

String Literals in Character Classes

/[\q{abc|def}]/v  // Matches strings "abc" or "def"

Enhanced Unicode Properties

/[\p{ASCII}--\q{emoji}]/v  // ASCII characters excluding emoji strings

Error Handling

The plugin includes built-in error handling:

  • Version Compatibility: Throws error if Babel version is below 7.0
  • Parser Errors: Babel's parser will throw syntax errors for malformed regex patterns
  • Plugin Loading: Standard Babel plugin loading error handling applies

Dependencies

This plugin depends on:

  • @babel/helper-create-regexp-features-plugin: Provides the createRegExpFeaturePlugin factory
  • @babel/helper-plugin-utils: Provides the declare utility for plugin creation
  • @babel/core: Peer dependency providing the Babel transformation environment