Skip to content

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

Merged
merged 3 commits into from
Mar 28, 2022

Conversation

andrewbranch
Copy link
Member

Fixes bug tracked in Azure DevOps found in VS testing. Thanks @zkat for helping figure out a test repro.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 28, 2022
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.

Comment on lines +1259 to +1260
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;
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.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

LGTM

@andrewbranch andrewbranch merged commit d962091 into microsoft:main Mar 28, 2022
@andrewbranch andrewbranch deleted the bug/format-on-brace branch March 28, 2022 20:50
@DanielRosenwasser
Copy link
Member

@typescript-bot cherry-pick this to release-4.6

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 29, 2022

Heya @DanielRosenwasser, I've started to run the task to cherry-pick this into release-4.6 on this PR at 19ef6b6. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

Hey @DanielRosenwasser, I've opened #48477 for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants