Skip to content

Commit 9163143

Browse files
committed
Fix newline inserted in empty block at end of formatting range
1 parent 0cc3535 commit 9163143

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/services/formatting/formatting.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -439,20 +439,21 @@ namespace ts.formatting {
439439
}
440440

441441
if (previousRange! && formattingScanner.getStartPos() >= originalRange.end) {
442-
const token =
442+
const tokenInfo =
443443
formattingScanner.isOnEOF() ? formattingScanner.readEOFTokenRange() :
444444
formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(enclosingNode).token :
445445
undefined;
446446

447-
if (token) {
447+
if (tokenInfo) {
448+
const parent = findPrecedingToken(tokenInfo.end, sourceFile, enclosingNode) || previousParent!;
448449
processPair(
449-
token,
450-
sourceFile.getLineAndCharacterOfPosition(token.pos).line,
451-
enclosingNode,
450+
tokenInfo,
451+
sourceFile.getLineAndCharacterOfPosition(tokenInfo.pos).line,
452+
parent,
452453
previousRange,
453454
previousRangeStartLine!,
454455
previousParent!,
455-
enclosingNode,
456+
parent,
456457
/*dynamicIndentation*/ undefined);
457458
}
458459
}

src/services/utilities.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,10 @@ namespace ts {
12561256
* Finds the rightmost token satisfying `token.end <= position`,
12571257
* excluding `JsxText` tokens containing only whitespace.
12581258
*/
1259-
export function findPrecedingToken(position: number, sourceFile: SourceFile, startNode?: Node, excludeJsdoc?: boolean): Node | undefined {
1260-
const result = find(startNode || sourceFile);
1259+
export function findPrecedingToken(position: number, sourceFile: SourceFileLike, startNode: Node, excludeJsdoc?: boolean): Node | undefined;
1260+
export function findPrecedingToken(position: number, sourceFile: SourceFile, startNode?: Node, excludeJsdoc?: boolean): Node | undefined;
1261+
export function findPrecedingToken(position: number, sourceFile: SourceFileLike, startNode?: Node, excludeJsdoc?: boolean): Node | undefined {
1262+
const result = find(startNode! || sourceFile);
12611263
Debug.assert(!(result && isWhiteSpaceOnlyJsxText(result)));
12621264
return result;
12631265

@@ -1322,7 +1324,7 @@ namespace ts {
13221324
return isToken(n) && !isWhiteSpaceOnlyJsxText(n);
13231325
}
13241326

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
//// if (foo) {
4+
//// if (bar) {/**/}
5+
//// }
6+
7+
goTo.marker("");
8+
format.onType("", "{");
9+
// format.document();
10+
verify.currentFileContentIs(
11+
`if (foo) {
12+
if (bar) { }
13+
}`);

0 commit comments

Comments
 (0)