Skip to content

Commit 8b2375d

Browse files
committed
refactor: minor improvements
1 parent 2da365e commit 8b2375d

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@
5858
}
5959
],
6060
"mdx.server.enable": true,
61-
"search.exclude": {
62-
"pnpm-lock.yaml": true
63-
},
6461
"files.exclude": {
6562
"**/.turbo": true,
6663
"*.bundled_*.mjs": true

packages/plugins/eslint-plugin-react-x/src/rules/no-set-state-in-component-did-mount.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ export default createRule<[], MessageID>({
4545
O.Do,
4646
O.bind("clazz", () => AST.traverseUpGuard(node, isClassComponent)),
4747
O.bind("method", ({ clazz }) => AST.traverseUpStop(node, clazz, isComponentDidMount)),
48+
O.bind("methodScope", ({ method }) => O.some(context.sourceCode.getScope(method))),
4849
O.bind("upperScope", () => O.fromNullable(context.sourceCode.getScope(node).upper)),
49-
O.filter(({ clazz, method, upperScope }) =>
50-
method.parent === clazz.body && upperScope === context.sourceCode.getScope(method)
50+
O.filter(({
51+
clazz,
52+
method,
53+
methodScope,
54+
upperScope,
55+
}) =>
56+
method.parent === clazz.body
57+
&& upperScope === methodScope
5158
),
5259
O.map(() => ({
5360
messageId: "noSetStateInComponentDidMount",

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-context-value.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as AST from "@eslint-react/ast";
22
import { useComponentCollector } from "@eslint-react/core";
3-
import { O } from "@eslint-react/eff";
3+
import { F, O } from "@eslint-react/eff";
44
import type { RuleFeature } from "@eslint-react/types";
55
import * as VAR from "@eslint-react/var";
66
import { AST_NODE_TYPES } from "@typescript-eslint/types";
@@ -38,42 +38,46 @@ export default createRule<[], MessageID>({
3838
name: RULE_NAME,
3939
create(context) {
4040
const { ctx, listeners } = useComponentCollector(context);
41-
const possibleValueConstructions = new Map<AST.TSESTreeFunction, VAR.Construction[]>();
41+
const constructions = new Map<AST.TSESTreeFunction, VAR.Construction[]>();
4242

4343
return {
4444
...listeners,
4545
JSXOpeningElement(node) {
4646
const openingElementName = node.name;
4747
if (openingElementName.type !== AST_NODE_TYPES.JSXMemberExpression) return;
4848
if (openingElementName.property.name !== "Provider") return;
49-
const maybeJSXValueAttribute = O.fromNullable(
50-
node.attributes.find((attribute) => {
51-
return attribute.type === AST_NODE_TYPES.JSXAttribute
52-
&& attribute.name.name === "value";
49+
const constructionEntry = F.pipe(
50+
O.Do,
51+
O.bind("function", ctx.getCurrentFunction),
52+
O.bind("attribute", () =>
53+
O.fromNullable(
54+
node.attributes.find((attribute) => {
55+
return attribute.type === AST_NODE_TYPES.JSXAttribute
56+
&& attribute.name.name === "value";
57+
}),
58+
)),
59+
O.bind("value", ({ attribute }) => "value" in attribute ? O.some(attribute.value) : O.none()),
60+
O.bind("valueExpression", ({ value }) =>
61+
value?.type === AST_NODE_TYPES.JSXExpressionContainer
62+
? O.some(value.expression)
63+
: O.none()),
64+
O.bind("construction", ({ valueExpression }) => {
65+
const initialScope = context.sourceCode.getScope(valueExpression);
66+
return O.some(VAR.inspectConstruction(valueExpression, initialScope));
5367
}),
68+
O.filter(({ construction }) => construction._tag !== "None"),
5469
);
55-
if (O.isNone(maybeJSXValueAttribute) || !("value" in maybeJSXValueAttribute.value)) return;
56-
const valueNode = maybeJSXValueAttribute.value.value;
57-
if (valueNode?.type !== AST_NODE_TYPES.JSXExpressionContainer) return;
58-
const valueExpression = valueNode.expression;
59-
const initialScope = context.sourceCode.getScope(valueExpression);
60-
const construction = VAR.inspectConstruction(valueExpression, initialScope);
61-
if (construction._tag === "None") return;
62-
O.map(
63-
ctx.getCurrentFunction(),
64-
([_, currentFn]) =>
65-
possibleValueConstructions.set(currentFn, [
66-
...possibleValueConstructions.get(currentFn) ?? [],
67-
construction,
68-
]),
69-
);
70+
for (const { construction, function: [_, fNode] } of O.toArray(constructionEntry)) {
71+
constructions.set(fNode, [
72+
...constructions.get(fNode) ?? [],
73+
construction,
74+
]);
75+
}
7076
},
7177
"Program:exit"(node) {
7278
const components = ctx.getAllComponents(node).values();
7379
for (const { node: component } of components) {
74-
const constructions = possibleValueConstructions.get(component);
75-
if (!constructions) continue;
76-
for (const construction of constructions) {
80+
for (const construction of constructions.get(component) ?? []) {
7781
if (construction._tag === "None") continue;
7882
const { node: constructionNode, _tag } = construction;
7983
const messageId = _tag.startsWith("Function")

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-default-props.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default createRule<[], MessageID>({
3939
name: RULE_NAME,
4040
create(context) {
4141
const { ctx, listeners } = useComponentCollector(context);
42-
const possibleDestructuringDeclarators = new WeakMap<
42+
const declarators = new WeakMap<
4343
AST.TSESTreeFunction,
4444
ObjectDestructuringDeclarator[]
4545
>();
@@ -55,11 +55,9 @@ export default createRule<[], MessageID>({
5555
const properties = match(props)
5656
.with({ type: AST_NODE_TYPES.ObjectPattern }, ({ properties }) => properties)
5757
.with({ type: AST_NODE_TYPES.Identifier }, ({ name }) => {
58-
const variableDeclarators = possibleDestructuringDeclarators.get(component);
59-
if (!variableDeclarators) return [];
60-
const declarators = variableDeclarators.filter(d => d.init.name === name);
61-
62-
return declarators.flatMap(d => d.id.properties);
58+
return declarators.get(component)
59+
?.filter(d => d.init.name === name)
60+
.flatMap(d => d.id.properties) ?? [];
6361
})
6462
.otherwise(() => []);
6563
for (const prop of properties) {
@@ -85,14 +83,10 @@ export default createRule<[], MessageID>({
8583
}
8684
},
8785
"VariableDeclarator[id.type='ObjectPattern'][init.type='Identifier']"(node: ObjectDestructuringDeclarator) {
88-
O.map(
89-
ctx.getCurrentFunction(),
90-
([_, currentFn]) =>
91-
possibleDestructuringDeclarators.set(currentFn, [
92-
...possibleDestructuringDeclarators.get(currentFn) ?? [],
93-
node,
94-
]),
95-
);
86+
for (const [_, currentFn] of O.toArray(ctx.getCurrentFunction())) {
87+
const prevDeclarators = declarators.get(currentFn) ?? [];
88+
declarators.set(currentFn, [...prevDeclarators, node]);
89+
}
9690
},
9791
};
9892
},

packages/plugins/eslint-plugin-react-x/src/rules/no-unused-class-component-members.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ export default createRule<[], MessageID>({
148148
if (node.init && AST.isThisExpression(node.init) && node.id.type === AST_NODE_TYPES.ObjectPattern) {
149149
for (const prop of node.id.properties) {
150150
if (prop.type === AST_NODE_TYPES.Property && AST.isKeyLiteralLike(prop, prop.key)) {
151-
O.map(getName(prop.key), name => propertyUsages.get(currentClass)?.add(name));
151+
for (const name of O.toArray(getName(prop.key))) {
152+
propertyUsages.get(currentClass)?.add(name);
153+
}
152154
}
153155
}
154156
}

packages/plugins/eslint-plugin-react-x/src/rules/use-jsx-vars.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ export default createRule<[], MessageID>({
1616
meta: {
1717
type: "problem",
1818
docs: {
19-
description:
20-
// eslint-disable-next-line eslint-plugin/require-meta-docs-description
21-
"helpes `eslint/no-unused-vars` to correctly mark JSX variables as used.",
19+
// eslint-disable-next-line eslint-plugin/require-meta-docs-description
20+
description: "helpes `eslint/no-unused-vars` to correctly mark JSX variables as used.",
2221
[Symbol.for("rule_features")]: RULE_FEATURES,
2322
},
2423
messages: {
@@ -41,9 +40,9 @@ export default createRule<[], MessageID>({
4140
return {
4241
JSXOpeningElement(node) {
4342
if (node.name.type === AST_NODE_TYPES.JSXIdentifier && /^[a-z]/u.test(node.name.name)) return;
44-
O.map(getName(node.name), (name) => {
43+
for (const name of O.toArray(getName(node.name))) {
4544
context.sourceCode.markVariableAsUsed(name, node);
46-
});
45+
}
4746
},
4847
};
4948
},

0 commit comments

Comments
 (0)