Skip to content

Remove extra checkDefined in visitEachChildOfJsxExpression #52482

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 1 commit into from
Jan 30, 2023

Conversation

jakebailey
Copy link
Member

Fixes #52479

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jan 28, 2023
@jakebailey
Copy link
Member Author

jakebailey commented Jan 28, 2023

Checked all of the other checkDefineds in this section; this was the only wrong one. I'll have to look into something we can do to checkDefined that makes it fail if you give it something that's definitely defined.

EDIT: No, that's the opposite of the problem; we would need something which checks whether or not a result of a checkDefined is used only in a place which already accepts undefined. That's not really easily doable.

(I was able to find one other place that calls checkDefined on a definitely-defined value, but that is harmless and not what I really want.)

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 29, 2023

we would need something which checks whether or not a result of a checkDefined is used only in a place which already accepts undefined.

Would this fit the bill?

export function checkDefined<T, U extends T | null | undefined>(
    value: T | null | undefined,
    message?: string,
    stackCrawlMark?: AnyFunction
): U extends NonNullable<U> ? U : unknown {
    assertIsDefined(value, message, stackCrawlMark || checkDefined);
    return (value satisfies NonNullable<T> as any);
}

@DanielRosenwasser
Copy link
Member

FWIW, if I do that, here are the errors I see in visitorPublic.ts:

src/compiler/visitorPublic.ts:655:13 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string | BindingName'.

655             Debug.checkDefined(nodeVisitor(node.name, visitor, isBindingName)),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/compiler/visitorPublic.ts:678:13 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string | PropertyName'.

678             Debug.checkDefined(nodeVisitor(node.name, visitor, isPropertyName)),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/compiler/visitorPublic.ts:1055:13 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'BinaryOperator | BinaryOperatorToken'.

1055             tokenVisitor ? Debug.checkDefined(nodeVisitor(node.operatorToken, tokenVisitor, isBinaryOperatorToken)) : node.operatorToken,
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/compiler/visitorPublic.ts:1457:13 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'Expression | undefined'.

1457             Debug.checkDefined(nodeVisitor(node.expression, visitor, isExpression)));
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@jakebailey
Copy link
Member Author

The first three appear to be cases where our factories accept string or SyntaxKind for convenience (but can't actually appear during visit), so there's something weird happening with that signature, I think.

The last one is this bug, so it did catch that at least. But I'm guessing if there were more, it would have caught them too (with the false positives present).

@jakebailey
Copy link
Member Author

Another way to do this would be to transform all Debug.checkDefined calls to ! and then see if eslint complains, but that is of course a bit more work.

@jakebailey
Copy link
Member Author

I wrote the above transform and then ran eslint; this is in fact the only place we use checkDefined on a definitely defined value. Good to know that we don't have any more of these kinds of potential bugs.

@jakebailey jakebailey merged commit 39a9ac8 into microsoft:main Jan 30, 2023
@jakebailey jakebailey deleted the fix-52479 branch January 30, 2023 01:08
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.

JSX comments break Transformer API in TS 5.0 beta
3 participants