Fix formatting at trailing comma (#25706)
This commit is contained in:
parent
6d8a5f6288
commit
854462d383
|
@ -2307,6 +2307,14 @@ Actual: ${stringify(fullActual)}`);
|
|||
}
|
||||
}
|
||||
|
||||
public verifyFormatDocumentChangesNothing(): void {
|
||||
const { fileName } = this.activeFile;
|
||||
const before = this.getFileContent(fileName);
|
||||
this.formatDocument();
|
||||
const after = this.getFileContent(fileName);
|
||||
this.assertObjectsEqual(after, before);
|
||||
}
|
||||
|
||||
public verifyTextAtCaretIs(text: string) {
|
||||
const actual = this.getFileContent(this.activeFile.fileName).substring(this.currentCaretPosition, this.currentCaretPosition + text.length);
|
||||
if (actual !== text) {
|
||||
|
@ -4206,6 +4214,10 @@ namespace FourSlashInterface {
|
|||
this.state.verifyCurrentFileContent(text);
|
||||
}
|
||||
|
||||
public formatDocumentChangesNothing(): void {
|
||||
this.state.verifyFormatDocumentChangesNothing();
|
||||
}
|
||||
|
||||
public goToDefinitionIs(endMarkers: ArrayOrSingle<string>) {
|
||||
this.state.verifyGoToDefinitionIs(endMarkers);
|
||||
}
|
||||
|
|
|
@ -717,7 +717,6 @@ namespace ts.formatting {
|
|||
Debug.assert(isNodeArray(nodes));
|
||||
|
||||
const listStartToken = getOpenTokenForList(parent, nodes);
|
||||
const listEndToken = getCloseTokenForOpenToken(listStartToken);
|
||||
|
||||
let listDynamicIndentation = parentDynamicIndentation;
|
||||
let startLine = parentStartLine;
|
||||
|
@ -752,17 +751,21 @@ namespace ts.formatting {
|
|||
inheritedIndentation = processChildNode(child, inheritedIndentation, node, listDynamicIndentation, startLine, startLine, /*isListItem*/ true, /*isFirstListItem*/ i === 0);
|
||||
}
|
||||
|
||||
if (listEndToken !== SyntaxKind.Unknown) {
|
||||
if (formattingScanner.isOnToken()) {
|
||||
const tokenInfo = formattingScanner.readTokenInfo(parent);
|
||||
// consume the list end token only if it is still belong to the parent
|
||||
// there might be the case when current token matches end token but does not considered as one
|
||||
// function (x: function) <--
|
||||
// without this check close paren will be interpreted as list end token for function expression which is wrong
|
||||
if (tokenInfo.token.kind === listEndToken && rangeContainsRange(parent, tokenInfo.token)) {
|
||||
// consume list end token
|
||||
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
|
||||
}
|
||||
const listEndToken = getCloseTokenForOpenToken(listStartToken);
|
||||
if (listEndToken !== SyntaxKind.Unknown && formattingScanner.isOnToken()) {
|
||||
let tokenInfo = formattingScanner.readTokenInfo(parent);
|
||||
if (tokenInfo.token.kind === SyntaxKind.CommaToken && isCallLikeExpression(parent)) {
|
||||
formattingScanner.advance();
|
||||
tokenInfo = formattingScanner.readTokenInfo(parent);
|
||||
}
|
||||
|
||||
// consume the list end token only if it is still belong to the parent
|
||||
// there might be the case when current token matches end token but does not considered as one
|
||||
// function (x: function) <--
|
||||
// without this check close paren will be interpreted as list end token for function expression which is wrong
|
||||
if (tokenInfo.token.kind === listEndToken && rangeContainsRange(parent, tokenInfo.token)) {
|
||||
// consume list end token
|
||||
consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
8
tests/cases/fourslash/formatTrailingComma.ts
Normal file
8
tests/cases/fourslash/formatTrailingComma.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////foo
|
||||
//// .bar(
|
||||
//// x,
|
||||
//// );
|
||||
|
||||
verify.formatDocumentChangesNothing();
|
|
@ -228,6 +228,7 @@ declare namespace FourSlashInterface {
|
|||
eval(expr: string, value: any): void;
|
||||
currentLineContentIs(text: string): void;
|
||||
currentFileContentIs(text: string): void;
|
||||
formatDocumentChangesNothing(): void;
|
||||
/** Verifies that goToDefinition at the current position would take you to `endMarker`. */
|
||||
goToDefinitionIs(endMarkers: ArrayOrSingle<string>): void;
|
||||
goToDefinitionName(name: string, containerName: string): void;
|
||||
|
|
Loading…
Reference in a new issue