@@ -8,6 +8,8 @@ pub(super) mod scope;
8
8
9
9
#[ derive( Debug , Default ) ]
10
10
pub ( super ) struct Analyzer {
11
+ pub safari_10 : bool ,
12
+
11
13
pub is_pat_decl : bool ,
12
14
pub var_belong_to_fn_scope : bool ,
13
15
pub in_catch_params : bool ,
@@ -42,6 +44,8 @@ impl Analyzer {
42
44
{
43
45
{
44
46
let mut v = Analyzer {
47
+ safari_10 : self . safari_10 ,
48
+
45
49
scope : Scope {
46
50
kind,
47
51
..Default :: default ( )
@@ -121,21 +125,56 @@ impl Visit for Analyzer {
121
125
}
122
126
}
123
127
128
+ fn visit_block_stmt ( & mut self , n : & BlockStmt ) {
129
+ self . with_scope ( ScopeKind :: Block , |v| n. visit_children_with ( v) )
130
+ }
131
+
132
+ fn visit_block_stmt_or_expr ( & mut self , n : & BlockStmtOrExpr ) {
133
+ match n {
134
+ // This avoid crating extra block scope for arrow function
135
+ BlockStmtOrExpr :: BlockStmt ( n) => n. visit_children_with ( self ) ,
136
+ BlockStmtOrExpr :: Expr ( n) => n. visit_with ( self ) ,
137
+ }
138
+ }
139
+
124
140
fn visit_catch_clause ( & mut self , n : & CatchClause ) {
125
- self . with_scope ( ScopeKind :: Block , |v| {
126
- let old = v . is_pat_decl ;
127
- let old_in_catch_params = v . in_catch_params ;
141
+ if self . safari_10 {
142
+ let old_is_pat_decl = self . is_pat_decl ;
143
+ let old_in_catch_params = self . in_catch_params ;
128
144
129
- v. is_pat_decl = false ;
130
- n. body . visit_children_with ( v) ;
145
+ self . is_pat_decl = true ;
146
+ self . in_catch_params = true ;
147
+ n. param . visit_with ( self ) ;
131
148
132
- v. is_pat_decl = true ;
133
- v. in_catch_params = true ;
134
- n. param . visit_with ( v) ;
149
+ self . in_catch_params = old_in_catch_params;
150
+ self . is_pat_decl = old_is_pat_decl;
135
151
136
- v. is_pat_decl = old;
137
- v. in_catch_params = old_in_catch_params;
138
- } )
152
+ self . with_scope ( ScopeKind :: Block , |v| {
153
+ let old = v. is_pat_decl ;
154
+ let old_in_catch_params = v. in_catch_params ;
155
+
156
+ v. is_pat_decl = false ;
157
+ n. body . visit_children_with ( v) ;
158
+
159
+ v. is_pat_decl = old;
160
+ v. in_catch_params = old_in_catch_params;
161
+ } )
162
+ } else {
163
+ self . with_scope ( ScopeKind :: Block , |v| {
164
+ let old = v. is_pat_decl ;
165
+ let old_in_catch_params = v. in_catch_params ;
166
+
167
+ v. is_pat_decl = false ;
168
+ n. body . visit_children_with ( v) ;
169
+
170
+ v. is_pat_decl = true ;
171
+ v. in_catch_params = true ;
172
+ n. param . visit_with ( v) ;
173
+
174
+ v. is_pat_decl = old;
175
+ v. in_catch_params = old_in_catch_params;
176
+ } )
177
+ }
139
178
}
140
179
141
180
fn visit_class_decl ( & mut self , c : & ClassDecl ) {
@@ -251,6 +290,40 @@ impl Visit for Analyzer {
251
290
}
252
291
}
253
292
293
+ fn visit_for_in_stmt ( & mut self , n : & ForInStmt ) {
294
+ self . with_scope ( ScopeKind :: Block , |v| {
295
+ n. left . visit_with ( v) ;
296
+ n. right . visit_with ( v) ;
297
+
298
+ v. with_scope ( ScopeKind :: Block , |v| {
299
+ v. visit_for_body_within_same_scope ( & n. body ) ;
300
+ } )
301
+ } ) ;
302
+ }
303
+
304
+ fn visit_for_of_stmt ( & mut self , n : & ForOfStmt ) {
305
+ self . with_scope ( ScopeKind :: Block , |v| {
306
+ n. left . visit_with ( v) ;
307
+ n. right . visit_with ( v) ;
308
+
309
+ v. with_scope ( ScopeKind :: Block , |v| {
310
+ v. visit_for_body_within_same_scope ( & n. body ) ;
311
+ } )
312
+ } ) ;
313
+ }
314
+
315
+ fn visit_for_stmt ( & mut self , n : & ForStmt ) {
316
+ self . with_scope ( ScopeKind :: Block , |v| {
317
+ n. init . visit_with ( v) ;
318
+ n. test . visit_with ( v) ;
319
+ n. update . visit_with ( v) ;
320
+
321
+ v. with_scope ( ScopeKind :: Block , |v| {
322
+ v. visit_for_body_within_same_scope ( & n. body ) ;
323
+ } )
324
+ } ) ;
325
+ }
326
+
254
327
fn visit_import_default_specifier ( & mut self , n : & ImportDefaultSpecifier ) {
255
328
self . add_decl ( n. local . to_id ( ) , true ) ;
256
329
}
@@ -326,77 +399,31 @@ impl Visit for Analyzer {
326
399
}
327
400
}
328
401
402
+ fn visit_static_block ( & mut self , n : & StaticBlock ) {
403
+ self . with_fn_scope ( |v| n. body . visit_children_with ( v) )
404
+ }
405
+
329
406
fn visit_super_prop_expr ( & mut self , e : & SuperPropExpr ) {
330
407
if let SuperProp :: Computed ( c) = & e. prop {
331
408
c. visit_with ( self ) ;
332
409
}
333
410
}
334
411
335
- fn visit_var_declarator ( & mut self , v : & VarDeclarator ) {
336
- let old = self . is_pat_decl ;
337
- self . is_pat_decl = true ;
338
- v. name . visit_with ( self ) ;
339
-
340
- self . is_pat_decl = false ;
341
- v. init . visit_with ( self ) ;
342
-
343
- self . is_pat_decl = old;
344
- }
345
-
346
412
fn visit_var_decl ( & mut self , n : & VarDecl ) {
347
413
let old_need_hoisted = self . var_belong_to_fn_scope ;
348
414
self . var_belong_to_fn_scope = n. kind == VarDeclKind :: Var ;
349
415
n. visit_children_with ( self ) ;
350
416
self . var_belong_to_fn_scope = old_need_hoisted;
351
417
}
352
418
353
- fn visit_block_stmt ( & mut self , n : & BlockStmt ) {
354
- self . with_scope ( ScopeKind :: Block , |v| n. visit_children_with ( v) )
355
- }
356
-
357
- fn visit_block_stmt_or_expr ( & mut self , n : & BlockStmtOrExpr ) {
358
- match n {
359
- // This avoid crating extra block scope for arrow function
360
- BlockStmtOrExpr :: BlockStmt ( n) => n. visit_children_with ( self ) ,
361
- BlockStmtOrExpr :: Expr ( n) => n. visit_with ( self ) ,
362
- }
363
- }
364
-
365
- fn visit_for_in_stmt ( & mut self , n : & ForInStmt ) {
366
- self . with_scope ( ScopeKind :: Block , |v| {
367
- n. left . visit_with ( v) ;
368
- n. right . visit_with ( v) ;
369
-
370
- v. with_scope ( ScopeKind :: Block , |v| {
371
- v. visit_for_body_within_same_scope ( & n. body ) ;
372
- } )
373
- } ) ;
374
- }
375
-
376
- fn visit_for_of_stmt ( & mut self , n : & ForOfStmt ) {
377
- self . with_scope ( ScopeKind :: Block , |v| {
378
- n. left . visit_with ( v) ;
379
- n. right . visit_with ( v) ;
380
-
381
- v. with_scope ( ScopeKind :: Block , |v| {
382
- v. visit_for_body_within_same_scope ( & n. body ) ;
383
- } )
384
- } ) ;
385
- }
386
-
387
- fn visit_for_stmt ( & mut self , n : & ForStmt ) {
388
- self . with_scope ( ScopeKind :: Block , |v| {
389
- n. init . visit_with ( v) ;
390
- n. test . visit_with ( v) ;
391
- n. update . visit_with ( v) ;
419
+ fn visit_var_declarator ( & mut self , v : & VarDeclarator ) {
420
+ let old = self . is_pat_decl ;
421
+ self . is_pat_decl = true ;
422
+ v. name . visit_with ( self ) ;
392
423
393
- v. with_scope ( ScopeKind :: Block , |v| {
394
- v. visit_for_body_within_same_scope ( & n. body ) ;
395
- } )
396
- } ) ;
397
- }
424
+ self . is_pat_decl = false ;
425
+ v. init . visit_with ( self ) ;
398
426
399
- fn visit_static_block ( & mut self , n : & StaticBlock ) {
400
- self . with_fn_scope ( |v| n. body . visit_children_with ( v) )
427
+ self . is_pat_decl = old;
401
428
}
402
429
}
0 commit comments