-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Merged
andrewbranch
merged 3 commits into
microsoft:main
from
hi-ogawa:45010-auto-complete-jsx-fragment
Sep 8, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// Using separate files for each example to avoid unclosed JSX tags affecting other tests. | ||
|
||
// @Filename: /0.tsx | ||
////const x = <>/*0*/; | ||
|
||
// @Filename: /1.tsx | ||
////const x = <> foo/*1*/ </>; | ||
|
||
// @Filename: /2.tsx | ||
////const x = <></>/*2*/; | ||
|
||
// @Filename: /3.tsx | ||
////const x = </>/*3*/; | ||
|
||
// @Filename: /4.tsx | ||
////const x = <div> | ||
//// <>/*4*/ | ||
//// </div> | ||
////</>; | ||
|
||
// @Filename: /5.tsx | ||
////const x = <> text /*5*/; | ||
|
||
// @Filename: /6.tsx | ||
////const x = <> | ||
//// <>/*6*/ | ||
////</>; | ||
|
||
// @Filename: /7.tsx | ||
////const x = <div> | ||
//// <>/*7*/ | ||
////</div>; | ||
|
||
// @Filename: /8.tsx | ||
////const x = <div> | ||
//// <>/*8*/</> | ||
////</div>; | ||
|
||
verify.jsxClosingTag({ | ||
0: { newText: "</>" }, | ||
1: undefined, | ||
2: undefined, | ||
3: undefined, | ||
4: { newText: "</>" }, | ||
5: { newText: "</>" }, | ||
6: { newText: "</>" }, | ||
7: { newText: "</>" }, | ||
8: undefined, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 necessarilyJsxElement
but it also can beJsxFragment
, even though typing-wise it's supposed to beJsxElement
according toTypeScript/src/compiler/types.ts
Lines 2601 to 2605 in 8362a0f
Without this change,
isUnclosedTag({ openingElement, ... })
below will lead to runtime error sinceopeningElement
becomesundefined
, for example, in the test caseconst x = <> foo/*1*/ </>
(see@Filename: /1.tsx
inautoCloseFragment.ts
).Probably, a legitimate fix would be something like defining
type JsxParent = JsxElement | JsxFragment
and replacing existing member typesparent: JsxElement
withparent: 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 beJsxElement | 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.
Uh oh!
There was an error while loading. Please reload this page.
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 includeJsxFragment
since I believe that's what it should be, then there was one type error insrc/services/completions.ts
, so I did a trivial fix for that.Overall, I think there aren't much use
JsxText.parent
orJsxExpression.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.