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

Modify debug assertion to avoid crashing on SyntaxList #47500

Merged
merged 3 commits into from Jan 19, 2022

Conversation

jakebailey
Copy link
Member

Fixes #47446
Fixes #21815
Fixes #46946

@typescript-bot typescript-bot added Author: Team For Backlog Bug PRs that fix a backlog bug For Milestone Bug PRs that fix a bug with a specific milestone labels Jan 19, 2022
*/
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number, sourceFile: SourceFile): Node | undefined {
function findRightmostChildNodeWithTokens(n: Node, exclusiveStartPosition: number, sourceFile: SourceFile): Node | undefined {
const children = n.getChildren(sourceFile);
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if I should bother leaving children as a parameter to save a little bit of processing; seemed more clear to me to just pass down the parent node and get the children, rather than passing the parent with its children (all call sites technically have the list).

Copy link
Member

Choose a reason for hiding this comment

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

It looks like n is only needed for its kind and that kind is only needed for an assert so, personally, I'd add a new parameter for the kind to make it clear that it's "extra".

Copy link
Member Author

Choose a reason for hiding this comment

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

Switched it to that.

for (let i = exclusiveStartPosition - 1; i >= 0; i--) {
const child = children[i];

if (isWhiteSpaceOnlyJsxText(child)) {
Debug.assert(i > 0, "`JsxText` tokens should not be the first child of `JsxElement | JsxSelfClosingElement`");
if (i === 0 && isWhiteSpaceOnlyJsxText(child)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why the index check moved into the if? That seems like a potentially big behavior change and I'm having trouble reasoning about its effect.

Copy link
Member Author

Choose a reason for hiding this comment

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

Shoot, you're right! I was trying to be too clever and optimize the assert, but I completely tunnel visioned away the code below. Will revert.

if (isWhiteSpaceOnlyJsxText(child)) {
Debug.assert(i > 0, "`JsxText` tokens should not be the first child of `JsxElement | JsxSelfClosingElement`");
if (i === 0 && isWhiteSpaceOnlyJsxText(child)) {
Debug.assert(!(isJsxElement(n) || isJsxSelfClosingElement(n)), "`JsxText` tokens should not be the first child of `JsxElement | JsxSelfClosingElement`");
}
else if (nodeHasTokens(children[i], sourceFile)) {
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 scope, but should children[i] be child here and below?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this seems like an oops. I can fix this, if desired. Shouldn't change anything.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe as a follow-up.

Comment on lines 1347 to 1348
if (parentKind === SyntaxKind.JsxText || parentKind === SyntaxKind.JsxSelfClosingElement) {
Debug.assert(i > 0, "`JsxText` tokens should not be the first child of `JsxElement | JsxSelfClosingElement`");
Copy link
Member

Choose a reason for hiding this comment

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

I might just fold the i > 0 into the above condition and turn this into a Debug.fail, but this is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's a reasonable change.

Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

Thanks!

@jakebailey jakebailey merged commit 049d4e0 into microsoft:main Jan 19, 2022
@jakebailey jakebailey deleted the fix-47446 branch January 19, 2022 21:37
@DanielRosenwasser
Copy link
Member

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 19, 2022

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

@typescript-bot
Copy link
Collaborator

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

typescript-bot pushed a commit to typescript-bot/TypeScript that referenced this pull request Jan 19, 2022
Component commits:
43c3f45 Modify debug assertion to avoid crashing on SyntaxList

9570a59 PR changes

e9a2355 More PR feedback
DanielRosenwasser pushed a commit that referenced this pull request Jan 19, 2022
Component commits:
43c3f45 Modify debug assertion to avoid crashing on SyntaxList

9570a59 PR changes

e9a2355 More PR feedback

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Backlog Bug PRs that fix a backlog bug For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
4 participants