1
1
use swc_common:: { collections:: AHashMap , SyntaxContext } ;
2
2
use swc_ecma_ast:: * ;
3
- use swc_ecma_utils:: { find_pat_ids, ExprCtx , ExprExt , IsEmpty , StmtExt } ;
3
+ use swc_ecma_utils:: { find_pat_ids, ExprCtx , ExprExt , IsEmpty , StmtExt , Type , Value } ;
4
4
use swc_ecma_visit:: { noop_visit_type, Visit , VisitWith } ;
5
5
use swc_timer:: timer;
6
6
@@ -155,33 +155,38 @@ where
155
155
156
156
fn report_assign_pat ( & mut self , p : & Pat , is_read_modify : bool ) {
157
157
for id in find_pat_ids ( p) {
158
- self . data . report_assign ( self . ctx , id, is_read_modify)
158
+ // It's hard to determined the type of pat assignment
159
+ self . data
160
+ . report_assign ( self . ctx , id, is_read_modify, Value :: Unknown )
159
161
}
160
162
161
163
if let Pat :: Expr ( e) = p {
162
164
match & * * e {
163
- Expr :: Ident ( i) => self . data . report_assign ( self . ctx , i. to_id ( ) , is_read_modify) ,
165
+ Expr :: Ident ( i) => {
166
+ self . data
167
+ . report_assign ( self . ctx , i. to_id ( ) , is_read_modify, Value :: Unknown )
168
+ }
164
169
_ => self . mark_mutation_if_member ( e. as_member ( ) ) ,
165
170
}
166
171
}
167
172
}
168
173
169
- fn report_assign_expr_if_ident ( & mut self , e : Option < & Ident > , is_op : bool ) {
174
+ fn report_assign_expr_if_ident ( & mut self , e : Option < & Ident > , is_op : bool , ty : Value < Type > ) {
170
175
if let Some ( i) = e {
171
- self . data . report_assign ( self . ctx , i. to_id ( ) , is_op)
176
+ self . data . report_assign ( self . ctx , i. to_id ( ) , is_op, ty )
172
177
}
173
178
}
174
179
175
180
fn declare_decl (
176
181
& mut self ,
177
182
i : & Ident ,
178
- has_init : bool ,
183
+ init_type : Option < Value < Type > > ,
179
184
kind : Option < VarDeclKind > ,
180
185
is_fn_decl : bool ,
181
186
) -> & mut S :: VarData {
182
187
self . scope . add_declared_symbol ( i) ;
183
188
184
- let v = self . data . declare_decl ( self . ctx , i, has_init , kind) ;
189
+ let v = self . data . declare_decl ( self . ctx , i, init_type , kind) ;
185
190
186
191
if is_fn_decl {
187
192
v. mark_declared_as_fn_decl ( ) ;
@@ -267,13 +272,15 @@ where
267
272
match & n. left {
268
273
AssignTarget :: Pat ( p) => {
269
274
for id in find_pat_ids ( p) {
270
- self . data . report_assign ( self . ctx , id, is_op_assign)
275
+ self . data
276
+ . report_assign ( self . ctx , id, is_op_assign, n. right . get_type ( ) )
271
277
}
272
278
}
273
279
AssignTarget :: Simple ( e) => {
274
280
self . report_assign_expr_if_ident (
275
281
e. as_ident ( ) . map ( Ident :: from) . as_ref ( ) ,
276
282
is_op_assign,
283
+ n. right . get_type ( ) ,
277
284
) ;
278
285
self . mark_mutation_if_member ( e. as_member ( ) )
279
286
}
@@ -516,7 +523,7 @@ where
516
523
517
524
#[ cfg_attr( feature = "tracing-spans" , tracing:: instrument( skip_all) ) ]
518
525
fn visit_class_decl ( & mut self , n : & ClassDecl ) {
519
- self . declare_decl ( & n. ident , true , None , false ) ;
526
+ self . declare_decl ( & n. ident , Some ( Value :: Unknown ) , None , false ) ;
520
527
521
528
n. visit_children_with ( self ) ;
522
529
}
@@ -526,7 +533,7 @@ where
526
533
n. visit_children_with ( self ) ;
527
534
528
535
if let Some ( id) = & n. ident {
529
- self . declare_decl ( id, true , None , false ) ;
536
+ self . declare_decl ( id, Some ( Value :: Unknown ) , None , false ) ;
530
537
}
531
538
}
532
539
@@ -681,7 +688,7 @@ where
681
688
in_pat_of_param : false ,
682
689
in_catch_param : false ,
683
690
var_decl_kind_of_pat : None ,
684
- in_pat_of_var_decl_with_init : false ,
691
+ in_pat_of_var_decl_with_init : None ,
685
692
..self . ctx
686
693
} ;
687
694
@@ -721,7 +728,8 @@ where
721
728
in_decl_with_no_side_effect_for_member_access : true ,
722
729
..self . ctx
723
730
} ;
724
- self . with_ctx ( ctx) . declare_decl ( & n. ident , true , None , true ) ;
731
+ self . with_ctx ( ctx)
732
+ . declare_decl ( & n. ident , Some ( Value :: Known ( Type :: Obj ) ) , None , true ) ;
725
733
726
734
if n. function . body . is_empty ( ) {
727
735
self . data . var_or_default ( n. ident . to_id ( ) ) . mark_as_pure_fn ( ) ;
@@ -889,15 +897,15 @@ where
889
897
}
890
898
891
899
fn visit_import_default_specifier ( & mut self , n : & ImportDefaultSpecifier ) {
892
- self . declare_decl ( & n. local , true , None , false ) ;
900
+ self . declare_decl ( & n. local , Some ( Value :: Unknown ) , None , false ) ;
893
901
}
894
902
895
903
fn visit_import_named_specifier ( & mut self , n : & ImportNamedSpecifier ) {
896
- self . declare_decl ( & n. local , true , None , false ) ;
904
+ self . declare_decl ( & n. local , Some ( Value :: Unknown ) , None , false ) ;
897
905
}
898
906
899
907
fn visit_import_star_as_specifier ( & mut self , n : & ImportStarAsSpecifier ) {
900
- self . declare_decl ( & n. local , true , None , false ) ;
908
+ self . declare_decl ( & n. local , Some ( Value :: Unknown ) , None , false ) ;
901
909
}
902
910
903
911
#[ cfg_attr( feature = "tracing-spans" , tracing:: instrument( skip_all) ) ]
@@ -907,7 +915,7 @@ where
907
915
in_pat_of_param : false ,
908
916
in_catch_param : false ,
909
917
var_decl_kind_of_pat : None ,
910
- in_pat_of_var_decl_with_init : false ,
918
+ in_pat_of_var_decl_with_init : None ,
911
919
..self . ctx
912
920
} ;
913
921
@@ -1238,7 +1246,7 @@ where
1238
1246
fn visit_update_expr ( & mut self , n : & UpdateExpr ) {
1239
1247
n. visit_children_with ( self ) ;
1240
1248
1241
- self . report_assign_expr_if_ident ( n. arg . as_ident ( ) , true ) ;
1249
+ self . report_assign_expr_if_ident ( n. arg . as_ident ( ) , true , Value :: Known ( Type :: Num ) ) ;
1242
1250
self . mark_mutation_if_member ( n. arg . as_member ( ) ) ;
1243
1251
}
1244
1252
@@ -1278,7 +1286,7 @@ where
1278
1286
let ctx = Ctx {
1279
1287
inline_prevented : self . ctx . inline_prevented || prevent_inline,
1280
1288
in_pat_of_var_decl : true ,
1281
- in_pat_of_var_decl_with_init : e. init . is_some ( ) ,
1289
+ in_pat_of_var_decl_with_init : e. init . as_ref ( ) . map ( |init| init . get_type ( ) ) ,
1282
1290
in_decl_with_no_side_effect_for_member_access : e
1283
1291
. init
1284
1292
. as_deref ( )
0 commit comments