@@ -4,7 +4,7 @@ use oxc_ast::ast::*;
44use oxc_span:: { GetSpan , Span } ;
55
66use crate :: {
7- Format , FormatResult , FormatWrite ,
7+ Format , FormatResult , FormatWrite , format_args ,
88 formatter:: { Formatter , prelude:: * , trivia:: FormatTrailingComments } ,
99 generated:: ast_nodes:: { AstNode , AstNodes } ,
1010 utils:: format_node_without_trailing_comments:: FormatNodeWithoutTrailingComments ,
@@ -373,41 +373,34 @@ impl<'a> FormatConditionalLike<'a, '_> {
373373 f : & mut Formatter < ' f , ' a > ,
374374 layout : ConditionalLayout ,
375375 ) -> FormatResult < ( ) > {
376- let format_inner = format_with ( |f| match self . conditional {
377- ConditionalLike :: ConditionalExpression ( conditional) => {
378- write ! ( f, FormatNodeWithoutTrailingComments ( conditional. test( ) ) ) ?;
379- format_trailing_comments (
380- conditional. test . span ( ) . end ,
381- conditional. consequent . span ( ) . start ,
382- b'?' ,
383- f,
384- )
385- }
386- ConditionalLike :: TSConditionalType ( conditional) => {
387- write ! (
388- f,
389- [
390- conditional. check_type( ) ,
391- space( ) ,
392- "extends" ,
393- space( ) ,
394- FormatNodeWithoutTrailingComments ( conditional. extends_type( ) )
395- ]
396- ) ?;
376+ let format_inner = format_with ( |f| {
377+ let ( start, end) = match self . conditional {
378+ ConditionalLike :: ConditionalExpression ( conditional) => {
379+ write ! ( f, FormatNodeWithoutTrailingComments ( conditional. test( ) ) ) ?;
380+ ( conditional. test . span ( ) . end , conditional. consequent . span ( ) . start )
381+ }
382+ ConditionalLike :: TSConditionalType ( conditional) => {
383+ write ! (
384+ f,
385+ [
386+ conditional. check_type( ) ,
387+ space( ) ,
388+ "extends" ,
389+ space( ) ,
390+ FormatNodeWithoutTrailingComments ( conditional. extends_type( ) )
391+ ]
392+ ) ?;
393+ ( conditional. extends_type . span ( ) . end , conditional. true_type . span ( ) . start )
394+ }
395+ } ;
397396
398- format_trailing_comments (
399- conditional. extends_type . span ( ) . end ,
400- conditional. true_type . span ( ) . start ,
401- b'?' ,
402- f,
403- )
404- }
397+ format_trailing_comments ( start, end, b'?' , f)
405398 } ) ;
406399
407400 if layout. is_nested_alternate ( ) {
408401 write ! ( f, [ align( 2 , & format_inner) ] )
409402 } else {
410- format_inner . fmt ( f )
403+ write ! ( f , format_inner )
411404 }
412405 }
413406
@@ -420,27 +413,19 @@ impl<'a> FormatConditionalLike<'a, '_> {
420413 write ! ( f, [ soft_line_break_or_space( ) , "?" , space( ) ] ) ?;
421414
422415 let format_consequent = format_with ( |f| {
423- let format_consequent_with_trailing_comments =
424- format_once ( |f| match self . conditional {
416+ let format_consequent_with_trailing_comments = format_once ( |f| {
417+ let ( start , end ) = match self . conditional {
425418 ConditionalLike :: ConditionalExpression ( conditional) => {
426419 write ! ( f, FormatNodeWithoutTrailingComments ( conditional. consequent( ) ) ) ?;
427- format_trailing_comments (
428- conditional. consequent . span ( ) . end ,
429- conditional. alternate . span ( ) . start ,
430- b':' ,
431- f,
432- )
420+ ( conditional. consequent . span ( ) . end , conditional. alternate . span ( ) . start )
433421 }
434422 ConditionalLike :: TSConditionalType ( conditional) => {
435423 write ! ( f, FormatNodeWithoutTrailingComments ( conditional. true_type( ) ) ) ?;
436- format_trailing_comments (
437- conditional. true_type . span ( ) . end ,
438- conditional. false_type . span ( ) . start ,
439- b':' ,
440- f,
441- )
424+ ( conditional. true_type . span ( ) . end , conditional. false_type . span ( ) . start )
442425 }
443- } ) ;
426+ } ;
427+ format_trailing_comments ( start, end, b':' , f)
428+ } ) ;
444429
445430 let format_consequent_with_proper_indentation = format_with ( |f| {
446431 if f. options ( ) . indent_style . is_space ( ) {
@@ -606,7 +591,11 @@ impl<'a> Format<'a> for FormatConditionalLike<'a, '_> {
606591 } ) ;
607592
608593 let grouped = format_with ( |f| {
609- if layout. is_root ( ) { write ! ( f, [ group( & format_inner) ] ) } else { format_inner. fmt ( f) }
594+ if layout. is_root ( ) || layout. is_nested_test ( ) {
595+ write ! ( f, [ group( & format_inner) ] )
596+ } else {
597+ format_inner. fmt ( f)
598+ }
610599 } ) ;
611600
612601 if layout. is_nested_test ( ) || should_extra_indent {
0 commit comments