Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix newline inserted in empty block at end of formatting range #48463

Merged
merged 3 commits into from Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/services/formatting/formatting.ts
Expand Up @@ -439,20 +439,21 @@ namespace ts.formatting {
}

if (previousRange! && formattingScanner.getStartPos() >= originalRange.end) {
const token =
const tokenInfo =
formattingScanner.isOnEOF() ? formattingScanner.readEOFTokenRange() :
formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(enclosingNode).token :
undefined;

if (token) {
if (tokenInfo) {
const parent = findPrecedingToken(tokenInfo.end, sourceFile, enclosingNode)?.parent || previousParent!;
processPair(
token,
sourceFile.getLineAndCharacterOfPosition(token.pos).line,
enclosingNode,
tokenInfo,
sourceFile.getLineAndCharacterOfPosition(tokenInfo.pos).line,
parent,
previousRange,
previousRangeStartLine!,
previousParent!,
enclosingNode,
parent,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TLDR is this final parameter is supposed to be the “context node” of the token, as if anyone knows what that means, and enclosingNode may be arbitrarily higher up the tree. Turns out that “context node” means “direct parent” in almost all cases. What was happening in this case was that contextNode should have been the inner if Block ({}), but it was actually the whole outer IfStatement (if (foo) { ... }). The rule NewLineBeforeCloseBraceInBlockContext is supposed to take effect only if the block ending in the close brace is not on a single line. The inner one is, so the rule should not have applied, but it was using the outer one to decide whether to apply the rule.

/*dynamicIndentation*/ undefined);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/services/utilities.ts
Expand Up @@ -1256,8 +1256,10 @@ namespace ts {
* Finds the rightmost token satisfying `token.end <= position`,
* excluding `JsxText` tokens containing only whitespace.
*/
export function findPrecedingToken(position: number, sourceFile: SourceFile, startNode?: Node, excludeJsdoc?: boolean): Node | undefined {
const result = find(startNode || sourceFile);
export function findPrecedingToken(position: number, sourceFile: SourceFileLike, startNode: Node, excludeJsdoc?: boolean): Node | undefined;
export function findPrecedingToken(position: number, sourceFile: SourceFile, startNode?: Node, excludeJsdoc?: boolean): Node | undefined;
Comment on lines +1259 to +1260
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why couldn't this just change to SourceFileLike? Seems like it'd be a backwards compatible change to accept a wider set of types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't notice that startNode becomes required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourceFileLike has no parse tree structure; it’s just a text container. So if you provide a SourceFileLike you must provide an actual Node to start in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just didn't notice the one character ? difference, oops.

export function findPrecedingToken(position: number, sourceFile: SourceFileLike, startNode?: Node, excludeJsdoc?: boolean): Node | undefined {
const result = find((startNode || sourceFile) as Node);
Debug.assert(!(result && isWhiteSpaceOnlyJsxText(result)));
return result;

Expand Down Expand Up @@ -1322,7 +1324,7 @@ namespace ts {
return isToken(n) && !isWhiteSpaceOnlyJsxText(n);
}

function findRightmostToken(n: Node, sourceFile: SourceFile): Node | undefined {
function findRightmostToken(n: Node, sourceFile: SourceFileLike): Node | undefined {
if (isNonWhitespaceToken(n)) {
return n;
}
Expand All @@ -1339,7 +1341,7 @@ namespace ts {
/**
* Finds the rightmost child to the left of `children[exclusiveStartPosition]` which is a non-all-whitespace token or has constituent tokens.
*/
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number, sourceFile: SourceFile, parentKind: SyntaxKind): Node | undefined {
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number, sourceFile: SourceFileLike, parentKind: SyntaxKind): Node | undefined {
for (let i = exclusiveStartPosition - 1; i >= 0; i--) {
const child = children[i];

Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/formatOnTypeOpenCurlyWithBraceCompletion.ts
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts"/>

//// if (foo) {
//// if (bar) {/**/}
//// }

goTo.marker("");
format.onType("", "{");
verify.currentFileContentIs(
`if (foo) {
if (bar) { }
}`);