-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix newline inserted in empty block at end of formatting range #48463
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why couldn't this just change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't notice that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I just didn't notice the one character |
||
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; | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -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]; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path="fourslash.ts"/> | ||
|
||
//// if (foo) { | ||
//// if (bar) {/**/} | ||
//// } | ||
|
||
goTo.marker(""); | ||
format.onType("", "{"); | ||
verify.currentFileContentIs( | ||
`if (foo) { | ||
if (bar) { } | ||
}`); |
There was a problem hiding this comment.
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 thatcontextNode
should have been the innerif
Block ({}
), but it was actually the whole outerIfStatement
(if (foo) { ... }
). The ruleNewLineBeforeCloseBraceInBlockContext
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.