-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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(typescript-estree): jsx comment parsing #703
fix(typescript-estree): jsx comment parsing #703
Conversation
2728d92
to
67a4e2e
Compare
b2d7a4d
to
8b0663c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the detailed write up @nathanmarks. The main priority I would have before merging this is ensuring that it is not going to cause any regressions in Prettier.
Please could you try applying the changes in your PR to Prettier to double check? From memory there is a fairly comprehensive set of tests for comments in JSX over there...
will do shortly and report back! just had a super busy week, |
8b0663c
to
b9b0a72
Compare
With my local
|
Codecov Report
@@ Coverage Diff @@
## master #703 +/- ##
==========================================
+ Coverage 94.21% 94.33% +0.12%
==========================================
Files 112 111 -1
Lines 4821 4593 -228
Branches 1336 1268 -68
==========================================
- Hits 4542 4333 -209
+ Misses 159 149 -10
+ Partials 120 111 -9
|
Awesome, thanks for verifying @nathanmarks! |
@JamesHenry did you have any thoughts RE the other points i brought up? Maybe it's fine how it is, I just don't want to deep dig a hole that will need more branching and digging in the future to handle edge cases like this. |
Fixes #643
Fixes #648
Fixes #841
The problem is that with the fix performed in #607, when you have something like this:
We currently perform
reScanJsxToken()
on the closing brace for thefoo
attribute value.This results in a
SyntaxKind.JsxText
token like this:So the
// two
never gets correctly identified. If we hadn't performed thereScanJsxToken()
on the}
closing brace for the JSX expression, the// two
would have been tokenized correctly.I have fixed this by changing the rescan call added in the PR referenced earlier to only apply if the JSX expression is inside a JSX element (which addresses the case it was added to fix to begin with).
Honestly, this feels like a bit of a band-aid solution. Admittedly, I don't have a lot of experience with the typescript scanner but I feel as though this is the best solution without reworking the method a bit. Looking at some of the tokens we end up with after rescanning sometimes, there seems to be some sacrifices made in order to keep this as simple as possible and just locate the comments (which is a totally fair and valid design decision).
I feel like a better way to make these rescan decisions would be to move forward to the next meaningful token before making the decision to rescan. Like, if we move forward past the
}
in this example:And then look at the AST for the next token (
http
which ends up asSyntaxKind.Identifier
without rescanning), the AST will tell us that we're on a JSXText node. So if we know the proceeding token was a closing brace for a JSXExpression, we can check the AST to see if we're on a JSXText node before making the decision to rescan. Even if the next token is a whitespace or a new line, the AST we have will already know that's a JSXText node.I think this is a better approach than basing it on the type of parent for the JSXExpression (the change I made to fix the issue in this PR), since the issue is that we don't want the jsx text nodes to get tokenized incorrectly (which is what resulted in the text after
http:
getting incorrectly picked up as a comment). We could also probably benefit from this approach for the>
token logic too.If you want, I can update this PR and use the technique described above instead of the current fix I have applied.
Anyways, I have no experience working with the TS scanner output so I could be way off in my conclusions here, but the fact that we end up with tokens identified as
JsxText
that look like}http://example.com
(notice the}
included) also indicates that we're a little off in the approach here (although one can argue that for the sake of extracting the comments it's irrelevant).