Skip to content

Commit d8e8626

Browse files
committed
refactor: minor improvements
1 parent 8fcab19 commit d8e8626

File tree

1 file changed

+29
-29
lines changed
  • packages/plugins/eslint-plugin-react-debug/src/rules

1 file changed

+29
-29
lines changed

packages/plugins/eslint-plugin-react-debug/src/rules/jsx.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
22
import type { CamelCase } from "string-ts";
33
import * as JSX from "@eslint-react/jsx";
44
import { JsxConfig, type RuleContext, type RuleFeature } from "@eslint-react/kit";
5+
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
56
import { match, P } from "ts-pattern";
67
import { JsxEmit } from "typescript";
78
import { createRule } from "../utils";
@@ -40,40 +41,39 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4041
...jsxConfigFromAnnotation,
4142
};
4243

43-
const getDescriptor = (type: string) => ({
44-
messageId: "jsx",
45-
data: {
46-
type,
47-
jsx: match(jsxConfig.jsx)
48-
.with(JsxEmit.None, () => "none")
49-
.with(JsxEmit.ReactJSX, () => "react-jsx")
50-
.with(JsxEmit.ReactJSXDev, () => "react-jsx-dev")
51-
.with(JsxEmit.React, () => "react")
52-
.with(JsxEmit.ReactNative, () => "react-native")
53-
.with(JsxEmit.Preserve, () => "preserve")
54-
.otherwise(() => "unknown"),
55-
jsxFactory: jsxConfig.jsxFactory,
56-
jsxFragmentFactory: jsxConfig.jsxFragmentFactory,
57-
jsxImportSource: jsxConfig.jsxImportSource,
58-
jsxRuntime: match(jsxConfig.jsx)
59-
.with(P.union(JsxEmit.None, JsxEmit.ReactJSX, JsxEmit.ReactJSXDev), () => "automatic")
60-
.otherwise(() => "classic"),
61-
},
62-
} as const);
44+
function getReportDescriptor(node: TSESTree.JSXElement | TSESTree.JSXFragment) {
45+
return {
46+
messageId: "jsx",
47+
node,
48+
data: {
49+
type: match(node)
50+
.with({ type: T.JSXElement }, (n) => JSX.isFragmentElement(n) ? "fragment" : "element")
51+
.with({ type: T.JSXFragment }, () => "fragment")
52+
.exhaustive(),
53+
jsx: match(jsxConfig.jsx)
54+
.with(JsxEmit.None, () => "none")
55+
.with(JsxEmit.ReactJSX, () => "react-jsx")
56+
.with(JsxEmit.ReactJSXDev, () => "react-jsx-dev")
57+
.with(JsxEmit.React, () => "react")
58+
.with(JsxEmit.ReactNative, () => "react-native")
59+
.with(JsxEmit.Preserve, () => "preserve")
60+
.otherwise(() => "unknown"),
61+
jsxFactory: jsxConfig.jsxFactory,
62+
jsxFragmentFactory: jsxConfig.jsxFragmentFactory,
63+
jsxImportSource: jsxConfig.jsxImportSource,
64+
jsxRuntime: match(jsxConfig.jsx)
65+
.with(P.union(JsxEmit.None, JsxEmit.ReactJSX, JsxEmit.ReactJSXDev), () => "automatic")
66+
.otherwise(() => "classic"),
67+
},
68+
} as const;
69+
}
6370

6471
return {
6572
JSXElement(node) {
66-
const isFragment = JSX.isFragmentElement(node);
67-
context.report({
68-
...getDescriptor(isFragment ? "fragment" : "element"),
69-
node,
70-
});
73+
context.report(getReportDescriptor(node));
7174
},
7275
JSXFragment(node) {
73-
context.report({
74-
...getDescriptor("fragment"),
75-
node,
76-
});
76+
context.report(getReportDescriptor(node));
7777
},
7878
};
7979
}

0 commit comments

Comments
 (0)