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 checking and manipulating RegExp flags in JavaScript AST nodes

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

To install, run

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

index.mddocs/

@babel/helper-regex

@babel/helper-regex is a utility library that provides functions for working with regular expression literals in JavaScript AST nodes. It offers two core functions for checking and manipulating regex flags in RegExpLiteral AST nodes, designed as a helper utility for Babel transformations and other JavaScript AST manipulation tools.

Package Information

  • Package Name: @babel/helper-regex
  • Package Type: npm
  • Language: JavaScript (with Flow type annotations)
  • Installation: npm install --save-dev @babel/helper-regex

Core Imports

import { is, pullFlag } from "@babel/helper-regex";

For CommonJS:

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

Basic Usage

import { is, pullFlag } from "@babel/helper-regex";

// Check if a RegExpLiteral node has a specific flag
const regexNode = {
  type: "RegExpLiteral",
  pattern: "test",
  flags: "gim"
};

const hasGlobalFlag = is(regexNode, "g"); // true
const hasYFlag = is(regexNode, "y");      // false

// Remove a flag from a RegExpLiteral node
pullFlag(regexNode, "i");
console.log(regexNode.flags);  // "gm" (the "i" flag was removed)

Capabilities

Flag Checking

Check if a RegExpLiteral AST node contains a specific flag.

/**
 * Check if a node represents a RegExpLiteral with a specific flag
 * @param node - AST node to check (expected to be RegExpLiteral)
 * @param flag - Regular expression flag to look for (e.g., "g", "i", "m")
 * @returns true if node is RegExpLiteral and contains the specified flag
 */
function is(node: Object, flag: string): boolean;

Usage Example:

import { is } from "@babel/helper-regex";

const regexNode = {
  type: "RegExpLiteral",
  pattern: "hello",
  flags: "gi"
};

// Check for different flags
console.log(is(regexNode, "g")); // true - has global flag
console.log(is(regexNode, "i")); // true - has case-insensitive flag  
console.log(is(regexNode, "m")); // false - no multiline flag

// Non-RegExpLiteral nodes return false
const stringNode = { type: "StringLiteral", value: "test" };
console.log(is(stringNode, "g")); // false

Flag Removal

Remove a specific flag from a RegExpLiteral node's flags string, modifying the node in-place.

/**
 * Remove a specific flag from a RegExpLiteral node's flags string
 * @param node - AST node to modify (expected to be RegExpLiteral)
 * @param flag - Regular expression flag to remove (e.g., "g", "i", "m")
 * @returns void (modifies the node in-place)
 */
function pullFlag(node: Object, flag: string): void;

Usage Example:

import { pullFlag } from "@babel/helper-regex";

const regexNode = {
  type: "RegExpLiteral",
  pattern: "test",
  flags: "gim"
};

// Remove specific flags
pullFlag(regexNode, "i");
console.log(regexNode.flags); // "gm"

pullFlag(regexNode, "g");
console.log(regexNode.flags); // "m"

// Removing non-existent flag has no effect
pullFlag(regexNode, "y");
console.log(regexNode.flags); // "m" (unchanged)

AST Node Requirements

Both functions expect AST nodes with the following structure:

interface RegExpLiteralNode {
  type: "RegExpLiteral";
  pattern: string;
  flags: string;
}

The functions are designed to work with JavaScript AST nodes that represent regular expression literals. The is() function validates the node type before checking flags, while pullFlag() assumes the node has the correct structure and will modify the flags property directly.

Error Handling

  • is() returns false for nodes that are not of type "RegExpLiteral"
  • is() returns false if the specified flag is not present in the flags string
  • pullFlag() returns early (no-op) if the specified flag is not present in the flags string
  • Both functions expect the node to have a flags property as a string

Dependencies

  • lodash: Uses lodash/pull for array manipulation during flag removal