跳至主要內容

@babel/helper-validator-identifier

@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 函式檢查指定的字串是否可以成為有效的 識別碼名稱。請注意,它不處理 Unicode 逸出序列。例如,isIdentifierName("\\u0061") 傳回 false,而 \u0061 可以是 JavaScript 識別碼名稱 (a)。

isIdentifierStart

function isIdentifierStart(codepoint: number): boolean

isIdentifierStart 函式檢查指定的 Unicode 編碼點是否可以開始識別碼,如 IdentifierStartChar 所定義。

isIdentifierChar

function isIdentifierChar(codepoint: number): boolean

isIdentifierChar 函式檢查指定的 Unicode 編碼點是否可以成為識別碼的一部分,如 IdentifierPartChar 所定義。

關鍵字和保留字幫手

這些幫手會偵測關鍵字和保留字。如需更多資訊,請參閱實作

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