Skip to content

Commit 3bc36ff

Browse files
committed
refactor(jsx): improve isJSXValue
1 parent 4d38484 commit 3bc36ff

File tree

7 files changed

+18
-41
lines changed

7 files changed

+18
-41
lines changed

examples/with-flat-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react-dom": "^18.2.0"
1515
},
1616
"devDependencies": {
17-
"@eslint-react/eslint-plugin": "latest",
17+
"@eslint-react/eslint-plugin": "0.7.4",
1818
"@tsconfig/node20": "20.1.2",
1919
"@tsconfig/strictest": "2.0.2",
2020
"@types/react": "18.2.36",

examples/with-legacy-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react-dom": "^18.2.0"
1515
},
1616
"devDependencies": {
17-
"@eslint-react/eslint-plugin": "latest",
17+
"@eslint-react/eslint-plugin": "0.7.4",
1818
"@tsconfig/node20": "20.1.2",
1919
"@tsconfig/strictest": "2.0.2",
2020
"@types/react": "18.2.36",

examples/with-stylistic-eslint-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react-dom": "^18.2.0"
1515
},
1616
"devDependencies": {
17-
"@eslint-react/eslint-plugin": "latest",
17+
"@eslint-react/eslint-plugin": "0.7.4",
1818
"@stylistic/eslint-plugin": "1.0.1",
1919
"@tsconfig/node20": "20.1.2",
2020
"@tsconfig/strictest": "2.0.2",

packages/jsx/src/value.ts

+9-32
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { F, O } from "@eslint-react/tools";
99
import type { RuleContext } from "@eslint-react/types";
1010
import { type TSESTree } from "@typescript-eslint/utils";
11-
import { match } from "ts-pattern";
11+
import { match, P } from "ts-pattern";
1212

1313
import { isCreateElementCall } from "./element";
1414

@@ -32,7 +32,7 @@ export const JSXValueCheckHint = {
3232
* @param hint The `JSXValueCheckHint` to use
3333
* @returns boolean
3434
*/
35-
// eslint-disable-next-line sonarjs/cognitive-complexity
35+
3636
export function isJSXValue(
3737
node: TSESTree.Node | null | undefined,
3838
context: RuleContext,
@@ -48,33 +48,17 @@ export function isJSXValue(
4848
.with({ type: NodeType.JSXMemberExpression }, F.constTrue)
4949
.with({ type: NodeType.JSXNamespacedName }, F.constTrue)
5050
.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);
6957
})
7058
.with({ type: NodeType.TemplateLiteral }, () => {
7159
return !(hint & JSXValueCheckHint.SkipStringLiteral);
7260
})
7361
.with({ type: NodeType.ArrayExpression }, (node) => {
74-
if (!("elements" in node)) {
75-
return false;
76-
}
77-
7862
if (hint & JSXValueCheckHint.StrictArray) {
7963
return node.elements.every((n) => isJSXValue(n, context, hint));
8064
}
@@ -105,16 +89,9 @@ export function isJSXValue(
10589
return leftHasJSX(node) || rightHasJSX(node);
10690
})
10791
.with({ type: NodeType.LogicalExpression }, (node) => {
108-
if (!("left" in node)) {
109-
return false;
110-
}
111-
11292
return isJSXValue(node.left, context, hint) || isJSXValue(node.right, context, hint);
11393
})
11494
.with({ type: NodeType.SequenceExpression }, (node) => {
115-
if (!("expressions" in node)) {
116-
return false;
117-
}
11895
const exp = node.expressions.at(-1);
11996

12097
return isJSXValue(exp, context, hint);
@@ -136,7 +113,7 @@ export function isJSXValue(
136113
return F.pipe(
137114
maybeVariable,
138115
O.flatMap(getVariableInit(0)),
139-
O.filter(isOneOf([NodeType.JSXElement, NodeType.JSXFragment])),
116+
O.filter(n => isJSXValue(n, context, hint)),
140117
O.isSome,
141118
);
142119
})

pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/components/TwoslashPatch.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function TwoslashPatchPortal() {
1111

1212
return (
1313
<div ref={cb} id={`twoslash-patch-holder-${id}`} hidden>
14-
{!!isReady && createPortal(
14+
{isReady && createPortal(
1515
<style id={`twoslash-patch-${id}`}>
1616
{currentStyle}
1717
</style>,

website/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ts-pattern": "5.0.5"
1919
},
2020
"devDependencies": {
21-
"@eslint-react/eslint-plugin": "latest",
21+
"@eslint-react/eslint-plugin": "0.7.4",
2222
"@mdx-js/mdx": "3.0.0",
2323
"@types/node": "20.9.0",
2424
"@types/react": "18.2.36",

0 commit comments

Comments
 (0)