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

[Bug]: Typescript parser crash on as-style assertion on generic instantiation expression #15990

Open
ethanresnick opened this issue Sep 21, 2023 · 4 comments

Comments

@ethanresnick
Copy link

ethanresnick commented Sep 21, 2023

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

// produces a parser error, but is valid TS
const x = jsonParse<SomeType> as (str: string) => KeyType

jsonParse<SomeType> is an instantiation expression, which is followed by a cast. As also shown in the repl, the same code works when the cast is written with angle-bracket syntax.

See REPL: https://babel.dev/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=PTAEAcCcHsBMFcDGBTAzqAhhDlXMqPjJAFCLQB2qALqAB6gC8oAVqpQAo54A8AytAC2yACoBPcMgB8mdAAoakAFyhFASwoBzAJRMZAaWRjxkkiRCgA7tEgBrdADMNyMpRr0moHgurLVvjR09UENjCWk2Tm5kfiFRcKkgA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=typescript&prettier=false&targets=&version=7.22.20&externalPlugins=&assumptions={}

Configuration file name

No response

Configuration

See REPL.

Current and expected behavior

The code in the REPL (i.e. const x = jsonParse<SomeType> as (str: string) => KeyType) should parse successfully, as it's valid TS.

Environment

System:
OS: macOS 13.5.1
Binaries:
Node: 20.0.0 - ~/.nvm/versions/node/v20.0.0/bin/node
npm: 9.6.4 - ~/.nvm/versions/node/v20.0.0/bin/npm

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @ethanresnick! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

Ugh very annoying that this code has two completely different meanings in JavaScript and in TypeScript:

let x = foo < 2 > as
F.getter;

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Sep 21, 2023

However, given that both Babel and SWC parse it according to the ECMAScript spec even when parsing TypeScript, we might try to get the TS team to change their parsing.

microsoft/TypeScript#55806

NOTE: We should still be able to parse the example in the OP, but let's wait for a decision on the TS side.

@yamafaktory
Copy link

We've just faced the same issue today at work with the following repro case (we use Vite):

Valid on TS side

https://www.typescriptlang.org/play?ts=4.8.4#code/JYWwDg9gTgLgBAJQKYEMDGMA0cDecBiArgHYbATEDCE4FSx8AvnAGZQ1wBEUqGnA3AChBaCgGd4AWQCe1WsXrwAvHAA8AFUwA+ABRh2YMQC446gJRwlW3ILh24PGISjE1AE2AA3LXgBSAZQB5ADkAOgkoYGIAc2AWaT0DMQtGVQB6D28hRiERcXgAI0JgABs3GTlIBQZLNU1dCys4CpoqxQ1rFDECEjIKSroGDv4gA

Parsing fails with Babel

https://babel.dev/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=JYWwDg9gTgLgBAJQKYEMDGMA0cDecBiArgHYbATEDCE4FSx8AvnAGZQ1wBEUqGnA3AChBaCgGd4AWQCe1WsXrwAvHAA8AFUwA-ABRh2YMQC446gJRwlW3ILh24PGISjE1AE2AA3LXgBSAZQB5ADkAOgkoYGIAc2AWaT0DMQtGVQB6D28hRiERcXgAI0JgABs3GTlIBQZLNU1dCys4CpoqxQ1rFDECEjIKSroGDv4gA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=react%2Ctypescript&prettier=false&targets=&version=7.22.20&externalPlugins=&assumptions=%7B%7D

For the record, SWC fails to parse it too (https://play.swc.rs/?version=1.3.86&code=H4sIAAAAAAAAA22OzQrCMBCE73mKoScLRe82zUXwIKigfYHaHwnYTdlshVLy7rYKKugcZz5mxradY8GpLkpJMGLbUynW0cZNAdUkCGjYtYh4RqJUqdKRF%2ByHD5JB54lZdOw6v0YeIzMYFSZxLT0TdGXvZsTufDwsvbClq22GFx8j6NUcpyq8yy%2B9vVV%2FFp7NX77ODQr%2Fe3ry0wcTfPp32gAAAA%3D%3D&config=H4sIAAAAAAAAA1WPSw7CMAxE9z1F5DUL1AUL7sAhouBWQfkpdiWqqncnf2AXv5nJ2MckBLxIwV0c6ZmGICNhHHMitDuW70SA94Ckog4Ml64yZYnjhoWcVQCWcUXOIaT5Os8tAMZ7woQXaQgbs9rpZf%2BtVN6GiET%2FxmyVbjUjXxun1grWP7citlPyunWDG3xNvWx8DJoePVnuOD9fu6hdFgEAAA%3D%3D).

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

No branches or pull requests

5 participants