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

[tsx] <T extends U></T>; is not parsed correctly #47355

Closed
nicolo-ribaudo opened this issue Jan 8, 2022 · 7 comments
Closed

[tsx] <T extends U></T>; is not parsed correctly #47355

nicolo-ribaudo opened this issue Jan 8, 2022 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@nicolo-ribaudo
Copy link

Bug Report

🔎 Search Terms

jsx extends ambiguity type parameters

🕗 Version & Regression Information

I only tested 4.5.4 and Nighty on the repl

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "JSX and React"

⏯ Playground Link

Playground link with relevant code

💻 Code

// This works
<T implements U></T>;

// This doesn't
<T extends U></T>;

🙁 Actual behavior

The second JSX expression is parsed as a malformed arrow function

🙂 Expected behavior

Both are valid JSX elements

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 11, 2022
@RyanCavanaugh
Copy link
Member

In this you'd need to write <T U extends>, etc instead. Otherwise we'd have to do a lookahead of the entire rest of the file.

@Josh-Cena
Copy link
Contributor

Seems Babel handles it correctly 🤔

@RyanCavanaugh
Copy link
Member

The Babel behavior is surprising to say the least

This is parsed as a <T>...</T> JSX element with the content "(x: T) => ", console.log('hi'), "f(); //"

let f = <T extends U>(x: T) => { console.log('hi') }

f();

//</T>

This is parsed as a generic arrow function:

let f = <T extends U>(x: T) => { console.log('hi'); }

f();

//</T>

Not sure I would call this "correctly" - it appears to be entirely heuristical

@Josh-Cena
Copy link
Contributor

Ah, that's interesting to know😄

@nicolo-ribaudo
Copy link
Author

nicolo-ribaudo commented Jan 12, 2022

Yeah, Babel first tries to parse it as JSX and then falls back to an arrow function. In your specific example, the semicolon make console.log('hi'); an invalid expression that cannot be used in JSX interpolations. With the current Babel implementation, can force it to always be an arrow by adding a trailing comma (<T extends U,>), similarly to how you need a trailing comma to disambiguate <T>.

I reported this "bug" while checking what need to do to align to your parser.

@Josh-Cena
Copy link
Contributor

FWIW, I actually like Babel's result more. It's better to be parsed as correctly as possible before spitting an error.

@fatcerberus
Copy link

Downside of the Babel behavior is that it basically requires infinite lookahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants