@@ -515,17 +515,22 @@ function formatSpanWorker(
515515 processNode ( enclosingNode , enclosingNode , startLine , undecoratedStartLine , initialIndentation , delta ) ;
516516 }
517517
518- if ( ! formattingScanner . isOnToken ( ) ) {
518+ // Leading trivia items get attached to and processed with the token that proceeds them. If the
519+ // range ends in the middle of some leading trivia, the token that proceeds them won't be in the
520+ // range and thus won't get processed. So we process those remaining trivia items here.
521+ const remainingTrivia = formattingScanner . getCurrentLeadingTrivia ( ) ;
522+ if ( remainingTrivia ) {
519523 const indentation = SmartIndenter . nodeWillIndentChild ( options , enclosingNode , /*child*/ undefined , sourceFile , /*indentByDefault*/ false )
520524 ? initialIndentation + options . indentSize !
521525 : initialIndentation ;
522- const leadingTrivia = formattingScanner . getCurrentLeadingTrivia ( ) ;
523- if ( leadingTrivia ) {
524- indentTriviaItems ( leadingTrivia , indentation , /*indentNextTokenOrTrivia*/ false ,
525- item => processRange ( item , sourceFile . getLineAndCharacterOfPosition ( item . pos ) , enclosingNode , enclosingNode , /*dynamicIndentation*/ undefined ! ) ) ;
526- if ( options . trimTrailingWhitespace !== false ) {
527- trimTrailingWhitespacesForRemainingRange ( leadingTrivia ) ;
526+ indentTriviaItems ( remainingTrivia , indentation , /*indentNextTokenOrTrivia*/ true ,
527+ item => {
528+ processRange ( item , sourceFile . getLineAndCharacterOfPosition ( item . pos ) , enclosingNode , enclosingNode , /*dynamicIndentation*/ undefined ! ) ;
529+ insertIndentation ( item . pos , indentation , /*lineAdded*/ false ) ;
528530 }
531+ ) ;
532+ if ( options . trimTrailingWhitespace !== false ) {
533+ trimTrailingWhitespacesForRemainingRange ( remainingTrivia ) ;
529534 }
530535 }
531536
0 commit comments