8
8
import { F , O } from "@eslint-react/tools" ;
9
9
import type { RuleContext } from "@eslint-react/types" ;
10
10
import { type TSESTree } from "@typescript-eslint/utils" ;
11
- import { match } from "ts-pattern" ;
11
+ import { match , P } from "ts-pattern" ;
12
12
13
13
import { isCreateElementCall } from "./element" ;
14
14
@@ -32,7 +32,7 @@ export const JSXValueCheckHint = {
32
32
* @param hint The `JSXValueCheckHint` to use
33
33
* @returns boolean
34
34
*/
35
- // eslint-disable-next-line sonarjs/cognitive-complexity
35
+
36
36
export function isJSXValue (
37
37
node : TSESTree . Node | null | undefined ,
38
38
context : RuleContext ,
@@ -48,33 +48,17 @@ export function isJSXValue(
48
48
. with ( { type : NodeType . JSXMemberExpression } , F . constTrue )
49
49
. with ( { type : NodeType . JSXNamespacedName } , F . constTrue )
50
50
. with ( { type : NodeType . Literal } , ( node ) => {
51
- if ( ! ( "value" in node ) ) {
52
- return false ;
53
- }
54
-
55
- if ( hint & JSXValueCheckHint . SkipNullLiteral && node . value === null ) {
56
- return false ;
57
- }
58
-
59
- if ( hint & JSXValueCheckHint . SkipStringLiteral && typeof node . value === "string" ) {
60
- return false ;
61
- }
62
-
63
- // eslint-disable-next-line sonarjs/prefer-single-boolean-return
64
- if ( hint & JSXValueCheckHint . SkipNumberLiteral && typeof node . value === "number" ) {
65
- return false ;
66
- }
67
-
68
- return true ;
51
+ return match ( node . value )
52
+ . with ( null , ( ) => ! ( hint & JSXValueCheckHint . SkipNullLiteral ) )
53
+ . with ( "" , F . constFalse )
54
+ . with ( P . string , ( ) => ! ( hint & JSXValueCheckHint . SkipStringLiteral ) )
55
+ . with ( P . number , ( ) => ! ( hint & JSXValueCheckHint . SkipNumberLiteral ) )
56
+ . otherwise ( F . constFalse ) ;
69
57
} )
70
58
. with ( { type : NodeType . TemplateLiteral } , ( ) => {
71
59
return ! ( hint & JSXValueCheckHint . SkipStringLiteral ) ;
72
60
} )
73
61
. with ( { type : NodeType . ArrayExpression } , ( node ) => {
74
- if ( ! ( "elements" in node ) ) {
75
- return false ;
76
- }
77
-
78
62
if ( hint & JSXValueCheckHint . StrictArray ) {
79
63
return node . elements . every ( ( n ) => isJSXValue ( n , context , hint ) ) ;
80
64
}
@@ -105,16 +89,9 @@ export function isJSXValue(
105
89
return leftHasJSX ( node ) || rightHasJSX ( node ) ;
106
90
} )
107
91
. with ( { type : NodeType . LogicalExpression } , ( node ) => {
108
- if ( ! ( "left" in node ) ) {
109
- return false ;
110
- }
111
-
112
92
return isJSXValue ( node . left , context , hint ) || isJSXValue ( node . right , context , hint ) ;
113
93
} )
114
94
. with ( { type : NodeType . SequenceExpression } , ( node ) => {
115
- if ( ! ( "expressions" in node ) ) {
116
- return false ;
117
- }
118
95
const exp = node . expressions . at ( - 1 ) ;
119
96
120
97
return isJSXValue ( exp , context , hint ) ;
@@ -136,7 +113,7 @@ export function isJSXValue(
136
113
return F . pipe (
137
114
maybeVariable ,
138
115
O . flatMap ( getVariableInit ( 0 ) ) ,
139
- O . filter ( isOneOf ( [ NodeType . JSXElement , NodeType . JSXFragment ] ) ) ,
116
+ O . filter ( n => isJSXValue ( n , context , hint ) ) ,
140
117
O . isSome ,
141
118
) ;
142
119
} )
0 commit comments