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

>= or <= binary expression containing TS "as expression" fails to parse #9471

Open
dsherret opened this issue Aug 20, 2024 · 3 comments
Open
Labels
Milestone

Comments

@dsherret
Copy link
Contributor

dsherret commented Aug 20, 2024

Describe the bug

Binary expressions with >= or <= operators containing a TypeScript "as expression" fail to parse.

Input code

const a = testNum as number >= 0 // or <=, but < and > are fine

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.17-nightly-20240820.3&code=H4sIAAAAAAAAA0vOzysuUUhUsFUoSS0u8SvNVUgsVsgrzU1KLVKws1UwAAAPKf%2BNIAAAAA%3D%3D&config=H4sIAAAAAAAAA1WQvW7DMAyE9z6FoTmoCw8ZsrbIlilPQCh0w0J%2FICkgRuB3jyTHcbqJ3%2FF04N0%2Fus78iTWH7l6eZUjAgvyaC5EpKNwKMTolFMuU1OxWVaVKyhkbmRfBKPAvajWhDF%2FDYFbOEGSM7N8TsuAPjhTwGPnbgciR0F3k%2Be8rihGsvvkKIp8i6zlmtljDrqpJDn2P4j%2Fl2qfFstsMnIOSb7uQNXpQsuYpz%2F8ucDFK3RvBSb2tcePjJTvc6qqVLFfuzbZEgcZp9TZEclqdrav5AeQIxLB6AQAA

SWC Info output

No response

Expected behavior

Should parse

Actual behavior

  x Expression expected
   ,-[input.tsx:1:1]
 1 | const a = testNum as number >= 0
   :                              ^
   `----


Caused by:
    0: failed to process js file
    1: Syntax Error

Version

1.7.17-nightly-20240820.3

Additional context

No response

@magic-akari
Copy link
Member

Should it be fixed in #9313 ?

@kdy1
Copy link
Member

kdy1 commented Aug 21, 2024

It will be fixed by #9313, but I expect #9313 to take lots of time, mainly because I don't have time to work on it

@magic-akari
Copy link
Member

magic-akari commented Aug 21, 2024

Investigation

In the SWC parser, the function read_token_lt_gt checks if the next token is either < or > and decides whether to process any following =. In in_type mode, it immediately returns either < or >.

In the next_then_parse_ts_type function, in_type is set to true to enter the TS type parsing mode.
However, this in_type mode also affects the next cache which used to peek the next token,
for example, for the expression testNum as number >= 0, after parsing testNum as number, the next cache will store a > character.
For the expression testNum >= 0, once testNum is processed, the next token to be queried is, as expected, >=.

In other words, a peak operation might have been performed in next_then_parse_ts_type, potentially resulting in the incorrect handling of subsequent tokens.

Possible solution:
At the end of the next_then_parse_ts_type function, discard the next cache and re-read the token.

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

No branches or pull requests

3 participants