@@ -1376,6 +1376,47 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> DirectDeclSmartConstructors<'a,
13761376 Some ( self . alloc ( aast:: Expr ( ( ) , pos, expr_) ) )
13771377 }
13781378
1379+ fn node_to_str ( & self , node : Node < ' a > , semicolon : Node < ' a > ) -> & ' a str {
1380+ let expr = self . node_to_expr ( node) ;
1381+ match expr {
1382+ // Only some nodes have a simple translate to an expression
1383+ // Since some nodes *are* expressions, we prefer to write the
1384+ // to_string logic once for expressions
1385+ Some ( expr) => self . expr_to_str ( expr) ,
1386+
1387+ // This is usually complex shapes / vec / dict etc. and the
1388+ // actual value is NOT in the nodes so we have to yank the text
1389+ None => self
1390+ . str_from_utf8 ( self . source_text_at_pos ( Pos :: from_lnum_bol_offset (
1391+ self . arena ,
1392+ self . filename ,
1393+ self . get_pos ( node) . to_start_and_end_lnum_bol_offset ( ) . 1 ,
1394+ self . get_pos ( semicolon) . to_start_and_end_lnum_bol_offset ( ) . 0 ,
1395+ ) ) )
1396+ . trim ( ) ,
1397+ }
1398+ }
1399+
1400+ fn expr_to_str ( & self , expr : & aast:: Expr < ' a , ( ) , ( ) > ) -> & ' a str {
1401+ match expr. 2 {
1402+ // Simple literals (99% of the cases)
1403+ aast:: Expr_ :: Int ( s) => s,
1404+ aast:: Expr_ :: Float ( s) => s,
1405+ aast:: Expr_ :: String ( s) => {
1406+ // Always use single quotes for strings
1407+ bumpalo:: format!( in self . arena, "'{}'" , s) . into_bump_str ( )
1408+ }
1409+ aast:: Expr_ :: True => "true" ,
1410+ aast:: Expr_ :: False => "false" ,
1411+ aast:: Expr_ :: Null => "null" ,
1412+
1413+ // Default to actual text
1414+ // ... VariableExpression, BinaryExpression, SubscriptExpression,
1415+ // FunctionCallExpression, ConditionalExpression ...
1416+ _ => self . str_from_utf8 ( self . source_text_at_pos ( expr. 1 ) ) ,
1417+ }
1418+ }
1419+
13791420 fn node_to_non_ret_ty ( & self , node : Node < ' a > ) -> Option < & ' a Ty < ' a > > {
13801421 self . node_to_ty_ ( node, false )
13811422 }
@@ -3947,12 +3988,18 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
39473988 let ty = ty
39483989 . or_else ( || self . infer_const ( name, initializer) )
39493990 . unwrap_or ( TANY ) ;
3991+
39503992 Some ( Node :: Const ( self . alloc (
39513993 shallow_decl_defs:: ShallowClassConst {
39523994 abstract_,
39533995 name : id. into ( ) ,
39543996 type_ : ty,
39553997 refs,
3998+ value : if self . opts . include_assignment_values {
3999+ Some ( self . node_to_str ( initializer, semicolon) )
4000+ } else {
4001+ None
4002+ } ,
39564003 } ,
39574004 ) ) )
39584005 }
@@ -3978,8 +4025,18 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
39784025 . node_to_ty ( hint)
39794026 . or_else ( || self . infer_const ( name, initializer) )
39804027 . unwrap_or_else ( || self . tany_with_pos ( id_pos) ) ;
4028+
4029+ let const_decl = ConstDecl {
4030+ pos,
4031+ type_ : ty,
4032+ value : if self . opts . include_assignment_values {
4033+ Some ( self . node_to_str ( initializer, semicolon) )
4034+ } else {
4035+ None
4036+ } ,
4037+ } ;
39814038 let this = Rc :: make_mut ( & mut self . state ) ;
3982- this. add_const ( id, this. alloc ( ConstDecl { pos , type_ : ty } ) ) ;
4039+ this. add_const ( id, this. alloc ( const_decl ) ) ;
39834040 }
39844041 }
39854042 _ => { }
@@ -4972,7 +5029,7 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
49725029 name : Self :: Output ,
49735030 _equal : Self :: Output ,
49745031 value : Self :: Output ,
4975- _semicolon : Self :: Output ,
5032+ semicolon : Self :: Output ,
49765033 ) -> Self :: Output {
49775034 let refs = self . stop_accumulating_const_refs ( ) ;
49785035 let id = match self . expect_name ( name) {
@@ -4988,6 +5045,11 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
49885045 . infer_const ( name, value)
49895046 . unwrap_or_else ( || self . tany_with_pos ( id. 0 ) ) ,
49905047 refs,
5048+ value : if self . opts . include_assignment_values {
5049+ Some ( self . node_to_str ( value, semicolon) )
5050+ } else {
5051+ None
5052+ } ,
49915053 } ) ,
49925054 )
49935055 }
@@ -5160,8 +5222,8 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
51605222 modifiers : Self :: Output ,
51615223 type_ : Self :: Output ,
51625224 name : Self :: Output ,
5163- _initializer : Self :: Output ,
5164- _semicolon : Self :: Output ,
5225+ initializer : Self :: Output ,
5226+ semicolon : Self :: Output ,
51655227 ) -> Self :: Output {
51665228 let refs = self . stop_accumulating_const_refs ( ) ;
51675229 let name = match self . expect_name ( name) {
@@ -5203,6 +5265,11 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
52035265 name : name. into ( ) ,
52045266 type_,
52055267 refs,
5268+ value : if self . opts . include_assignment_values {
5269+ Some ( self . node_to_str ( initializer, semicolon) )
5270+ } else {
5271+ None
5272+ } ,
52065273 } ) )
52075274 }
52085275
0 commit comments