|
1 | | -export const browserGlobals = [ |
2 | | - 'document', |
3 | | - 'history', |
4 | | - 'location', |
5 | | - 'navigator', |
6 | | - 'window', |
7 | | -]; |
| 1 | +import { keywords } from './keywords'; |
8 | 2 |
|
9 | | -export const javaScriptGlobals = [ |
10 | | - 'console', |
11 | | - 'Array', |
12 | | - 'Date', |
13 | | - 'Error', |
14 | | - 'Function', |
15 | | - 'JSON', |
16 | | - 'Map', |
17 | | - 'Math', |
18 | | - 'Object', |
19 | | - 'Promise', |
20 | | - 'RegExp', |
21 | | - 'Set', |
22 | | - 'WeakMap', |
23 | | - 'WeakSet', |
24 | | -]; |
| 3 | +type List = ReadonlyArray<string>; |
25 | 4 |
|
26 | | -export const javaScriptKeywords = [ |
27 | | - 'arguments', |
28 | | - 'async', |
29 | | - 'await', |
30 | | - 'break', |
31 | | - 'case', |
32 | | - 'catch', |
33 | | - 'class', |
34 | | - 'const', |
35 | | - 'continue', |
36 | | - 'debugger', |
37 | | - 'default', |
38 | | - 'delete', |
39 | | - 'do', |
40 | | - 'else', |
41 | | - 'enum', |
42 | | - 'eval', |
43 | | - 'export', |
44 | | - 'extends', |
45 | | - 'false', |
46 | | - 'finally', |
47 | | - 'for', |
48 | | - 'from', |
49 | | - 'function', |
50 | | - 'if', |
51 | | - 'implements', |
52 | | - 'import', |
53 | | - 'in', |
54 | | - 'instanceof', |
55 | | - 'interface', |
56 | | - 'let', |
57 | | - 'new', |
58 | | - 'null', |
59 | | - 'package', |
60 | | - 'private', |
61 | | - 'protected', |
62 | | - 'public', |
63 | | - 'return', |
64 | | - 'static', |
65 | | - 'super', |
66 | | - 'switch', |
67 | | - 'this', |
68 | | - 'throw', |
69 | | - 'true', |
70 | | - 'try', |
71 | | - 'typeof', |
72 | | - 'var', |
73 | | - 'void', |
74 | | - 'while', |
75 | | - 'with', |
76 | | - 'yield', |
77 | | -]; |
| 5 | +export class ReservedList { |
| 6 | + private _array: List; |
| 7 | + private _set: Set<string>; |
78 | 8 |
|
79 | | -export const nodeGlobals = ['global', 'process', 'Buffer']; |
| 9 | + constructor(values: List) { |
| 10 | + this._array = values; |
| 11 | + this._set = new Set(values); |
| 12 | + } |
80 | 13 |
|
81 | | -export const typeScriptKeywords = [ |
82 | | - 'any', |
83 | | - 'as', |
84 | | - 'bigint', |
85 | | - 'boolean', |
86 | | - 'namespace', |
87 | | - 'never', |
88 | | - 'null', |
89 | | - 'number', |
90 | | - 'string', |
91 | | - 'symbol', |
92 | | - 'type', |
93 | | - 'undefined', |
94 | | - 'unknown', |
95 | | - 'void', |
96 | | -]; |
| 14 | + get '~values'() { |
| 15 | + return this._set; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Updates the reserved list with new values. |
| 20 | + * |
| 21 | + * @param values New reserved values or a function that receives the previous |
| 22 | + * reserved values and returns the new ones. |
| 23 | + */ |
| 24 | + set(values: List | ((prev: List) => List)): void { |
| 25 | + const vals = typeof values === 'function' ? values(this._array) : values; |
| 26 | + this._array = vals; |
| 27 | + this._set = new Set(vals); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const runtimeReserved = new ReservedList([ |
| 32 | + ...keywords.browserGlobals, |
| 33 | + ...keywords.javaScriptGlobals, |
| 34 | + ...keywords.javaScriptKeywords, |
| 35 | + ...keywords.nodeGlobals, |
| 36 | + ...keywords.typeScriptKeywords, |
| 37 | +]); |
| 38 | + |
| 39 | +const typeReserved = new ReservedList([ |
| 40 | + ...keywords.javaScriptKeywords, |
| 41 | + ...keywords.typeScriptKeywords, |
| 42 | +]); |
| 43 | + |
| 44 | +/** |
| 45 | + * Reserved names for identifiers. These names will not be used |
| 46 | + * for variables, functions, classes, or other identifiers in generated code. |
| 47 | + */ |
| 48 | +export const reserved = { |
| 49 | + /** |
| 50 | + * Reserved names for runtime identifiers. These names will not be used |
| 51 | + * for variables, functions, classes, or other runtime identifiers in |
| 52 | + * generated code. |
| 53 | + */ |
| 54 | + runtime: runtimeReserved, |
| 55 | + /** |
| 56 | + * Reserved names for type identifiers. These names will not be used |
| 57 | + * for type or interface identifiers in generated code. |
| 58 | + */ |
| 59 | + type: typeReserved, |
| 60 | +}; |
0 commit comments