Skip to content

Commit 768c9ed

Browse files
committed
Handle jsx Opening, Closing and Self closing tags
1 parent edad317 commit 768c9ed

14 files changed

+55
-43
lines changed

src/services/findAllReferences.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ namespace ts.FindAllReferences {
4848
return getDeclarationForDeclarationSpan(node);
4949
}
5050

51-
// TODO(shkamat)::
52-
// JSXOpeningElement or JSXElement for tagName ?
5351
if (!node.parent) return undefined;
5452

5553
if (!isDeclaration(node.parent) && !isExportAssignment(node.parent)) {
54+
// Jsx Tags
55+
if (isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) {
56+
return node.parent.parent;
57+
}
58+
else if (isJsxSelfClosingElement(node.parent)) {
59+
return node.parent;
60+
}
61+
5662
// Special property assignment in javascript
5763
if (isInJSFile(node)) {
5864
const binaryExpression = isBinaryExpression(node.parent) ?

src/services/services.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,8 +1577,14 @@ namespace ts {
15771577
const node = getTouchingPropertyName(sourceFile, position);
15781578
if (isIdentifier(node) && (isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) && isIntrinsicJsxName(node.escapedText)) {
15791579
const { openingElement, closingElement } = node.parent.parent;
1580-
return [openingElement, closingElement].map((node): RenameLocation =>
1581-
({ fileName: sourceFile.fileName, textSpan: createTextSpanFromNode(node.tagName, sourceFile) }));
1580+
return [openingElement, closingElement].map(node => {
1581+
const result: RenameLocation = {
1582+
fileName: sourceFile.fileName,
1583+
textSpan: createTextSpanFromNode(node.tagName, sourceFile)
1584+
};
1585+
FindAllReferences.setDeclarationSpan(result, sourceFile, node.parent);
1586+
return result;
1587+
});
15821588
}
15831589
else {
15841590
return getReferencesWorker(node, position, { findInStrings, findInComments, providePrefixAndSuffixTextForRename, isForRename: true },

tests/cases/fourslash/findReferencesJSXTagName.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
////[|import { [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}SubmissionComp|] } from "./RedditSubmission"|]
55
////function displaySubreddit(subreddit: string) {
66
//// let components = submissions
7-
//// .map((value, index) => <[|SubmissionComp|] key={ index } elementPosition= { index } {...value.data} />);
7+
//// .map((value, index) => [|<[|{| "declarationRangeIndex": 2 |}SubmissionComp|] key={ index } elementPosition= { index } {...value.data} />|]);
88
////}
99

1010
// @Filename: RedditSubmission.ts
11-
////export const [|[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 3 |}SubmissionComp|] = (submission: SubmissionProps) =>
11+
////export const [|[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}SubmissionComp|] = (submission: SubmissionProps) =>
1212
//// <div style|]={{ fontFamily: "sans-serif" }}></div>;
1313

14-
const [r0Def, r0, r1, r2Def, r2] = test.ranges();
14+
const [r0Def, r0, r1Def, r1, r2Def, r2] = test.ranges();
1515
const imports = { definition: "(alias) const SubmissionComp: (submission: any) => any\nimport SubmissionComp", ranges: [r0, r1] };
1616
const def = { definition: "const SubmissionComp: (submission: any) => any", ranges: [r2] };
1717
verify.referenceGroups([r0, r1], [imports, def]);

tests/cases/fourslash/findReferencesJSXTagName3.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
////}
1212
////
1313
////[|const [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 2 |}Comp|] = () =>
14-
//// <[|div|]>
14+
//// [|<[|{| "declarationRangeIndex": 4 |}div|]>
1515
//// Some content
16-
//// <[|div|]>More content</[|div|]>
17-
//// </[|div|]>;|]
16+
//// [|<[|{| "declarationRangeIndex": 6 |}div|]>More content</[|{| "declarationRangeIndex": 6 |}div|]>|]
17+
//// </[|{| "declarationRangeIndex": 4 |}div|]>|];|]
1818
////
19-
////const x = <[|Comp|]>
19+
////const x = [|<[|{| "declarationRangeIndex": 10 |}Comp|]>
2020
//// Content
21-
////</[|Comp|]>;
21+
////</[|{| "declarationRangeIndex": 10 |}Comp|]>|];
2222

23-
const [d0Def, d0, c0Def, c0, d1, d2, d3, d4, c1, c2] = test.ranges();
23+
const [d0Def, d0, c0Def, c0, d1Def, d1, d2Def, d2, d3, d4, c1Def, c1, c2] = test.ranges();
2424

2525
const allD = [d0, d1, d2, d3, d4];
2626
const allC = [c0, c1, c2];

tests/cases/fourslash/tsxFindAllReferences1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//// span: { n: string; };
1212
//// }
1313
//// }
14-
//// var x = <[|div|] />;
14+
//// var x = [|<[|{| "declarationRangeIndex": 2 |}div|] />|];
1515

1616
verify.singleReferenceGroup(
1717
`(property) JSX.IntrinsicElements.div: {

tests/cases/fourslash/tsxFindAllReferences4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
//// }|]
1515
////
1616
////
17-
//// var x = <[|MyClass|] name='hello'></[|MyClass|]>;
17+
//// var x = [|<[|{| "declarationRangeIndex" : 2 |}MyClass|] name='hello'></[|{| "declarationRangeIndex" : 2 |}MyClass|]>|];
1818

1919
verify.singleReferenceGroup("class MyClass", "MyClass");

tests/cases/fourslash/tsxFindAllReferences5.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
//// optional?: boolean
1717
//// }
1818
//// [|declare function [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}Opt|](attributes: OptionPropBag): JSX.Element;|]
19-
//// let opt = <[|Opt|] />;
20-
//// let opt1 = <[|Opt|] propx={100} propString />;
21-
//// let opt2 = <[|Opt|] propx={100} optional/>;
22-
//// let opt3 = <[|Opt|] wrong />;
23-
//// let opt4 = <[|Opt|] propx={100} propString="hi" />;
19+
//// let opt = [|<[|{| "declarationRangeIndex": 2 |}Opt|] />|];
20+
//// let opt1 = [|<[|{| "declarationRangeIndex": 4 |}Opt|] propx={100} propString />|];
21+
//// let opt2 = [|<[|{| "declarationRangeIndex": 6 |}Opt|] propx={100} optional/>|];
22+
//// let opt3 = [|<[|{| "declarationRangeIndex": 8 |}Opt|] wrong />|];
23+
//// let opt4 = [|<[|{| "declarationRangeIndex": 10 |}Opt|] propx={100} propString="hi" />|];
2424

2525
verify.singleReferenceGroup(
2626
"function Opt(attributes: OptionPropBag): JSX.Element",

tests/cases/fourslash/tsxFindAllReferences8.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
//// [|declare function [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}MainButton|](buttonProps: ButtonProps): JSX.Element;|]
2424
//// [|declare function [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 2 |}MainButton|](linkProps: LinkProps): JSX.Element;|]
2525
//// [|declare function [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}MainButton|](props: ButtonProps | LinkProps): JSX.Element;|]
26-
//// let opt = <[|MainButton|] />;
27-
//// let opt = <[|MainButton|] children="chidlren" />;
28-
//// let opt = <[|MainButton|] onClick={()=>{}} />;
29-
//// let opt = <[|MainButton|] onClick={()=>{}} ignore-prop />;
30-
//// let opt = <[|MainButton|] goTo="goTo" />;
31-
//// let opt = <[|MainButton|] wrong />;
26+
//// let opt = [|<[|{| "declarationRangeIndex": 6 |}MainButton|] />|];
27+
//// let opt = [|<[|{| "declarationRangeIndex": 8 |}MainButton|] children="chidlren" />|];
28+
//// let opt = [|<[|{| "declarationRangeIndex": 10 |}MainButton|] onClick={()=>{}} />|];
29+
//// let opt = [|<[|{| "declarationRangeIndex": 12 |}MainButton|] onClick={()=>{}} ignore-prop />|];
30+
//// let opt = [|<[|{| "declarationRangeIndex": 14 |}MainButton|] goTo="goTo" />|];
31+
//// let opt = [|<[|{| "declarationRangeIndex": 16 |}MainButton|] wrong />|];
3232

3333
verify.singleReferenceGroup(
3434
"function MainButton(buttonProps: ButtonProps): JSX.Element (+2 overloads)",

tests/cases/fourslash/tsxFindAllReferencesUnionElementType1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//// }
2020

2121
//// [|var [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}SFCComp|] = SFC1 || SFC2;|]
22-
//// <[|SFCComp|] x={ "hi" } />
22+
//// [|<[|{| "declarationRangeIndex": 2 |}SFCComp|] x={ "hi" } />|]
2323

2424
verify.singleReferenceGroup(`var SFCComp: ((prop: {
2525
x: number;

tests/cases/fourslash/tsxFindAllReferencesUnionElementType2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
//// }
1919

2020
//// [|var [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}RCComp|] = RC1 || RC2;|]
21-
//// <[|RCComp|] />
21+
//// [|<[|{| "declarationRangeIndex": 2 |}RCComp|] />|]
2222

2323
verify.singleReferenceGroup("var RCComp: typeof RC1", "RCComp");

0 commit comments

Comments
 (0)