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

fix(js_parser): desambigaute JSX and const type param of arrow function #845

Merged

Conversation

Conaclos
Copy link
Member

@Conaclos Conaclos commented Nov 23, 2023

Summary

Fix #846.

This fixes a parsing error that rejected the following valid code when TypeScript and JSX are both enabled:

<const T,>() => {};

See these two comments for some context:

Test Plan

I added tests and updated snapshots.

@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 13:12 — with GitHub Actions Inactive
@Conaclos Conaclos requested a review from a team November 23, 2023 13:12
@github-actions github-actions bot added A-Parser Area: parser L-JavaScript Language: JavaScript and super languages labels Nov 23, 2023
Copy link
Contributor

github-actions bot commented Nov 23, 2023

Parser conformance results on

js/262

Test result main count This PR count Difference
Total 49701 49701 0
Passed 48721 48721 0
Failed 980 980 0
Panics 0 0 0
Coverage 98.03% 98.03% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6322 6322 0
Passed 2036 2036 0
Failed 4286 4286 0
Panics 0 0 0
Coverage 32.20% 32.20% 0.00%

ts/babel

Test result main count This PR count Difference
Total 662 662 0
Passed 592 592 0
Failed 70 70 0
Panics 0 0 0
Coverage 89.43% 89.43% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 17646 17646 0
Passed 13454 13453 ❌ ⏬ -1
Failed 4190 4191 ❌ ⏫ +1
Panics 2 2 0
Coverage 76.24% 76.24% -0.01%
🔥 Regression (1):
compiler/parseJsxExtends2.ts

@Conaclos Conaclos force-pushed the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch from 38a29ab to bfc622c Compare November 23, 2023 13:20
@github-actions github-actions bot added the A-Website Area: website label Nov 23, 2023
@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 13:20 — with GitHub Actions Inactive
@Conaclos Conaclos force-pushed the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch from bfc622c to 8441d2e Compare November 23, 2023 13:20
@github-actions github-actions bot added the A-Changelog Area: changelog label Nov 23, 2023
@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 13:20 — with GitHub Actions Inactive
@Conaclos Conaclos force-pushed the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch from 8441d2e to 7bef185 Compare November 23, 2023 13:40
@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 13:40 — with GitHub Actions Inactive
if JsSyntaxFeature::Jsx.is_supported(p) {
// Disambiguate between JSX and type parameters
// Type parameters of arrow functions accept only the `const` modifier.
let n = if p.nth(n + 1) == T![const] { n + 1 } else { n };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
You can use p.nth_at here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. By the way, why do we have this helper? Using nth(<n>) = <kind> looks cleaner?

Copy link
Contributor

@denbezrukov denbezrukov Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, we can discuss this. I'm inclined to use one specific approach. However, I was under the impression that using nth_at would be preferable. I'm open to hearing your thoughts on this.

p.nth(T![abstract]) && p.nth(T![class])
p.cur() == T![abstract] && p.cur() == T![class]
p.nth_at(n, T![abstract]) && p.nth_at(n + 1, T![class])
p.nth(n) == T![abstract] && p.nth(n + 1) == T![class]
matches!(p.nth(n), T![abstract] | T![const]) && matches!(p.nth(n + 1), T![class] | T![const)
p.nth_at_ts(n, FIRST_SET) && p.nth_at_ts(n + 1, SECOND_SET)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah for consistency I updated the code. However, I think we should remove this helper: we actually end with more characters and a layer of indirection (in a future PR)...

@ematipico
Copy link
Member

@Conaclos Is the regression expected?

@Conaclos
Copy link
Member Author

Is the regression expected?

No. I updated the code to avoid the regression. However, the post seems not updated yet?

@Conaclos Conaclos force-pushed the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch from 7bef185 to 4238aa1 Compare November 23, 2023 14:41
@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 14:41 — with GitHub Actions Inactive
@Conaclos Conaclos force-pushed the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch from 4238aa1 to e1058c2 Compare November 23, 2023 14:53
@Conaclos Conaclos temporarily deployed to Website deployment November 23, 2023 14:53 — with GitHub Actions Inactive
@Conaclos
Copy link
Member Author

@ematipico Taking a closer look to the regression it is expected. In fact TypeScript expects an error because the tag T is not declared. We previously emitted an error because we were expecting an arrow function. Now we correctly parse it to a JSX element.

@Conaclos Conaclos merged commit d1434c8 into main Nov 23, 2023
18 checks passed
@Conaclos Conaclos deleted the conaclos/js-parser/desambiguate-jsx-type-param-arrow-function branch November 23, 2023 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Parser Area: parser A-Website Area: website L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Adding a const modifier for the first type parameter of an arrow function produces a parsing error
3 participants