Skip to main content

@babel/helper-validator-identifier

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

安装​

🌐 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

这些辅助工具可以检测 关键字和保留字。更多信息,请参见 实现。

🌐 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