@@ -41,6 +41,7 @@ use super::swc_helpers::is_void_type;
41
41
use super :: swc_helpers:: maybe_lit_to_ts_type;
42
42
use super :: swc_helpers:: new_ident;
43
43
use super :: swc_helpers:: ts_keyword_type;
44
+ use super :: swc_helpers:: DeclMutabilityKind ;
44
45
use super :: swc_helpers:: ReturnStatementAnalysis ;
45
46
use super :: transform_dts:: FastCheckDtsDiagnostic ;
46
47
use super :: transform_dts:: FastCheckDtsTransformer ;
@@ -858,14 +859,20 @@ impl<'a> FastCheckTransformer<'a> {
858
859
_ => None ,
859
860
} ;
860
861
explicit_type_ann. or_else ( || {
861
- self . maybe_infer_type_from_expr ( & assign. right ) . map (
862
- |type_ann| {
862
+ self
863
+ . maybe_infer_type_from_expr (
864
+ & assign. right ,
865
+ match prop. readonly {
866
+ true => DeclMutabilityKind :: Const ,
867
+ false => DeclMutabilityKind :: Mutable ,
868
+ } ,
869
+ )
870
+ . map ( |type_ann| {
863
871
Box :: new ( TsTypeAnn {
864
872
span : DUMMY_SP ,
865
873
type_ann : Box :: new ( type_ann) ,
866
874
} )
867
- } ,
868
- )
875
+ } )
869
876
} )
870
877
}
871
878
}
@@ -1006,10 +1013,15 @@ impl<'a> FastCheckTransformer<'a> {
1006
1013
return Ok ( true ) ;
1007
1014
}
1008
1015
if n. type_ann . is_none ( ) {
1009
- let inferred_type = n
1010
- . value
1011
- . as_ref ( )
1012
- . and_then ( |e| self . maybe_infer_type_from_expr ( e) ) ;
1016
+ let inferred_type = n. value . as_ref ( ) . and_then ( |e| {
1017
+ self . maybe_infer_type_from_expr (
1018
+ e,
1019
+ match n. readonly {
1020
+ true => DeclMutabilityKind :: Const ,
1021
+ false => DeclMutabilityKind :: Mutable ,
1022
+ } ,
1023
+ )
1024
+ } ) ;
1013
1025
match inferred_type {
1014
1026
Some ( t) => {
1015
1027
n. type_ann = Some ( Box :: new ( TsTypeAnn {
@@ -1056,10 +1068,9 @@ impl<'a> FastCheckTransformer<'a> {
1056
1068
} else if let Some ( type_ann) = n. type_ann . clone ( ) {
1057
1069
type_ann
1058
1070
} else {
1059
- let inferred_type = n
1060
- . value
1061
- . as_ref ( )
1062
- . and_then ( |e| self . maybe_infer_type_from_expr ( e) ) ;
1071
+ let inferred_type = n. value . as_ref ( ) . and_then ( |e| {
1072
+ self . maybe_infer_type_from_expr ( e, DeclMutabilityKind :: Mutable )
1073
+ } ) ;
1063
1074
match inferred_type {
1064
1075
Some ( t) => Box :: new ( TsTypeAnn {
1065
1076
span : DUMMY_SP ,
@@ -1244,7 +1255,8 @@ impl<'a> FastCheckTransformer<'a> {
1244
1255
) ?;
1245
1256
}
1246
1257
BlockStmtOrExpr :: Expr ( expr) => {
1247
- let inferred_type = self . maybe_infer_type_from_expr ( expr) ;
1258
+ let inferred_type =
1259
+ self . maybe_infer_type_from_expr ( expr, DeclMutabilityKind :: Mutable ) ;
1248
1260
match inferred_type {
1249
1261
Some ( t) => {
1250
1262
let mut return_type = Box :: new ( t) ;
@@ -1370,7 +1382,10 @@ impl<'a> FastCheckTransformer<'a> {
1370
1382
Pat :: Assign ( assign) => match & mut * assign. left {
1371
1383
Pat :: Ident ( ident) => {
1372
1384
if ident. type_ann . is_none ( ) {
1373
- let inferred_type = self . maybe_infer_type_from_expr ( & assign. right ) ;
1385
+ let inferred_type = self . maybe_infer_type_from_expr (
1386
+ & assign. right ,
1387
+ DeclMutabilityKind :: Mutable ,
1388
+ ) ;
1374
1389
match inferred_type {
1375
1390
Some ( t) => {
1376
1391
ident. type_ann = Some ( Box :: new ( TsTypeAnn {
@@ -1491,7 +1506,7 @@ impl<'a> FastCheckTransformer<'a> {
1491
1506
// don't need to do anything for ambient decls
1492
1507
if !is_ambient {
1493
1508
for decl in & mut n. decls {
1494
- self . transform_var_declarator ( decl) ?;
1509
+ self . transform_var_declarator ( n . kind , decl) ?;
1495
1510
}
1496
1511
}
1497
1512
@@ -1500,15 +1515,23 @@ impl<'a> FastCheckTransformer<'a> {
1500
1515
1501
1516
fn transform_var_declarator (
1502
1517
& mut self ,
1518
+ decl_kind : VarDeclKind ,
1503
1519
n : & mut VarDeclarator ,
1504
1520
) -> Result < ( ) , Vec < FastCheckDiagnostic > > {
1505
1521
match & mut n. name {
1506
1522
Pat :: Ident ( ident) => {
1507
1523
if ident. type_ann . is_none ( ) {
1508
- let inferred_type = n
1509
- . init
1510
- . as_ref ( )
1511
- . and_then ( |e| self . maybe_infer_type_from_expr ( e) ) ;
1524
+ let inferred_type = n. init . as_ref ( ) . and_then ( |e| {
1525
+ self . maybe_infer_type_from_expr (
1526
+ e,
1527
+ match decl_kind {
1528
+ VarDeclKind :: Var | VarDeclKind :: Let => {
1529
+ DeclMutabilityKind :: Mutable
1530
+ }
1531
+ VarDeclKind :: Const => DeclMutabilityKind :: Const ,
1532
+ } ,
1533
+ )
1534
+ } ) ;
1512
1535
match inferred_type {
1513
1536
Some ( t) => {
1514
1537
ident. type_ann = Some ( Box :: new ( TsTypeAnn {
@@ -1719,8 +1742,8 @@ impl<'a> FastCheckTransformer<'a> {
1719
1742
}
1720
1743
}
1721
1744
is_leavable
1722
- } ,
1723
- Expr :: Object ( n) => {
1745
+ } ,
1746
+ Expr :: Object ( n) => {
1724
1747
let mut is_leavable = true ;
1725
1748
for prop in & mut n. props {
1726
1749
is_leavable = match prop {
@@ -1817,11 +1840,15 @@ impl<'a> FastCheckTransformer<'a> {
1817
1840
Ok ( is_leavable)
1818
1841
}
1819
1842
1820
- fn maybe_infer_type_from_expr ( & self , expr : & Expr ) -> Option < TsType > {
1843
+ fn maybe_infer_type_from_expr (
1844
+ & self ,
1845
+ expr : & Expr ,
1846
+ decl_kind : DeclMutabilityKind ,
1847
+ ) -> Option < TsType > {
1821
1848
match expr {
1822
1849
Expr :: TsTypeAssertion ( n) => infer_simple_type_from_type ( & n. type_ann ) ,
1823
1850
Expr :: TsAs ( n) => infer_simple_type_from_type ( & n. type_ann ) ,
1824
- Expr :: Lit ( lit) => maybe_lit_to_ts_type ( lit) ,
1851
+ Expr :: Lit ( lit) => maybe_lit_to_ts_type ( lit, decl_kind ) ,
1825
1852
Expr :: Call ( call_expr) => {
1826
1853
if self . is_call_expr_symbol_create ( call_expr) {
1827
1854
Some ( TsType :: TsTypeOperator ( TsTypeOperator {
@@ -1837,7 +1864,7 @@ impl<'a> FastCheckTransformer<'a> {
1837
1864
None
1838
1865
}
1839
1866
}
1840
- Expr :: Paren ( n) => self . maybe_infer_type_from_expr ( & n. expr ) ,
1867
+ Expr :: Paren ( n) => self . maybe_infer_type_from_expr ( & n. expr , decl_kind ) ,
1841
1868
Expr :: This ( _)
1842
1869
| Expr :: Array ( _)
1843
1870
| Expr :: Object ( _)
0 commit comments