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 2
2
// Licensed under the MIT License.
3
3
4
4
import { 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
6
6
import { JSXOpeningElement } from "estree-jsx" ;
7
7
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
-
13
8
const hasToolTipParent = ( context : TSESLint . RuleContext < string , unknown [ ] > ) : boolean => {
14
9
const ancestors = context . getAncestors ( ) ;
15
10
16
11
if ( ! ancestors || ancestors . length === 0 ) {
17
12
return false ;
18
13
}
19
14
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
+ ) ;
37
22
} ;
38
23
39
24
export { hasToolTipParent } ;
Original file line number Diff line number Diff line change @@ -67,23 +67,4 @@ describe("hasToolTipParent", () => {
67
67
const result = hasToolTipParent ( mockContext ) ;
68
68
expect ( result ) . toBe ( true ) ;
69
69
} ) ;
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
- } ) ;
89
70
} ) ;
You can’t perform that action at this time.
0 commit comments