Skip to main content

@babel/helper-validator-identifier

@babel/helper-validator-identifier 是一个用于解析 JavaScript 关键字和标识符的实用程序包。它提供了几个辅助函数来识别有效的标识符名称和检测保留字和关键字。

¥@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 install @babel/helper-validator-identifier

用法

¥Usage

要在你的代码中使用该包,请从 @babel/helper-validator-identifier 导入所需的函数:

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

my-babel-plugin.js
import {
isIdentifierName,
isIdentifierStart,
isIdentifierChar,
isReservedWord,
isStrictBindOnlyReservedWord,
isStrictBindReservedWord,
isStrictReservedWord,
isKeyword,
} from "@babel/helper-validator-identifier";

isIdentifierName

function isIdentifierName(name: string): boolean

isIdentifierName 函数检查给定字符串是否可以是有效的 标识符名称。请注意,它不处理 unicode 转义序列。例如,isIdentifierName("\\u0061") 返回 false,而 \u0061 可以是 JavaScript 标识符名称 (a)。

¥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

function isIdentifierStart(codepoint: number): boolean

isIdentifierStart 函数检查给定的 Unicode 代码点是否可以启动标识符,如 IdentifierStartChar 所定义。

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

isIdentifierChar

function isIdentifierChar(codepoint: number): boolean

isIdentifierChar 函数检查给定的 Unicode 代码点是否可以作为 IdentifierPartChar 定义的标识符的一部分。

¥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

这些助手检测 关键字和保留字。有关详细信息,请参阅 implementation

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

function isReservedWord(word: string, inModule: boolean): boolean
function isStrictReservedWord(word: string, inModule: boolean): boolean
function isStrictBindOnlyReservedWord(word: string): boolean
function isStrictBindReservedWord(word: string, inModule: boolean): boolean
function isKeyword(word: string): boolean