Skip to content

Commit 355d1cc

Browse files
committed
refactor(plugins/x): minor improvements
1 parent 8e2db56 commit 355d1cc

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AST_NODE_TYPES } from "@typescript-eslint/types";
88
import type { RuleFix, RuleFixer } from "@typescript-eslint/utils/ts-eslint";
99
import { compare } from "compare-versions";
1010
import type { CamelCase } from "string-ts";
11-
import { match } from "ts-pattern";
11+
import { match, P } from "ts-pattern";
1212

1313
import { createRule } from "../utils";
1414

@@ -78,45 +78,36 @@ function getComponentPropsFixes(
7878
.with({ type: AST_NODE_TYPES.Identifier }, (n) => O.some(`...${n.name}`))
7979
.with({ type: AST_NODE_TYPES.ObjectPattern }, (n) => O.some(n.properties.map(getText).join(", ")))
8080
.otherwise(O.none);
81-
if (O.isNone(fixedArg0Text)) return [];
81+
const fixedArg1Text = match(arg1)
82+
.with(P.nullish, () => O.some("ref"))
83+
.with({ type: AST_NODE_TYPES.Identifier, name: "ref" }, () => O.some("ref"))
84+
.with({ type: AST_NODE_TYPES.Identifier, name: P.not("ref") }, (n) => O.some(`ref: ${n.name}`))
85+
.otherwise(O.none);
86+
if (O.isNone(fixedArg0Text) || O.isNone(fixedArg1Text)) return [];
8287
const fixedPropsText = fixedArg0Text.value;
83-
if (!arg1) {
84-
return [fixer.replaceText(
85-
arg0,
86-
[
87-
"{",
88-
"ref,",
89-
fixedPropsText,
90-
"}",
91-
].join(" "),
92-
)] as const;
93-
}
94-
if (arg1.type !== AST_NODE_TYPES.Identifier) return [];
88+
const fixedRefText = fixedArg1Text.value;
9589
if (!typeArg0 || !typeArg1) {
9690
return [
9791
fixer.replaceText(
9892
arg0,
9993
[
10094
"{",
101-
arg1.name === "ref"
102-
? `ref,`
103-
: `ref: ${arg1.name},`,
95+
fixedRefText + ",",
10496
fixedPropsText,
10597
"}",
10698
].join(" "),
10799
),
108-
fixer.remove(arg1),
109-
fixer.removeRange([arg0.range[1], arg1.range[0]]),
100+
...arg1
101+
? [fixer.remove(arg1), fixer.removeRange([arg0.range[1], arg1.range[0]])]
102+
: [],
110103
] as const;
111104
}
112105
return [
113106
fixer.replaceText(
114107
arg0,
115108
[
116109
"{",
117-
arg1.name === "ref"
118-
? `ref,`
119-
: `ref: ${arg1.name},`,
110+
fixedRefText + ",",
120111
fixedPropsText,
121112
"}:",
122113
getText(typeArg1),
@@ -127,7 +118,8 @@ function getComponentPropsFixes(
127118
"}",
128119
].join(" "),
129120
),
130-
fixer.remove(arg1),
131-
fixer.removeRange([arg0.range[1], arg1.range[0]]),
121+
...arg1
122+
? [fixer.remove(arg1), fixer.removeRange([arg0.range[1], arg1.range[0]])]
123+
: [],
132124
] as const;
133125
}

0 commit comments

Comments
 (0)