fix(30003): formatter deletes comments after trailing comma (#36674)

This commit is contained in:
Alexander T 2020-02-15 00:57:18 +02:00 committed by GitHub
parent 1aaf314678
commit c59fcae117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
/// <reference path="fourslash.ts"/>
////foo(1, /* comment */ );
format.document();
verify.currentFileContentIs(`foo(1, /* comment */);`);

View File

@ -0,0 +1,6 @@
/// <reference path="fourslash.ts"/>
////new Foo(1, /* comment */ );
format.document();
verify.currentFileContentIs(`new Foo(1, /* comment */);`);

View File

@ -0,0 +1,6 @@
/// <reference path="fourslash.ts"/>
////new Foo(1, );
format.document();
verify.currentFileContentIs(`new Foo(1,);`);