-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat(45010): handle unclosed fragment in getJsxClosingTagAtPosition
#45532
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
feat(45010): handle unclosed fragment in getJsxClosingTagAtPosition
#45532
Conversation
@@ -2088,10 +2088,15 @@ namespace ts { | |||
const token = findPrecedingToken(position, sourceFile); | |||
if (!token) return undefined; | |||
const element = token.kind === SyntaxKind.GreaterThanToken && isJsxOpeningElement(token.parent) ? token.parent.parent | |||
: isJsxText(token) ? token.parent : undefined; | |||
: isJsxText(token) && isJsxElement(token.parent) ? token.parent : undefined; |
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.
To comment on why this change is necessary, I found that JsxText.parent
is not necessarily JsxElement
but it also can be JsxFragment
, even though typing-wise it's supposed to be JsxElement
according to
TypeScript/src/compiler/types.ts
Lines 2601 to 2605 in 8362a0f
export interface JsxText extends LiteralLikeNode { | |
readonly kind: SyntaxKind.JsxText; | |
readonly parent: JsxElement; | |
readonly containsOnlyTriviaWhiteSpaces: boolean; | |
} |
Without this change, isUnclosedTag({ openingElement, ... })
below will lead to runtime error since openingElement
becomes undefined
, for example, in the test case const x = <> foo/*1*/ </>
(see @Filename: /1.tsx
in autoCloseFragment.ts
).
Probably, a legitimate fix would be something like defining type JsxParent = JsxElement | JsxFragment
and replacing existing member types parent: JsxElement
with parent: JsxParant
, but I'm not so sure yet that is what's supposed to be since I'm very new to the code base.
I would appreciate if someone familar with these parts can comment on this.
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.
As far as I can tell, I think this is a good way to fix this problem. I think maybe the original code just didn't consider JsxFragments
when it was written.
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.
@hi-ogawa did you try changing the type of parent
here to be JsxElement | JsxFragment
? I agree we should really fix that type. Would you mind trying it and seeing if you can resolve any other errors that come up?
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 for the feedback!
Well, I didn't try anything yet, but I'll see what I can do and get back to you with my findings.
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.
@andrewbranch Following your request, I addressed the issue with this commit 90a35e0.
There wasn't any type error related to JsxText.parent
so it looks alright.
I also changed the type of JsxExpression.parent
to include JsxFragment
since I believe that's what it should be, then there was one type error in src/services/completions.ts
, so I did a trivial fix for that.
Overall, I think there aren't much use JsxText.parent
or JsxExpression.parent
in the code base currently, so hopefully the type change won't have much impact.
But, one thing I worry about is that it's going to change public API (as you can see from baselines/references/api), so the library users will be also affected by this change.
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.
Could I ask an code-unrelated question?
And there is no related info on tsserver log, which you could see by run command |
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.
Looks very good, thanks! :)
@@ -2088,10 +2088,15 @@ namespace ts { | |||
const token = findPrecedingToken(position, sourceFile); | |||
if (!token) return undefined; | |||
const element = token.kind === SyntaxKind.GreaterThanToken && isJsxOpeningElement(token.parent) ? token.parent.parent | |||
: isJsxText(token) ? token.parent : undefined; | |||
: isJsxText(token) && isJsxElement(token.parent) ? token.parent : undefined; |
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.
As far as I can tell, I think this is a good way to fix this problem. I think maybe the original code just didn't consider JsxFragments
when it was written.
@@ -2088,10 +2088,15 @@ namespace ts { | |||
const token = findPrecedingToken(position, sourceFile); | |||
if (!token) return undefined; | |||
const element = token.kind === SyntaxKind.GreaterThanToken && isJsxOpeningElement(token.parent) ? token.parent.parent | |||
: isJsxText(token) ? token.parent : undefined; | |||
: isJsxText(token) && isJsxElement(token.parent) ? token.parent : undefined; |
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.
@hi-ogawa did you try changing the type of parent
here to be JsxElement | JsxFragment
? I agree we should really fix that type. Would you mind trying it and seeing if you can resolve any other errors that come up?
@ShuiRuTian I’m not sure, but the debug issue you were seeing might be solved by |
19cc4a3
to
90a35e0
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! I’m not concerned about the public API change—the API was wrong, and you’re fixing it. Consumers might have type errors, but it should help them catch bugs in their own code.
Awesome! Thank you two for the code review and triaging my first PR! |
Fixes #45010
This PR attempts to handle unclosed jsx fragment in language service so that it will auto-complete on vscode.
I used
NodeFlags.ThisNodeHasError
directly to detect the parse error related to the closing fragment, which might not be the most reliable way to check unclosed fragment, but at least for the test cases I added (mostly the copy of existingautoCloseTag.ts
), it seems to be working.Please let me know if this is a right approach or if there's a better way to do it.
I would appreciate any feedback, thanks a lot!