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

Regression: Cannot parse arrow fn exprs in conditional expr cons and alt in object literals in TypeScript #4067

Closed
dsherret opened this issue Mar 17, 2022 · 10 comments · Fixed by #4138
Labels
Milestone

Comments

@dsherret
Copy link
Contributor

dsherret commented Mar 17, 2022

Describe the bug

Regression of #2174

#3685 (comment)

This syntax no longer parses, yet it is valid JavaScript. Maybe the TS compiler can't figure out how to parse it right now(Edit: it can figure this out now), but it works in V8.

https://astexplorer.net/#/gist/d8417911fb6ea22a87b63d441b2a0e87/20b56c10cf6e34b3e7ddc60e9328702e09c930fd

cc @williamtetlow

Input code

const x = {
    prop: true
        ? fn => ({ })
            : fn => true,
};

Config

No response

Playground link

https://play.swc.rs/?version=1.2.151&code=H4sIAAAAAAAAA0vOzysuUahQsFWo5uVSAIKCovwCK4WSotJUCB8E7BXS8hRs7RQ0qhVqNRHCIGAFlQJp0OHlqrUGAGjLrV1RAAAA&config=H4sIAAAAAAAAA0WOQQ6DMAwE%2F%2BIz1%2FaQP%2FCIKBiUipDIa6RGiL%2FXoaG9edfeWR%2F0QiB3UPECljahburf5EhrYQSJRWkghVmzX8GnCS8Lq50wHrZbcwaTU9l5oBS3ONcGCjkVYaDnbOW3ZeUbY5yUp70Zx9V18Z7Gg0oM%2Bot95Zgn%2Fn9wt%2FSTiLGj2hPnBwXfhU7WAAAA

Expected behavior

Should parse.

Actual behavior

error: Expected ':', got ','
 --> input.ts:4:25
  |
4 |             : fn => true,
  |                         ^



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

Version

1.2.151

Additional context

No response

@dsherret dsherret added the C-bug label Mar 17, 2022
@kdy1
Copy link
Member

kdy1 commented Mar 17, 2022

I think the patch for #2174 was removed for some reasons, but I don't remember the PR removed it.

@dsherret
Copy link
Contributor Author

It was in #3685. I think it's reasonable that this doesn't parse in TS given that the TS compiler can't figure it out, but it should still at least parse in JS.

@kdy1 kdy1 added this to the Planned milestone Mar 17, 2022
@dsherret
Copy link
Contributor Author

dsherret commented Mar 17, 2022

@williamtetlow yup, I missed that and my bad! I think though that we should still try to support this in TS because it might be fixed in tsc in the future.

That said, given that it parses in JS and the TS compiler itself has this bug then I think it's very low priority. Edit: It is fixed in the latest TS nightly.

@dsherret
Copy link
Contributor Author

@williamtetlow
Copy link
Contributor

No probs 🙂

For context, I can see why TSC doesn't support it at the moment as it's quite a difficult issue to fix.

The following will parse as we can tell that what comes after the : is the falsy expression.

true ? fn => { } : fn => 1

but when parsing

true ? fn => ({ }) : fn => true

This bit({ }) : fn => true could be the beginning of a type definition or the falsy expression and we assume the former the same way TSC does.

@williamtetlow
Copy link
Contributor

williamtetlow commented Mar 17, 2022

I just went to report this to TypeScript's repo and at the last second I discovered it is fixed in the latest TS nightly:

https://ts-ast-viewer.com/#code/MYewdgzgLgBAHjAvDA3gKBpmAHATibALhilwFcBTDLGgfhgDMwkA+GAChRgF8BKamoJjEmrEuQoAaNNwDcQA (select @next in Options > Version)

https://www.typescriptlang.org/play?ts=4.7.0-dev.20220302#code/MYewdgzgLgBAHjAvDA3gKBpmAHATibALhilwFcBTDLGgfhgDMwkA+GAChRgF8BKamoJjEmrEuQoAaNNwDcQA

Oh ok let me take a look at how they fixed it

@williamtetlow
Copy link
Contributor

It looks like pretty simple fix they just do the opposite now 😄 I can take a look

microsoft/TypeScript#47550

@dsherret
Copy link
Contributor Author

@williamtetlow oh nice! Thanks so much for looking into this 🙂

@dsherret dsherret changed the title Regression: Cannot parse arrow fn exprs in conditional expr cons and alt in this scenario Regression: Cannot parse arrow fn exprs in conditional expr cons and alt in object literals in TypeScript Mar 17, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.161 Mar 23, 2022
kdy1 pushed a commit that referenced this issue May 6, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 17, 2022

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 Oct 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4 participants