Skip to content

Commit b6640e9

Browse files
author
Andy
authored
getJsxClosingTagAtPosition: Return a result if parent has same name and is unclosed (#25557)
1 parent 3dd1d25 commit b6640e9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/services/services.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,11 +1884,16 @@ namespace ts {
18841884
if (!token) return undefined;
18851885
const element = token.kind === SyntaxKind.GreaterThanToken && isJsxOpeningElement(token.parent) ? token.parent.parent
18861886
: isJsxText(token) ? token.parent : undefined;
1887-
if (element && !tagNamesAreEquivalent(element.openingElement.tagName, element.closingElement.tagName)) {
1887+
if (element && isUnclosedTag(element)) {
18881888
return { newText: `</${element.openingElement.tagName.getText(sourceFile)}>` };
18891889
}
18901890
}
18911891

1892+
function isUnclosedTag({ openingElement, closingElement, parent }: JsxElement): boolean {
1893+
return !tagNamesAreEquivalent(openingElement.tagName, closingElement.tagName) ||
1894+
isJsxElement(parent) && tagNamesAreEquivalent(openingElement.tagName, parent.openingElement.tagName) && isUnclosedTag(parent);
1895+
}
1896+
18921897
function getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan | undefined {
18931898
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
18941899
const range = formatting.getRangeOfEnclosingComment(sourceFile, position);

tests/cases/fourslash/autoCloseTag.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
/// <reference path='fourslash.ts' />
22

3-
// @Filename: /a.tsx
3+
// Using separate files for each example to avoid unclosed JSX tags affecting other tests.
4+
5+
// @Filename: /0.tsx
46
////const x = <div>/*0*/;
7+
8+
// @Filename: /1.tsx
59
////const x = <div> foo/*1*/ </div>;
10+
11+
// @Filename: /2.tsx
612
////const x = <div></div>/*2*/;
13+
14+
// @Filename: /3.tsx
715
////const x = <div/>/*3*/;
16+
17+
// @Filename: /4.tsx
818
////const x = <div>
919
//// <p>/*4*/
1020
//// </div>
1121
////</p>;
22+
23+
// @Filename: /5.tsx
1224
////const x = <div> text /*5*/;
1325

26+
// @Filename: /6.tsx
27+
////const x = <div>
28+
//// <div>/*6*/
29+
////</div>;
30+
31+
// @Filename: /7.tsx
32+
////const x = <div>
33+
//// <p>/*7*/
34+
////</div>;
35+
36+
// @Filename: /8.tsx
37+
////const x = <div>
38+
//// <div>/*8*/</div>
39+
////</div>;
40+
1441
verify.jsxClosingTag({
1542
0: { newText: "</div>" },
1643
1: undefined,
1744
2: undefined,
1845
3: undefined,
1946
4: { newText: "</p>" },
47+
5: { newText: "</div>" },
48+
6: { newText: "</div>" },
49+
7: { newText: "</p>" },
50+
8: undefined,
2051
});

0 commit comments

Comments
 (0)