Skip to content

Commit fbc2b67

Browse files
TypeScript Botandrewbranch
TypeScript Bot
andauthored
Cherry-pick PR #48463 into release-4.6 (#48477)
Component commits: 9163143 Fix newline inserted in empty block at end of formatting range 79b600a Clean up 19ef6b6 Fix refactoring mistake Co-authored-by: Andrew Branch <[email protected]>
1 parent 2bed482 commit fbc2b67

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-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)?.parent || 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) as Node);
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,12 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
//// if (foo) {
4+
//// if (bar) {/**/}
5+
//// }
6+
7+
goTo.marker("");
8+
format.onType("", "{");
9+
verify.currentFileContentIs(
10+
`if (foo) {
11+
if (bar) { }
12+
}`);

0 commit comments

Comments
 (0)