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(es/parser): Re-lex << as two <-s if required #7439

Merged
merged 21 commits into from Jun 30, 2023
Merged

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented May 24, 2023

@kdy1 kdy1 added this to the Planned milestone May 24, 2023
@kdy1 kdy1 self-assigned this May 24, 2023
@sandersn
Copy link

I looked at this and found the source of the bug, but I'm not sure how to fix it. It's in expr.rs:691, near the end of parse_member_expr_new_expr_inner. For the example, assertType<<K ..., << gets lexed as LShift not <,< because the context has should_not_lex_lt_or_gt_as_type set (and maybe !in_type, I'm not sure).

Problem is: this context is correct at the time. The lexing actually happens in parse_primary_expr, which should not be in a type context. But by the time parse_primary_expr returns, the lexer has already read LShift, so on line 691, let type_args = if self.syntax().typescript() && is!(self, '<') { will fail because it's LShift not <.

The Typescript parser relies on contexts less heavily than swc, and its lexer has a reScanLessThanToken method that it calls at this point; the lexer backs up 1 character and changes its token to < if the current token is LShift. However, I don't know if swc has this capability. It's possible that you can change the context in order to cause rescanning of the current token, but my initial reading of the code says No.

@kdy1 could you advise on whether rescanning is a possibility in the swc lexer?

A third option would be massive lookahead, in which parse_primary_expr is parsed in a context with should_not_lex_lt_or_gt_as_type set to false, but I expect this would be expensive.

@kdy1 kdy1 changed the title fix(es/parser): Fix parsing of generic type arguments fix(es/parser): Re-lex << as two <-s if required Jun 30, 2023
@kdy1 kdy1 marked this pull request as ready for review June 30, 2023 05:24
@kdy1 kdy1 enabled auto-merge (squash) June 30, 2023 05:24
Copy link
Member Author

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_parser

@kdy1
Copy link
Member Author

kdy1 commented Jun 30, 2023

@sandersn Thank you! Your comment was very helpful.

Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit 6850372 into swc-project:main Jun 30, 2023
127 checks passed
@kdy1 kdy1 deleted the issue-7187 branch June 30, 2023 07:41
@kdy1 kdy1 modified the milestones: Planned, v1.3.68 Jul 5, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Parser error for type parameters containing a generic function signature
3 participants