跳至主内容

@babel/helper-validator-identifier

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

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

安装

npm install @babel/helper-validator-identifier

用法

要在代码中使用此包,请从 @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 函数用于检测给定字符串是否符合 identifier name 规范。注意:该函数不处理 Unicode 转义序列。例如 isIdentifierName("\\u0061") 返回 false,而 \u0061 实际可表示 JavaScript 标识符名称 a

isIdentifierStart

function isIdentifierStart(codepoint: number): boolean

isIdentifierStart 函数根据 IdentifierStartChar 规范,检测给定 Unicode 码点能否作为标识符起始字符。

isIdentifierChar

function isIdentifierChar(codepoint: number): boolean

isIdentifierChar 函数根据 IdentifierPartChar 规范,检测给定 Unicode 码点能否作为标识符组成部分。

关键字与保留字辅助函数

这些辅助函数用于检测 keyword and reserved words,具体实现可参考 源代码

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