@@ -432,13 +432,12 @@ namespace ts.formatting {
432
432
if ( leadingTrivia ) {
433
433
indentTriviaItems ( leadingTrivia , indentation , /*indentNextTokenOrTrivia*/ false ,
434
434
item => processRange ( item , sourceFile . getLineAndCharacterOfPosition ( item . pos ) , enclosingNode , enclosingNode , /*dynamicIndentation*/ undefined ! ) ) ;
435
+ if ( options . trimTrailingWhitespace !== false ) {
436
+ trimTrailingWhitespacesForRemainingRange ( leadingTrivia ) ;
437
+ }
435
438
}
436
439
}
437
440
438
- if ( options . trimTrailingWhitespace !== false ) {
439
- trimTrailingWhitespacesForRemainingRange ( ) ;
440
- }
441
-
442
441
return edits ;
443
442
444
443
// local functions
@@ -1142,13 +1141,29 @@ namespace ts.formatting {
1142
1141
}
1143
1142
1144
1143
/**
1145
- * Trimming will be done for lines after the previous range
1144
+ * Trimming will be done for lines after the previous range.
1145
+ * Exclude comments as they had been previously processed.
1146
1146
*/
1147
- function trimTrailingWhitespacesForRemainingRange ( ) {
1148
- const startPosition = previousRange ? previousRange . end : originalRange . pos ;
1147
+ function trimTrailingWhitespacesForRemainingRange ( trivias : TextRangeWithKind < SyntaxKind > [ ] ) {
1148
+ let startPos = previousRange ? previousRange . end : originalRange . pos ;
1149
+ for ( const trivia of trivias ) {
1150
+ if ( isComment ( trivia . kind ) ) {
1151
+ if ( startPos < trivia . pos ) {
1152
+ trimTrailingWitespacesForPositions ( startPos , trivia . pos - 1 , previousRange ) ;
1153
+ }
1154
+
1155
+ startPos = trivia . end + 1 ;
1156
+ }
1157
+ }
1158
+
1159
+ if ( startPos < originalRange . end ) {
1160
+ trimTrailingWitespacesForPositions ( startPos , originalRange . end , previousRange ) ;
1161
+ }
1162
+ }
1149
1163
1150
- const startLine = sourceFile . getLineAndCharacterOfPosition ( startPosition ) . line ;
1151
- const endLine = sourceFile . getLineAndCharacterOfPosition ( originalRange . end ) . line ;
1164
+ function trimTrailingWitespacesForPositions ( startPos : number , endPos : number , previousRange : TextRangeWithKind ) {
1165
+ const startLine = sourceFile . getLineAndCharacterOfPosition ( startPos ) . line ;
1166
+ const endLine = sourceFile . getLineAndCharacterOfPosition ( endPos ) . line ;
1152
1167
1153
1168
trimTrailingWhitespacesForLines ( startLine , endLine + 1 , previousRange ) ;
1154
1169
}
0 commit comments