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

TypeScript: x < y >= z is incorrectly interpreted as a type argument list #7387

Closed
evanw opened this issue May 13, 2023 · 1 comment · Fixed by #7388
Closed

TypeScript: x < y >= z is incorrectly interpreted as a type argument list #7387

evanw opened this issue May 13, 2023 · 1 comment · Fixed by #7388
Labels
Milestone

Comments

@evanw
Copy link

evanw commented May 13, 2023

Describe the bug

SWC's interpretation of type arguments in TypeScript code is different than the TypeScript compiler, which can cause SWC to generate incorrect JavaScript.

Input code

fn(x < y, x >= y)

Config

(I only ran SWC via the playground)

Playground link

https://play.swc.rs/?version=1.3.57&code=H4sIAAAAAAAAA0vL06hQsFGo1FGoULCzVajU5AIAIT%2B1gxIAAAA%3D&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3CHK0KF36CEQdSIqfsKOVBTl7oUE0maz38fveRVSwps0PORaxrJElQjTuReEsmf1KQhwjkg6mchw6yxTpSZlCXdoOxhglWbk6kIah3FoDrAhEHZHw5zxZsr%2FmTq4mJDoKqxS5WeL10TRUsGF17KT7Zfa92hwh5%2Boh52HwdCzOzktKLYvIO%2B2AhcBAAA%3D

Expected behavior

SWC should produce this to match the TypeScript compiler:

fn(x < y, x >= y);

Actual behavior

SWC produces this instead, which is incorrect:

fn(x = y);

Version

1.3.57

Additional context

Specifically TypeScript's parseTypeArgumentsInExpression function backtracks unless the trailing > comes from a > real token. This is subtle because TypeScript's type argument parser normally strips off the leading > from a token, which is why >> works in a<b<c>>(d). But it turns out that should only be done when you're in a type context. When you're in an expression context, the trailing > must not come from a token that has anything else after it. So for example a<b<c>>=(d) should not be considered a type argument list because parseTypeArgumentsInExpression encounters a >= token.

I just fixed this bug in esbuild: evanw/esbuild#3111. I'm reporting it here because SWC appears to also have this issue. The bug does not introduce a syntax error, which means the problematic output might be missed before releasing it, so I considered this bug to be higher severity.

@swc-bot
Copy link
Collaborator

swc-bot commented Jun 14, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Jun 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants