Fix formatting at trailing comma (#25706)

This commit is contained in:
Andy 2018-07-17 15:21:35 -07:00 committed by GitHub
parent 6d8a5f6288
commit 854462d383
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 12 deletions

View file

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

View file

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

View file

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////foo
//// .bar(
//// x,
//// );
verify.formatDocumentChangesNothing();

View file

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