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

3.18.0 regression: Invalid code for nullish coalescing with type assertion #609

Closed
kryops opened this issue Apr 12, 2021 · 2 comments · Fixed by #610
Closed

3.18.0 regression: Invalid code for nullish coalescing with type assertion #609

kryops opened this issue Apr 12, 2021 · 2 comments · Fixed by #610

Comments

@kryops
Copy link

kryops commented Apr 12, 2021

After the 3.18.0 release, sucrase seems to produce invalid code when combining nullish coalescing with a type assertion:

This still works:

const foo = {
  bar: (baz ?? null) as any
}

This stopped working:

const foo = {
  bar: baz ?? null as any
}

https://sucrase.io/#code=const%20foo%20%3D%20%7B%0A%20%20bar%3A%20baz%20%3F%3F%20null%20as%20any%0A%7D

Thanks for looking into it!

@alangpierce
Copy link
Owner

Thanks for reporting! I think I've tracked down the problem and I should be able to release a fix soon.

alangpierce added a commit that referenced this issue Apr 12, 2021
Fixes #609

The original implementation of the Jest transform (#540) needed to remove
regions of code, but ran into issues with the optional chaining and nullish
coalescing transforms, since those transforms would still emit function calls
around tokens even if they were removed. The implementation fixed those issues
by disabling the optional chaining and nullish coalescing code emit in
`removeToken` and `removeInitialToken`. Unfortunately, this broke other cases,
like a nullish coalescing call immediately followed by a type token. The nullish
coalescing implementation expected `appendTokenSuffix` to be called on the last
token even though it was a type token.

The changes to `TokenProcessor` actually became unnecessary as of #608 since we
no longer are deleting a region of code, so I reverted the two methods back to
their original implementation, which fixes the issue.
alangpierce added a commit that referenced this issue Apr 12, 2021
Fixes #609

The original implementation of the Jest transform (#540) needed to remove
regions of code, but ran into issues with the optional chaining and nullish
coalescing transforms, since those transforms would still emit function calls
around tokens even if they were removed. The implementation fixed those issues
by disabling the optional chaining and nullish coalescing code emit in
`removeToken` and `removeInitialToken`. Unfortunately, this broke other cases,
like a nullish coalescing call immediately followed by a type token. The nullish
coalescing implementation expected `appendTokenSuffix` to be called on the last
token even though it was a type token.

The changes to `TokenProcessor` actually became unnecessary as of #608 since we
no longer are deleting a region of code, so I reverted the two methods back to
their original implementation, which fixes the issue.
@alangpierce
Copy link
Owner

The fix is now released as Sucrase 3.18.1. Thanks again, and sorry for the trouble!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants