main
ts 87 lines 1.7 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 /**
9 * https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-keywords-and-reserved-words
10 */
11
12 /**
13 * Note: `await` and `yield` are contextually allowed as identifiers.
14 * await: reserved inside async functions and modules
15 * yield: reserved inside generator functions
16 *
17 * Note: `async` is not reserved.
18 */
19 const RESERVED_WORDS = new Set([
20 'break',
21 'case',
22 'catch',
23 'class',
24 'const',
25 'continue',
26 'debugger',
27 'default',
28 'delete',
29 'do',
30 'else',
31 'enum',
32 'export',
33 'extends',
34 'false',
35 'finally',
36 'for',
37 'function',
38 'if',
39 'import',
40 'in',
41 'instanceof',
42 'new',
43 'null',
44 'return',
45 'super',
46 'switch',
47 'this',
48 'throw',
49 'true',
50 'try',
51 'typeof',
52 'var',
53 'void',
54 'while',
55 'with',
56 ]);
57
58 /**
59 * Reserved when a module has a 'use strict' directive.
60 */
61 const STRICT_MODE_RESERVED_WORDS = new Set([
62 'let',
63 'static',
64 'implements',
65 'interface',
66 'package',
67 'private',
68 'protected',
69 'public',
70 ]);
71 /**
72 * The names arguments and eval are not keywords, but they are subject to some restrictions in
73 * strict mode code.
74 */
75 const STRICT_MODE_RESTRICTED_WORDS = new Set(['eval', 'arguments']);
76
77 /**
78 * Conservative check for whether an identifer name is reserved or not. We assume that code is
79 * written with strict mode.
80 */
81 export function isReservedWord(identifierName: string): boolean {
82 return (
83 RESERVED_WORDS.has(identifierName) ||
84 STRICT_MODE_RESERVED_WORDS.has(identifierName) ||
85 STRICT_MODE_RESTRICTED_WORDS.has(identifierName)
86 );
87 }