@@ -78,6 +78,8 @@ use crate::{
7878
7979use self :: {
8080 array_expression:: FormatArrayExpression ,
81+ class:: format_grouped_parameters_with_return_type,
82+ function:: should_group_function_parameters,
8183 object_like:: ObjectLike ,
8284 object_pattern_like:: ObjectPatternLike ,
8385 parameters:: { ParameterLayout , ParameterList } ,
@@ -1072,10 +1074,16 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSEnumMember<'a>> {
10721074
10731075impl < ' a > FormatWrite < ' a > for AstNode < ' a , TSTypeAnnotation < ' a > > {
10741076 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
1075- if matches ! ( self . parent, AstNodes :: TSTypePredicate ( _) ) {
1076- write ! ( f, [ self . type_annotation( ) ] )
1077- } else {
1078- write ! ( f, [ ":" , space( ) , self . type_annotation( ) ] )
1077+ match self . parent {
1078+ AstNodes :: TSFunctionType ( _) | AstNodes :: TSConstructorType ( _) => {
1079+ write ! ( f, [ "=>" , space( ) , self . type_annotation( ) ] )
1080+ }
1081+ AstNodes :: TSTypePredicate ( _) => {
1082+ write ! ( f, [ self . type_annotation( ) ] )
1083+ }
1084+ _ => {
1085+ write ! ( f, [ ":" , space( ) , self . type_annotation( ) ] )
1086+ }
10791087 }
10801088 }
10811089}
@@ -1466,18 +1474,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, TSSignature<'a>>> {
14661474
14671475impl < ' a > FormatWrite < ' a > for AstNode < ' a , TSCallSignatureDeclaration < ' a > > {
14681476 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
1469- if let Some ( type_parameters) = & self . type_parameters ( ) {
1470- write ! ( f, group( type_parameters) ) ?;
1471- }
1472-
1473- if let Some ( this_param) = & self . this_param ( ) {
1474- write ! ( f, [ this_param, "," , soft_line_break_or_space( ) ] ) ?;
1475- }
1476- write ! ( f, group( & self . params( ) ) ) ?;
1477- if let Some ( return_type) = & self . return_type ( ) {
1478- write ! ( f, return_type) ?;
1479- }
1480- Ok ( ( ) )
1477+ write ! ( f, group( & format_args!( self . type_parameters( ) , self . params( ) , self . return_type( ) ) ) )
14811478 }
14821479}
14831480
@@ -1502,42 +1499,21 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
15021499 if self . optional ( ) {
15031500 write ! ( f, "?" ) ?;
15041501 }
1505- // TODO:
1506- // if should_group_function_parameters(
1507- // type_parameters.as_ref(),
1508- // parameters.len(),
1509- // return_type_annotation
1510- // .as_ref()
1511- // .map(|annotation| annotation.ty()),
1512- // &mut format_return_type_annotation,
1513- // f,
1514- // )? {
1515- // write!(f, [group(¶meters)])?;
1516- // } else {
1517- // write!(f, [parameters])?;
1518- // }
1519- if let Some ( type_parameters) = & self . type_parameters ( ) {
1520- write ! ( f, [ group( & type_parameters) ] ) ?;
1521- }
1522- write ! ( f, group( & self . params( ) ) ) ?;
1523- if let Some ( return_type) = & self . return_type ( ) {
1524- write ! ( f, return_type) ?;
1525- }
1526- Ok ( ( ) )
1502+
1503+ format_grouped_parameters_with_return_type (
1504+ self . type_parameters ( ) ,
1505+ self . this_param . as_deref ( ) ,
1506+ self . params ( ) ,
1507+ self . return_type ( ) ,
1508+ f,
1509+ )
15271510 }
15281511}
15291512
15301513impl < ' a > FormatWrite < ' a > for AstNode < ' a , TSConstructSignatureDeclaration < ' a > > {
15311514 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
15321515 write ! ( f, [ "new" , space( ) ] ) ?;
1533- if let Some ( type_parameters) = & self . type_parameters ( ) {
1534- write ! ( f, group( & type_parameters) ) ?;
1535- }
1536- write ! ( f, group( & self . params( ) ) ) ?;
1537- if let Some ( return_type) = self . return_type ( ) {
1538- write ! ( f, return_type) ?;
1539- }
1540- Ok ( ( ) )
1516+ write ! ( f, group( & format_args!( self . type_parameters( ) , self . params( ) , self . return_type( ) ) ) )
15411517 }
15421518}
15431519
@@ -1677,45 +1653,39 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSImportTypeQualifiedName<'a>> {
16771653
16781654impl < ' a > FormatWrite < ' a > for AstNode < ' a , TSFunctionType < ' a > > {
16791655 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
1680- let type_parameters = self . type_parameters ( ) ;
1681- let params = self . params ( ) ;
1682- let return_type = self . return_type ( ) ;
1683-
16841656 let format_inner = format_with ( |f| {
1657+ let type_parameters = self . type_parameters ( ) ;
16851658 write ! ( f, type_parameters) ?;
16861659
1687- // TODO
1688- // let mut format_return_type = return_type.format().memoized();
1689- let should_group_parameters = false ;
1690- // TODO
1691- //should_group_function_parameters(
1692- // type_parameters.as_ref(),
1693- // parameters.as_ref()?.items().len(),
1694- // Some(return_type.clone()),
1695- // &mut format_return_type,
1696- // f,
1697- // )?;
1698-
1699- if should_group_parameters {
1700- write ! ( f, group( & params) ) ?;
1701- } else {
1702- write ! ( f, params) ?;
1703- }
1660+ let params = self . params ( ) ;
1661+ let return_type = self . return_type ( ) ;
1662+ let mut format_parameters = params. memoized ( ) ;
1663+ let mut format_return_type = return_type. memoized ( ) ;
1664+
1665+ // Inspect early, in case the `return_type` is formatted before `parameters`
1666+ // in `should_group_function_parameters`.
1667+ format_parameters. inspect ( f) ?;
1668+
1669+ let group_parameters = should_group_function_parameters (
1670+ type_parameters. map ( AsRef :: as_ref) ,
1671+ params. items . len ( )
1672+ + usize:: from ( params. rest . is_some ( ) )
1673+ + usize:: from ( self . this_param . is_some ( ) ) ,
1674+ Some ( & self . return_type ) ,
1675+ & mut format_return_type,
1676+ f,
1677+ ) ?;
17041678
1705- let comments = f. context ( ) . comments ( ) . comments_before_character ( params. span . end , b'=' ) ;
1706- FormatTrailingComments :: Comments ( comments) . fmt ( f) ?;
1679+ if group_parameters {
1680+ write ! ( f, [ group( & format_parameters) ] )
1681+ } else {
1682+ write ! ( f, [ format_parameters] )
1683+ } ?;
17071684
1708- write ! ( f, [ space( ) , "=>" , space ( ) , return_type . type_annotation ( ) ] )
1685+ write ! ( f, [ space( ) , format_return_type ] )
17091686 } ) ;
17101687
1711- if self . needs_parentheses ( f) {
1712- "(" . fmt ( f) ?;
1713- }
1714- write ! ( f, group( & format_inner) ) ?;
1715- if self . needs_parentheses ( f) {
1716- ")" . fmt ( f) ?;
1717- }
1718- Ok ( ( ) )
1688+ write ! ( f, group( & format_inner) )
17191689 }
17201690}
17211691
@@ -1731,21 +1701,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructorType<'a>> {
17311701 }
17321702 write ! (
17331703 f,
1734- [ group( & format_args!(
1735- "new" ,
1736- space( ) ,
1737- type_parameters,
1738- params,
1739- format_once( |f| {
1740- let comments =
1741- f. context( ) . comments( ) . comments_before_character( params. span. end, b'=' ) ;
1742- FormatTrailingComments :: Comments ( comments) . fmt( f)
1743- } ) ,
1744- space( ) ,
1745- "=>" ,
1746- space( ) ,
1747- return_type. type_annotation( )
1748- ) ) ]
1704+ [ group( & format_args!( "new" , space( ) , type_parameters, params, space( ) , return_type) ) ]
17491705 ) ;
17501706 Ok ( ( ) )
17511707 }
0 commit comments