forked from babel/babel
/
keyword.js
98 lines (92 loc) 路 1.97 KB
/
keyword.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// @flow
const reservedWords = {
keyword: [
"break",
"case",
"catch",
"continue",
"debugger",
"default",
"do",
"else",
"finally",
"for",
"function",
"if",
"return",
"switch",
"throw",
"try",
"var",
"const",
"while",
"with",
"new",
"this",
"super",
"class",
"extends",
"export",
"import",
"null",
"true",
"false",
"in",
"instanceof",
"typeof",
"void",
"delete",
],
strict: [
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
],
strictBind: ["eval", "arguments"],
};
const keywords = new Set(reservedWords.keyword);
const reservedWordsStrictSet = new Set(reservedWords.strict);
const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
/**
* Checks if word is a reserved word in non-strict mode
*/
export const isReservedWord = (word: string, inModule: boolean): boolean => {
return (inModule && word === "await") || word === "enum";
};
/**
* Checks if word is a reserved word in non-binding strict mode
*
* Includes non-strict reserved words
*/
export function isStrictReservedWord(word: string, inModule: boolean): boolean {
return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
}
/**
* Checks if word is a reserved word in binding strict mode, but it is allowed as
* a normal identifier.
*/
export function isStrictBindOnlyReservedWord(word: string): boolean {
return reservedWordsStrictBindSet.has(word);
}
/**
* Checks if word is a reserved word in binding strict mode
*
* Includes non-strict reserved words and non-binding strict reserved words
*/
export function isStrictBindReservedWord(
word: string,
inModule: boolean,
): boolean {
return (
isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word)
);
}
export function isKeyword(word: string): boolean {
return keywords.has(word);
}