Skip to content

Commit c59fcae

Browse files
authored
fix(30003): formatter deletes comments after trailing comma (#36674)
1 parent 1aaf314 commit c59fcae

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

src/services/formatting/formatting.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,11 @@ namespace ts.formatting {
824824
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken()) {
825825
let tokenInfo: TokenInfo | undefined = formattingScanner.readTokenInfo(parent);
826826
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
827-
formattingScanner.advance();
828-
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
827+
const commaTokenLine = sourceFile.getLineAndCharacterOfPosition(tokenInfo.token.pos).line;
828+
if (startLine !== commaTokenLine) {
829+
formattingScanner.advance();
830+
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
831+
}
829832
}
830833

831834
// consume the list end token only if it is still belong to the parent

src/services/formatting/rules.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace ts.formatting {
241241
rule("SpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
242242
rule("NoSpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionDisabledOrUndefined("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
243243

244-
rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket], RuleAction.InsertSpace),
244+
rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket, isNextTokenNotCloseParen], RuleAction.InsertSpace),
245245
rule("NoSpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext], RuleAction.DeleteSpace),
246246

247247
// Insert space after function keyword for anonymous functions
@@ -670,6 +670,10 @@ namespace ts.formatting {
670670
return context.nextTokenSpan.kind !== SyntaxKind.CloseBracketToken;
671671
}
672672

673+
function isNextTokenNotCloseParen(context: FormattingContext): boolean {
674+
return context.nextTokenSpan.kind !== SyntaxKind.CloseParenToken;
675+
}
676+
673677
function isArrowFunctionContext(context: FormattingContext): boolean {
674678
return context.contextNode.kind === SyntaxKind.ArrowFunction;
675679
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////foo(1, /* comment */ );
4+
5+
format.document();
6+
verify.currentFileContentIs(`foo(1, /* comment */);`);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////new Foo(1, /* comment */ );
4+
5+
format.document();
6+
verify.currentFileContentIs(`new Foo(1, /* comment */);`);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////new Foo(1, );
4+
5+
format.document();
6+
verify.currentFileContentIs(`new Foo(1,);`);

0 commit comments

Comments
 (0)