File tree Expand file tree Collapse file tree 2 files changed +8
-42
lines changed
Expand file tree Collapse file tree 2 files changed +8
-42
lines changed Original file line number Diff line number Diff line change 22// Licensed under the MIT License.
33
44import { elementType } from "jsx-ast-utils" ;
5- import { TSESLint , TSESTree } from "@typescript-eslint/utils" ;
5+ import { TSESLint } from "@typescript-eslint/utils" ; // Assuming context comes from TSESLint
66import { JSXOpeningElement } from "estree-jsx" ;
77
8- // Type guard to check if a node is a JSXElement
9- const isJSXElement = ( node : TSESTree . Node ) : node is TSESTree . JSXElement => {
10- return node . type === "JSXElement" ;
11- } ;
12-
138const hasToolTipParent = ( context : TSESLint . RuleContext < string , unknown [ ] > ) : boolean => {
149 const ancestors = context . getAncestors ( ) ;
1510
1611 if ( ! ancestors || ancestors . length === 0 ) {
1712 return false ;
1813 }
1914
20- // Iterate through ancestors and return false if a non-JSXElement is found before a Tooltip
21- for ( const item of ancestors ) {
22- if ( ! isJSXElement ( item ) ) {
23- return false ; // Stop if a non-JSXElement is encountered
24- }
25-
26- const { openingElement } = item ;
27- if (
28- openingElement &&
29- openingElement . type === "JSXOpeningElement" &&
30- elementType ( openingElement as unknown as JSXOpeningElement ) === "Tooltip"
31- ) {
32- return true ; // Return true if we find a Tooltip
33- }
34- }
35-
36- return false ;
15+ return ancestors . some (
16+ item =>
17+ item . type === "JSXElement" &&
18+ item . openingElement &&
19+ item . openingElement . type === "JSXOpeningElement" &&
20+ elementType ( item . openingElement as unknown as JSXOpeningElement ) === "Tooltip"
21+ ) ;
3722} ;
3823
3924export { hasToolTipParent } ;
Original file line number Diff line number Diff line change @@ -67,23 +67,4 @@ describe("hasToolTipParent", () => {
6767 const result = hasToolTipParent ( mockContext ) ;
6868 expect ( result ) . toBe ( true ) ;
6969 } ) ;
70-
71- test ( "should return false when the ancestor is not a JSXElement" , ( ) => {
72- const mockAncestors = [
73- {
74- type : "Literal" // Not a JSXElement
75- } ,
76- {
77- type : "JSXElement" ,
78- openingElement : {
79- type : "JSXOpeningElement" ,
80- name : { name : "Tooltip" } // Tooltip exists but first ancestor is invalid
81- }
82- }
83- ] ;
84- ( mockContext . getAncestors as jest . Mock ) . mockReturnValue ( mockAncestors ) ;
85-
86- const result = hasToolTipParent ( mockContext ) ;
87- expect ( result ) . toBe ( false ) ;
88- } ) ;
8970} ) ;
You can’t perform that action at this time.
0 commit comments