22
22
* along with this program; if not, write to the Free Software
23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
24
25
-
26
- type element = string
27
-
28
- let rec binarySearchAux (arr : element array ) (lo : int ) (hi : int ) key : bool =
29
- let mid = (lo + hi)/ 2 in
30
- let midVal = Array. unsafe_get arr mid in
31
- if key = midVal then true
32
- else if key < midVal then (* a[lo] =< key < a[mid] <= a[hi] *)
33
- if hi = mid then
34
- (Array. unsafe_get arr lo) = key
35
- else binarySearchAux arr lo mid key
36
- else (* a[lo] =< a[mid] < key <= a[hi] *)
37
- if lo = mid then
38
- (Array. unsafe_get arr hi) = key
39
- else binarySearchAux arr mid hi key
40
-
41
- let binarySearch (key : element ) (sorted : element array ) : bool =
42
- let len = Array. length sorted in
43
- if len = 0 then false
44
- else
45
- let lo = Array. unsafe_get sorted 0 in
46
- if key < lo then false
47
- else
48
- let hi = Array. unsafe_get sorted (len - 1 ) in
49
- if key > hi then false
50
- else binarySearchAux sorted 0 (len - 1 ) key
25
+ module SSet = Set. Make (String )
51
26
52
27
(* * Words that can never be identifier's name.
53
28
54
29
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
55
30
*)
56
- let sorted_js_keywords = [|
57
- " await" ;
31
+ let js_keywords = SSet. of_list [
58
32
" break" ;
59
33
" case" ;
60
34
" catch" ;
@@ -66,28 +40,19 @@ let sorted_js_keywords = [|
66
40
" delete" ;
67
41
" do" ;
68
42
" else" ;
69
- " enum" ;
70
43
" export" ;
71
44
" extends" ;
72
45
" false" ;
73
46
" finally" ;
74
47
" for" ;
75
48
" function" ;
76
49
" if" ;
77
- " implements" ;
78
50
" import" ;
79
51
" in" ;
80
52
" instanceof" ;
81
- " interface" ;
82
- " let" ;
83
53
" new" ;
84
54
" null" ;
85
- " package" ;
86
- " private" ;
87
- " protected" ;
88
- " public" ;
89
55
" return" ;
90
- " static" ;
91
56
" super" ;
92
57
" switch" ;
93
58
" this" ;
@@ -99,10 +64,23 @@ let sorted_js_keywords = [|
99
64
" void" ;
100
65
" while" ;
101
66
" with" ;
67
+ (* The following are also reserved in strict context, including ESM *)
68
+ " let" ;
69
+ " static" ;
102
70
" yield" ;
103
- |]
71
+ (* `await` is reserved in async context, including ESM *)
72
+ " await" ;
73
+ (* Future reserved words *)
74
+ " enum" ;
75
+ " implements" ;
76
+ " interface" ;
77
+ " package" ;
78
+ " private" ;
79
+ " protected" ;
80
+ " public" ;
81
+ ]
104
82
105
- let is_js_keyword s = binarySearch s sorted_js_keywords
83
+ let is_js_keyword s = SSet. exists ( fun x -> x = s) js_keywords
106
84
107
85
(* * Identifiers with special meanings.
108
86
@@ -112,7 +90,7 @@ let is_js_keyword s = binarySearch s sorted_js_keywords
112
90
113
91
However, these names are actually used with no problems today. Preventing this can be annoying.
114
92
*)
115
- let sorted_js_special_words = [|
93
+ let js_special_words = SSet. of_list [
116
94
" arguments" ;
117
95
" as" ;
118
96
" async" ;
@@ -121,12 +99,15 @@ let sorted_js_special_words = [|
121
99
" get" ;
122
100
" of" ;
123
101
" set" ;
124
- | ]
102
+ ]
125
103
126
- let is_js_special_word s = binarySearch s sorted_js_special_words
104
+ let is_js_special_word s = SSet. exists ( fun x -> x = s) js_special_words
127
105
128
106
(* * Identifier names _might_ need to care about *)
129
- let sorted_js_globals = [|
107
+ let js_globals = SSet. of_list [
108
+ (* JavaScript standards built-ins
109
+ See https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects
110
+ *)
130
111
" AggregateError" ;
131
112
" Array" ;
132
113
" ArrayBuffer" ;
@@ -139,11 +120,14 @@ let sorted_js_globals = [|
139
120
" BigInt64Array" ;
140
121
" BigUint64Array" ;
141
122
" Boolean" ;
142
- " Bun" ;
143
123
" DataView" ;
144
124
" Date" ;
145
- " Deno" ;
125
+ " decodeURI" ;
126
+ " decodeURIComponent" ;
127
+ " encodeURI" ;
128
+ " encodeURIComponent" ;
146
129
" Error" ;
130
+ " eval" ;
147
131
" EvalError" ;
148
132
" FinalizationRegistry" ;
149
133
" Float16Array" ;
@@ -152,18 +136,23 @@ let sorted_js_globals = [|
152
136
" Function" ;
153
137
" Generator" ;
154
138
" GeneratorFunction" ;
139
+ " globalThis" ;
155
140
" Infinity" ;
156
141
" Int16Array" ;
157
142
" Int32Array" ;
158
143
" Int8Array" ;
159
144
" Intl" ;
145
+ " isFinite" ;
146
+ " isNaN" ;
160
147
" Iterator" ;
161
148
" JSON" ;
162
149
" Map" ;
163
150
" Math" ;
164
151
" NaN" ;
165
152
" Number" ;
166
153
" Object" ;
154
+ " parseFloat" ;
155
+ " parseInt" ;
167
156
" Promise" ;
168
157
" Proxy" ;
169
158
" RangeError" ;
@@ -175,32 +164,49 @@ let sorted_js_globals = [|
175
164
" String" ;
176
165
" Symbol" ;
177
166
" SyntaxError" ;
178
- " TypeError" ;
179
167
" TypedArray" ;
180
- " URIError " ;
168
+ " TypeError " ;
181
169
" Uint16Array" ;
182
170
" Uint32Array" ;
183
171
" Uint8Array" ;
184
172
" Uint8ClampedArray" ;
173
+ " undefined" ;
174
+ " URIError" ;
185
175
" WeakMap" ;
186
176
" WeakRef" ;
187
177
" WeakSet" ;
178
+
179
+ (* A few of the HTML standard globals
180
+
181
+ See https://developer.mozilla.org/en-US/docs/Web/API/Window
182
+ See https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope
183
+
184
+ But we don't actually need to protect these names.
185
+
186
+ "window";
187
+ "self";
188
+ "document";
189
+ "location";
190
+ "navigator";
191
+ "origin";
192
+ *)
193
+
194
+ (* A few of the Node.js globals
195
+
196
+ Specifically related to the CommonJS module system
197
+ They cannot be redeclared in nested scope.
198
+ *)
188
199
" __dirname" ;
189
200
" __filename" ;
190
- " decodeURI" ;
191
- " decodeURIComponent" ;
192
- " encodeURI" ;
193
- " encodeURIComponent" ;
194
- " eval" ;
195
- " exports" ;
196
- " globalThis" ;
197
- " isFinite" ;
198
- " isNaN" ;
199
- " module" ;
200
- " parseFloat" ;
201
- " parseInt" ;
202
201
" require" ;
203
- " undefined" ;
204
- |]
202
+ " module" ;
203
+ " exports" ;
204
+
205
+ (* Bun's global namespace *)
206
+ " Bun" ;
207
+
208
+ (* Deno's global namespace *)
209
+ " Deno" ;
210
+ ]
205
211
206
- let is_js_global s = binarySearch s sorted_js_globals
212
+ let is_js_global s = SSet. exists ( fun x -> x = s) js_globals
0 commit comments