| 1 |
import { |
| 2 |
isIdentifierName, |
| 3 |
isStrictReservedWord, |
| 4 |
isKeyword, |
| 5 |
} from "@babel/helper-validator-identifier"; |
| 6 |
|
| 7 |
/** |
| 8 |
* Check if the input `name` is a valid identifier name |
| 9 |
* and isn't a reserved word. |
| 10 |
*/ |
| 11 |
export default function isValidIdentifier( |
| 12 |
name: string, |
| 13 |
reserved: boolean = true, |
| 14 |
): boolean { |
| 15 |
if (typeof name !== "string") return false; |
| 16 |
|
| 17 |
if (reserved) { |
| 18 |
// "await" is invalid in module, valid in script; better be safe (see #4952) |
| 19 |
if (isKeyword(name) || isStrictReservedWord(name, true)) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return isIdentifierName(name); |
| 25 |
} |