mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 11:35:42 -06:00
fix(30003): formatter deletes comments after trailing comma (#36674)
This commit is contained in:
parent
1aaf314678
commit
c59fcae117
@ -824,8 +824,11 @@ namespace ts.formatting {
|
||||
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken()) {
|
||||
let tokenInfo: TokenInfo | undefined = formattingScanner.readTokenInfo(parent);
|
||||
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
|
||||
formattingScanner.advance();
|
||||
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
|
||||
const commaTokenLine = sourceFile.getLineAndCharacterOfPosition(tokenInfo.token.pos).line;
|
||||
if (startLine !== commaTokenLine) {
|
||||
formattingScanner.advance();
|
||||
tokenInfo = formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(parent) : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// consume the list end token only if it is still belong to the parent
|
||||
|
||||
@ -241,7 +241,7 @@ namespace ts.formatting {
|
||||
rule("SpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.InsertSpace),
|
||||
rule("NoSpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionDisabledOrUndefined("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
|
||||
|
||||
rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket], RuleAction.InsertSpace),
|
||||
rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket, isNextTokenNotCloseParen], RuleAction.InsertSpace),
|
||||
rule("NoSpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext], RuleAction.DeleteSpace),
|
||||
|
||||
// Insert space after function keyword for anonymous functions
|
||||
@ -670,6 +670,10 @@ namespace ts.formatting {
|
||||
return context.nextTokenSpan.kind !== SyntaxKind.CloseBracketToken;
|
||||
}
|
||||
|
||||
function isNextTokenNotCloseParen(context: FormattingContext): boolean {
|
||||
return context.nextTokenSpan.kind !== SyntaxKind.CloseParenToken;
|
||||
}
|
||||
|
||||
function isArrowFunctionContext(context: FormattingContext): boolean {
|
||||
return context.contextNode.kind === SyntaxKind.ArrowFunction;
|
||||
}
|
||||
|
||||
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace.ts
Normal file
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace.ts
Normal file
@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////foo(1, /* comment */ );
|
||||
|
||||
format.document();
|
||||
verify.currentFileContentIs(`foo(1, /* comment */);`);
|
||||
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace1.ts
Normal file
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace1.ts
Normal file
@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////new Foo(1, /* comment */ );
|
||||
|
||||
format.document();
|
||||
verify.currentFileContentIs(`new Foo(1, /* comment */);`);
|
||||
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace2.ts
Normal file
6
tests/cases/fourslash/formatNoSpaceBeforeCloseBrace2.ts
Normal file
@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts"/>
|
||||
|
||||
////new Foo(1, );
|
||||
|
||||
format.document();
|
||||
verify.currentFileContentIs(`new Foo(1,);`);
|
||||
Loading…
x
Reference in New Issue
Block a user