or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-helper-regex

Helper functions for working with regular expression literals in Babel transformations

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-helper-regex@6.26.x

To install, run

npx @tessl/cli install tessl/npm-babel-helper-regex@6.26.0

index.mddocs/

Babel Helper Regex

Babel Helper Regex provides utility functions for working with regular expression literals in Babel transformations. It operates on Babel AST nodes representing RegExp literals and helps with flag manipulation during JavaScript transpilation workflows.

Package Information

  • Package Name: babel-helper-regex
  • Package Type: npm
  • Language: JavaScript (with Flow types in source)
  • Installation: npm install babel-helper-regex

Core Imports

ES6 modules:

import * as regex from "babel-helper-regex";
import { is, pullFlag } from "babel-helper-regex";

CommonJS:

const regex = require("babel-helper-regex");
const { is, pullFlag } = require("babel-helper-regex");

Basic Usage

import * as regex from "babel-helper-regex";
import * as t from "babel-types";

// Example: Check if a RegExp literal has the unicode flag
export default function () {
  return {
    visitor: {
      RegExpLiteral({ node }) {
        // Check if node has unicode flag
        if (regex.is(node, "u")) {
          // Process the node with unicode flag
          console.log("Found unicode regex:", node.pattern);
          
          // Remove the unicode flag after processing
          regex.pullFlag(node, "u");
        }
      }
    }
  };
}

Capabilities

Flag Detection

Checks if a RegExp literal node contains a specific flag.

/**
 * Checks if a node is a RegExp literal with a specific flag
 * @param {Object} node - Babel AST node to check (must be RegExp literal)
 * @param {string} flag - The flag to look for ('g', 'i', 'm', 's', 'u', 'y')
 * @returns {boolean} true if the node is a RegExp literal and contains the specified flag
 */
function is(node, flag);

Usage Examples:

import * as regex from "babel-helper-regex";
import * as t from "babel-types";

// Create a RegExp literal node for testing
const regexNode = t.regExpLiteral("test", "gui");

// Check for different flags
console.log(regex.is(regexNode, "g")); // true
console.log(regex.is(regexNode, "u")); // true  
console.log(regex.is(regexNode, "y")); // false

// Works with any valid RegExp flags
console.log(regex.is(regexNode, "i")); // true (ignoreCase)
console.log(regex.is(regexNode, "m")); // false (multiline)
console.log(regex.is(regexNode, "s")); // false (dotAll)

Flag Removal

Removes a flag from a RegExp literal node by mutating the node in place.

/**
 * Removes a flag from a RegExp literal node
 * @param {Object} node - Babel AST node representing a RegExp literal
 * @param {string} flag - The flag to remove ('g', 'i', 'm', 's', 'u', 'y')
 * @returns {void} Mutates the node.flags property in place
 */
function pullFlag(node, flag);

Usage Examples:

import * as regex from "babel-helper-regex";
import * as t from "babel-types";

// Create a RegExp literal with multiple flags
const regexNode = t.regExpLiteral("pattern", "guiy");
console.log(regexNode.flags); // "guiy"

// Remove the unicode flag
regex.pullFlag(regexNode, "u");
console.log(regexNode.flags); // "giy"

// Remove global flag
regex.pullFlag(regexNode, "g");
console.log(regexNode.flags); // "iy"

// Removing non-existent flag does nothing
regex.pullFlag(regexNode, "m");
console.log(regexNode.flags); // "iy" (unchanged)

Types

/**
 * Babel AST node representing a regular expression literal
 * This is provided by babel-types package
 */
interface RegExpLiteral {
  type: "RegExpLiteral";
  pattern: string;    // The regex pattern without delimiters
  flags: string;      // String containing flag characters (e.g., "gui")
}

/**
 * Valid RegExp flag characters
 */
type RegExpFlag = "g" | "i" | "m" | "s" | "u" | "y";

Error Handling

Both functions expect valid Babel AST nodes:

  • is() returns false if the node is not a RegExp literal (uses babel-types.isRegExpLiteral() internally)
  • pullFlag() will have no effect if the flag doesn't exist in the node's flags
  • Both functions assume the node parameter is a valid object with a flags property when it is a RegExp literal

Dependencies

This package requires:

  • babel-types: For AST node type checking and manipulation
  • lodash: Specifically lodash/pull for array manipulation during flag removal
  • babel-runtime: Babel runtime helpers

When using this package, ensure these dependencies are available in your project.