@babel/helper-validator-identifier

@babel/helper-validator-identifier is a utility package for parsing JavaScript keywords and identifiers. It provides several helper functions for identifying valid identifier names and detecting reserved words and keywords.

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install @babel/helper-validator-identifier
  1. yarn add @babel/helper-validator-identifier
  1. pnpm add @babel/helper-validator-identifier

Usage

To use the package in your code, import the required functions from @babel/helper-validator-identifier:

my-babel-plugin.js

  1. import {
  2. isIdentifierName,
  3. isIdentifierStart,
  4. isIdentifierChar,
  5. isReservedWord,
  6. isStrictBindOnlyReservedWord,
  7. isStrictBindReservedWord,
  8. isStrictReservedWord,
  9. isKeyword,
  10. } from "@babel/helper-validator-identifier";

isIdentifierName

  1. function isIdentifierName(name: string): boolean

The isIdentifierName function checks whether a given string can be a valid identifier name. Note that it doesn’t handle unicode escape sequences. For example, isIdentifierName("\\u0061") returns false while \u0061 could be an JavaScript identifier name (a).

isIdentifierStart

  1. function isIdentifierStart(codepoint: number): boolean

The isIdentifierStart function checks whether a given Unicode code point can start an identifier, as defined by the IdentifierStartChar.

isIdentifierChar

  1. function isIdentifierChar(codepoint: number): boolean

The isIdentifierChar function checks whether a given Unicode code point can be part of an identifier, as defined by the IdentifierPartChar.

Keywords and Reserved words helpers

These helpers detect keyword and reserved words. For more information, see the implementation.

  1. function isReservedWord(word: string, inModule: boolean): boolean
  2. function isStrictReservedWord(word: string, inModule: boolean): boolean
  3. function isStrictBindOnlyReservedWord(word: string): boolean
  4. function isStrictBindReservedWord(word: string, inModule: boolean): boolean
  5. function isKeyword(word: string): boolean