-
Notifications
You must be signed in to change notification settings - Fork 745
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
Simplify JSX and TSX lexers #1492
Conversation
@louisrli In the course of trying to fix the TSX lexer, I ended up writing a version that didn't rely on the JSX lexer at all. I thought it was a significant enough improvement that I essentially replaced the JSX lexer with it. It now appears to lex your buggy case properly: Let me know if you spot anything amiss! |
As a note: I'll merge this into master by making separate PRs that are more targeted. This is mainly intended as a proof of concept of how the new architecture would fit together. |
Looking at this a few weeks after writing it, I don't think it is a problem to merge this as a single commit. |
fbefe54
to
ebb9ad1
Compare
ebb9ad1
to
c375d0d
Compare
The JSX lexer handles interpolation by running an embedded version of itself. This is not the manner in which other lexers handle interpolation and so makes maintenance difficult. It also greatly complicates subclassing the lexer (a practical concern because the TSX lexer subclasses the JSX lexer). To fix this, this commit rewrites the JSX lexer to simplify its structure. In particular, by specifying `:interpol` and `:interpol_inner` states, the need for an embedded version of the lexer is obviated.
The JSX lexer is implemented with an embedded version of itself that is used for lexing interpolated text. This is not the manner in which other lexers handle interpolation and so makes maintenance difficult as patterns developed in other lexers for handling interpolation cannot be brought across. It also makes subclassing the lexer difficult (a practical concern because the TSX lexer subclasses the JSX lexer).
To fix this, this PR rewrites the JSX lexer. By using two interpolation states, it obviates the need for an embedded version.
In the course of implementation, it was discovered that the JavaScript lexer contains a bug in the:statement
state. This state contains a rule for lexing braces but this rule causes the:object
state to never be closed. The bug was discovered in the implementation of the rewrite because braces are used to begin and end interpolation.Having a simplified implementation of the JSX lexer makes it straightforward to implement a more robust TSX lexer. The PR includes a version of the TSX lexer that fixes a bug where generics that are expressed in the form<T,>
would not be lexed correctly.This fixes #1517.