Skip to content

Commit 38631e6

Browse files
authored
fix(30003): formatter deletes comment after trailing comma (#49168)
Co-authored-by: TRCYX <[email protected]>
1 parent 757a3ac commit 38631e6

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

Diff for: src/services/formatting/formatting.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,9 @@ namespace ts.formatting {
836836
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken() && formattingScanner.getStartPos() < originalRange.end) {
837837
let tokenInfo: TokenInfo | undefined = formattingScanner.readTokenInfo(parent);
838838
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
839-
const commaTokenLine = sourceFile.getLineAndCharacterOfPosition(tokenInfo.token.pos).line;
840-
if (startLine !== commaTokenLine) {
841-
formattingScanner.advance();
842-
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
843-
}
839+
// consume the comma
840+
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
841+
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
844842
}
845843

846844
// consume the list end token only if it is still belong to the parent
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////foo(
4+
//// 1, /* comment */ );
5+
6+
format.document();
7+
verify.currentFileContentIs(`foo(
8+
1, /* comment */);`);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////new Foo(1
4+
////, /* comment */ );
5+
6+
format.document();
7+
verify.currentFileContentIs(`new Foo(1
8+
, /* comment */);`);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////new Foo(1,
4+
//// /* comment */ );
5+
6+
format.document();
7+
verify.currentFileContentIs(`new Foo(1,
8+
/* comment */);`);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////new Foo(1, /* comment */
4+
//// );
5+
6+
format.document();
7+
verify.currentFileContentIs(`new Foo(1, /* comment */
8+
);`);

0 commit comments

Comments
 (0)