Skip to content

Commit 854462d

Browse files
author
Andy
authored
Fix formatting at trailing comma (#25706)
1 parent 6d8a5f6 commit 854462d

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/harness/fourslash.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,14 @@ Actual: ${stringify(fullActual)}`);
23072307
}
23082308
}
23092309

2310+
public verifyFormatDocumentChangesNothing(): void {
2311+
const { fileName } = this.activeFile;
2312+
const before = this.getFileContent(fileName);
2313+
this.formatDocument();
2314+
const after = this.getFileContent(fileName);
2315+
this.assertObjectsEqual(after, before);
2316+
}
2317+
23102318
public verifyTextAtCaretIs(text: string) {
23112319
const actual = this.getFileContent(this.activeFile.fileName).substring(this.currentCaretPosition, this.currentCaretPosition + text.length);
23122320
if (actual !== text) {
@@ -4206,6 +4214,10 @@ namespace FourSlashInterface {
42064214
this.state.verifyCurrentFileContent(text);
42074215
}
42084216

4217+
public formatDocumentChangesNothing(): void {
4218+
this.state.verifyFormatDocumentChangesNothing();
4219+
}
4220+
42094221
public goToDefinitionIs(endMarkers: ArrayOrSingle<string>) {
42104222
this.state.verifyGoToDefinitionIs(endMarkers);
42114223
}

src/services/formatting/formatting.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,6 @@ namespace ts.formatting {
717717
Debug.assert(isNodeArray(nodes));
718718

719719
const listStartToken = getOpenTokenForList(parent, nodes);
720-
const listEndToken = getCloseTokenForOpenToken(listStartToken);
721720

722721
let listDynamicIndentation = parentDynamicIndentation;
723722
let startLine = parentStartLine;
@@ -752,17 +751,21 @@ namespace ts.formatting {
752751
inheritedIndentation = processChildNode(child, inheritedIndentation, node, listDynamicIndentation, startLine, startLine, /*isListItem*/ true, /*isFirstListItem*/ i === 0);
753752
}
754753

755-
if (listEndToken !== SyntaxKind.Unknown) {
756-
if (formattingScanner.isOnToken()) {
757-
const tokenInfo = formattingScanner.readTokenInfo(parent);
758-
// consume the list end token only if it is still belong to the parent
759-
// there might be the case when current token matches end token but does not considered as one
760-
// function (x: function) <--
761-
// without this check close paren will be interpreted as list end token for function expression which is wrong
762-
if (tokenInfo.token.kind === listEndToken && rangeContainsRange(parent, tokenInfo.token)) {
763-
// consume list end token
764-
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
765-
}
754+
const listEndToken = getCloseTokenForOpenToken(listStartToken);
755+
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken()) {
756+
let tokenInfo = formattingScanner.readTokenInfo(parent);
757+
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
758+
formattingScanner.advance();
759+
tokenInfo = formattingScanner.readTokenInfo(parent);
760+
}
761+
762+
// consume the list end token only if it is still belong to the parent
763+
// there might be the case when current token matches end token but does not considered as one
764+
// function (x: function) <--
765+
// without this check close paren will be interpreted as list end token for function expression which is wrong
766+
if (tokenInfo.token.kind === listEndToken && rangeContainsRange(parent, tokenInfo.token)) {
767+
// consume list end token
768+
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
766769
}
767770
}
768771
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////foo
4+
//// .bar(
5+
//// x,
6+
//// );
7+
8+
verify.formatDocumentChangesNothing();

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ declare namespace FourSlashInterface {
228228
eval(expr: string, value: any): void;
229229
currentLineContentIs(text: string): void;
230230
currentFileContentIs(text: string): void;
231+
formatDocumentChangesNothing(): void;
231232
/** Verifies that goToDefinition at the current position would take you to `endMarker`. */
232233
goToDefinitionIs(endMarkers: ArrayOrSingle<string>): void;
233234
goToDefinitionName(name: string, containerName: string): void;

0 commit comments

Comments
 (0)