@@ -67,7 +67,8 @@ pub struct ExportedClass {
67
67
/// All readable properties of the class
68
68
readable_properties : Vec < String > ,
69
69
/// Map from field name to type as a string plus whether it has a setter
70
- typescript_fields : HashMap < String , ( String , bool ) > ,
70
+ /// and it is optional
71
+ typescript_fields : HashMap < String , ( String , bool , bool ) > ,
71
72
}
72
73
73
74
const INITIAL_HEAP_VALUES : & [ & str ] = & [ "undefined" , "null" , "true" , "false" ] ;
@@ -703,14 +704,18 @@ impl<'a> Context<'a> {
703
704
let mut fields = class. typescript_fields . keys ( ) . collect :: < Vec < _ > > ( ) ;
704
705
fields. sort ( ) ; // make sure we have deterministic output
705
706
for name in fields {
706
- let ( ty, has_setter) = & class. typescript_fields [ name] ;
707
+ let ( ty, has_setter, is_optional ) = & class. typescript_fields [ name] ;
707
708
ts_dst. push_str ( " " ) ;
708
709
if !has_setter {
709
710
ts_dst. push_str ( "readonly " ) ;
710
711
}
711
712
ts_dst. push_str ( name) ;
712
- ts_dst. push_str ( ": " ) ;
713
- ts_dst. push_str ( ty) ;
713
+ if * is_optional {
714
+ ts_dst. push_str ( "?: " ) ;
715
+ } else {
716
+ ts_dst. push_str ( ": " ) ;
717
+ }
718
+ ts_dst. push_str ( & ty) ;
714
719
ts_dst. push_str ( ";\n " ) ;
715
720
}
716
721
dst. push_str ( "}\n " ) ;
@@ -781,9 +786,7 @@ impl<'a> Context<'a> {
781
786
if !self . should_write_global ( "not_defined" ) {
782
787
return ;
783
788
}
784
- self . global (
785
- "function notDefined(what) { return () => { throw new Error(`${what} is not defined`); }; }"
786
- ) ;
789
+ self . global ( "function notDefined(what) { return () => { throw new Error(`${what} is not defined`); }; }" ) ;
787
790
}
788
791
789
792
fn expose_assert_num ( & mut self ) {
@@ -2045,6 +2048,7 @@ impl<'a> Context<'a> {
2045
2048
ts_ret_ty,
2046
2049
js_doc,
2047
2050
code,
2051
+ might_be_optional_field,
2048
2052
} = builder
2049
2053
. process ( & adapter, instrs, arg_names)
2050
2054
. with_context ( || match kind {
@@ -2089,7 +2093,7 @@ impl<'a> Context<'a> {
2089
2093
AuxExportKind :: Setter { class, field } => {
2090
2094
let arg_ty = ts_arg_tys[ 0 ] . clone ( ) ;
2091
2095
let exported = require_class ( & mut self . exported_classes , class) ;
2092
- exported. push_setter ( & docs, field, & code, & arg_ty) ;
2096
+ exported. push_setter ( & docs, field, & code, & arg_ty, might_be_optional_field ) ;
2093
2097
}
2094
2098
AuxExportKind :: StaticFunction { class, name } => {
2095
2099
let exported = require_class ( & mut self . exported_classes , class) ;
@@ -3097,9 +3101,17 @@ impl ExportedClass {
3097
3101
3098
3102
/// Used for adding a setter to a class, mainly to ensure that TypeScript
3099
3103
/// generation is handled specially.
3100
- fn push_setter ( & mut self , docs : & str , field : & str , js : & str , ret_ty : & str ) {
3101
- let has_setter = self . push_accessor ( docs, field, js, "set " , ret_ty) ;
3104
+ fn push_setter (
3105
+ & mut self ,
3106
+ docs : & str ,
3107
+ field : & str ,
3108
+ js : & str ,
3109
+ ret_ty : & str ,
3110
+ might_be_optional_field : bool ,
3111
+ ) {
3112
+ let ( has_setter, is_optional) = self . push_accessor ( docs, field, js, "set " , ret_ty) ;
3102
3113
* has_setter = true ;
3114
+ * is_optional = might_be_optional_field;
3103
3115
}
3104
3116
3105
3117
fn push_accessor (
@@ -3109,18 +3121,20 @@ impl ExportedClass {
3109
3121
js : & str ,
3110
3122
prefix : & str ,
3111
3123
ret_ty : & str ,
3112
- ) -> & mut bool {
3124
+ ) -> ( & mut bool , & mut bool ) {
3113
3125
self . contents . push_str ( docs) ;
3114
3126
self . contents . push_str ( prefix) ;
3115
3127
self . contents . push_str ( field) ;
3116
3128
self . contents . push_str ( js) ;
3117
3129
self . contents . push_str ( "\n " ) ;
3118
- let ( ty, has_setter) = self
3130
+
3131
+ let ( ty, has_setter, is_optional) = self
3119
3132
. typescript_fields
3120
3133
. entry ( field. to_string ( ) )
3121
3134
. or_insert_with ( Default :: default) ;
3135
+
3122
3136
* ty = ret_ty. to_string ( ) ;
3123
- has_setter
3137
+ ( has_setter, is_optional )
3124
3138
}
3125
3139
}
3126
3140
0 commit comments