@@ -26,46 +26,51 @@ const rule: TSESLint.RuleModule<"preferShowAnd" | "preferShowTernary", []> = {
26
26
return node . type === "JSXElement" || node . type === "JSXFragment" ? text : `{${ text } }` ;
27
27
} ;
28
28
29
+ const logicalExpressionHandler = ( node : T . LogicalExpression ) => {
30
+ if ( node . operator === "&&" && EXPENSIVE_TYPES . includes ( node . right . type ) ) {
31
+ context . report ( {
32
+ node,
33
+ messageId : "preferShowAnd" ,
34
+ fix : ( fixer ) =>
35
+ fixer . replaceText (
36
+ node . parent ?. type === "JSXExpressionContainer" &&
37
+ node . parent . parent ?. type === "JSXElement"
38
+ ? node . parent
39
+ : node ,
40
+ `<Show when={${ sourceCode . getText ( node . left ) } }>${ putIntoJSX ( node . right ) } </Show>`
41
+ ) ,
42
+ } ) ;
43
+ }
44
+ } ;
45
+ const conditionalExpressionHandler = ( node : T . ConditionalExpression ) => {
46
+ if (
47
+ EXPENSIVE_TYPES . includes ( node . consequent . type ) ||
48
+ EXPENSIVE_TYPES . includes ( node . alternate . type )
49
+ ) {
50
+ context . report ( {
51
+ node,
52
+ messageId : "preferShowTernary" ,
53
+ fix : ( fixer ) =>
54
+ fixer . replaceText (
55
+ node . parent ?. type === "JSXExpressionContainer" &&
56
+ node . parent . parent ?. type === "JSXElement"
57
+ ? node . parent
58
+ : node ,
59
+ `<Show when={${ sourceCode . getText ( node . test ) } } fallback={${ sourceCode . getText (
60
+ node . alternate
61
+ ) } }>${ putIntoJSX ( node . consequent ) } </Show>`
62
+ ) ,
63
+ } ) ;
64
+ }
65
+ } ;
66
+
29
67
return {
30
- "JSXElement > JSXExpressionContainer > LogicalExpression" : ( node : T . LogicalExpression ) => {
31
- if ( node . operator === "&&" && EXPENSIVE_TYPES . includes ( node . right . type ) ) {
32
- context . report ( {
33
- node,
34
- messageId : "preferShowAnd" ,
35
- fix : ( fixer ) =>
36
- fixer . replaceText (
37
- node . parent ?. type === "JSXExpressionContainer" &&
38
- node . parent . parent ?. type === "JSXElement"
39
- ? node . parent
40
- : node ,
41
- `<Show when={${ sourceCode . getText ( node . left ) } }>${ putIntoJSX ( node . right ) } </Show>`
42
- ) ,
43
- } ) ;
44
- }
45
- } ,
46
- "JSXElement > JSXExpressionContainer > ConditionalExpression" : (
47
- node : T . ConditionalExpression
48
- ) => {
49
- if (
50
- EXPENSIVE_TYPES . includes ( node . consequent . type ) ||
51
- EXPENSIVE_TYPES . includes ( node . alternate . type )
52
- ) {
53
- context . report ( {
54
- node,
55
- messageId : "preferShowTernary" ,
56
- fix : ( fixer ) =>
57
- fixer . replaceText (
58
- node . parent ?. type === "JSXExpressionContainer" &&
59
- node . parent . parent ?. type === "JSXElement"
60
- ? node . parent
61
- : node ,
62
- `<Show when={${ sourceCode . getText ( node . test ) } } fallback={${ sourceCode . getText (
63
- node . alternate
64
- ) } }>${ putIntoJSX ( node . consequent ) } </Show>`
65
- ) ,
66
- } ) ;
67
- }
68
- } ,
68
+ "JSXElement > JSXExpressionContainer > LogicalExpression" : logicalExpressionHandler ,
69
+ "JSXElement > JSXExpressionContainer > ArrowFunctionExpression > LogicalExpression" :
70
+ logicalExpressionHandler ,
71
+ "JSXElement > JSXExpressionContainer > ConditionalExpression" : conditionalExpressionHandler ,
72
+ "JSXElement > JSXExpressionContainer > ArrowFunctionExpression > ConditionalExpression" :
73
+ conditionalExpressionHandler ,
69
74
} ;
70
75
} ,
71
76
} ;
0 commit comments