UNPKG

1.5 kBJavaScriptView Raw
1import {IS_IDENTIFIER_CHAR, IS_IDENTIFIER_START} from "../parser/util/identifier";
2
3// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
4// Hard-code a list of reserved words rather than trying to use keywords or contextual keywords
5// from the parser, since currently there are various exceptions, like `package` being reserved
6// but unused and various contextual keywords being reserved. Note that we assume that all code
7// compiled by Sucrase is in a module, so strict mode words and await are all considered reserved
8// here.
9const RESERVED_WORDS = new Set([
10 // Reserved keywords as of ECMAScript 2015
11 "break",
12 "case",
13 "catch",
14 "class",
15 "const",
16 "continue",
17 "debugger",
18 "default",
19 "delete",
20 "do",
21 "else",
22 "export",
23 "extends",
24 "finally",
25 "for",
26 "function",
27 "if",
28 "import",
29 "in",
30 "instanceof",
31 "new",
32 "return",
33 "super",
34 "switch",
35 "this",
36 "throw",
37 "try",
38 "typeof",
39 "var",
40 "void",
41 "while",
42 "with",
43 "yield",
44 // Future reserved keywords
45 "enum",
46 "implements",
47 "interface",
48 "let",
49 "package",
50 "private",
51 "protected",
52 "public",
53 "static",
54 "await",
55]);
56
57export default function isIdentifier(name) {
58 if (name.length === 0) {
59 return false;
60 }
61 if (!IS_IDENTIFIER_START[name.charCodeAt(0)]) {
62 return false;
63 }
64 for (let i = 1; i < name.length; i++) {
65 if (!IS_IDENTIFIER_CHAR[name.charCodeAt(i)]) {
66 return false;
67 }
68 }
69 return !RESERVED_WORDS.has(name);
70}