eslint-plugin-react-hooks-extra 2.8.1 → 2.8.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +15 -14
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { WEBSITE_URL, getConfigAdapters, getSettingsFromContext } from "@eslint-react/shared";
|
|
2
|
-
import * as
|
|
3
|
-
import
|
|
2
|
+
import * as ast from "@eslint-react/ast";
|
|
3
|
+
import * as core from "@eslint-react/core";
|
|
4
|
+
import { isUseCallbackCall, isUseEffectLikeCall, isUseMemoCall, isUseStateLikeCall } from "@eslint-react/core";
|
|
4
5
|
import { constVoid, getOrElseUpdate, not } from "@eslint-react/eff";
|
|
5
6
|
import { findVariable, getVariableDefinitionNode } from "@eslint-react/var";
|
|
6
7
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
@@ -36,7 +37,7 @@ const rules = { "react-hooks-extra/no-direct-set-state-in-use-effect": "warn" };
|
|
|
36
37
|
//#endregion
|
|
37
38
|
//#region package.json
|
|
38
39
|
var name = "eslint-plugin-react-hooks-extra";
|
|
39
|
-
var version = "2.8.
|
|
40
|
+
var version = "2.8.2-beta.0";
|
|
40
41
|
|
|
41
42
|
//#endregion
|
|
42
43
|
//#region src/utils/create-rule.ts
|
|
@@ -50,8 +51,8 @@ const createRule = ESLintUtils.RuleCreator(getDocsUrl);
|
|
|
50
51
|
function isInitFromHookCall(init) {
|
|
51
52
|
if (init?.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
52
53
|
switch (init.callee.type) {
|
|
53
|
-
case AST_NODE_TYPES.Identifier: return isHookName(init.callee.name);
|
|
54
|
-
case AST_NODE_TYPES.MemberExpression: return init.callee.property.type === AST_NODE_TYPES.Identifier && isHookName(init.callee.property.name);
|
|
54
|
+
case AST_NODE_TYPES.Identifier: return core.isHookName(init.callee.name);
|
|
55
|
+
case AST_NODE_TYPES.MemberExpression: return init.callee.property.type === AST_NODE_TYPES.Identifier && core.isHookName(init.callee.property.name);
|
|
55
56
|
default: return false;
|
|
56
57
|
}
|
|
57
58
|
}
|
|
@@ -103,14 +104,14 @@ function create(context) {
|
|
|
103
104
|
return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseEffectLikeCall(node.parent);
|
|
104
105
|
}
|
|
105
106
|
function getCallName(node) {
|
|
106
|
-
if (node.type === AST_NODE_TYPES.CallExpression) return
|
|
107
|
-
return
|
|
107
|
+
if (node.type === AST_NODE_TYPES.CallExpression) return ast.toStringFormat(node.callee, getText);
|
|
108
|
+
return ast.toStringFormat(node, getText);
|
|
108
109
|
}
|
|
109
110
|
function getCallKind(node) {
|
|
110
111
|
return match(node).when(isUseStateCall, () => "useState").when(isUseEffectLikeCall, () => "useEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
|
|
111
112
|
}
|
|
112
113
|
function getFunctionKind(node) {
|
|
113
|
-
const parent =
|
|
114
|
+
const parent = ast.findParentNode(node, not(ast.isTypeExpression)) ?? node.parent;
|
|
114
115
|
switch (true) {
|
|
115
116
|
case node.async:
|
|
116
117
|
case parent.type === AST_NODE_TYPES.CallExpression && isThenCall(parent): return "deferred";
|
|
@@ -171,7 +172,7 @@ function create(context) {
|
|
|
171
172
|
case pEntry.kind === "deferred":
|
|
172
173
|
case pEntry.node.async: break;
|
|
173
174
|
case pEntry.node === setupFunction:
|
|
174
|
-
case pEntry.kind === "immediate" &&
|
|
175
|
+
case pEntry.kind === "immediate" && ast.findParentNode(pEntry.node, ast.isFunction) === setupFunction:
|
|
175
176
|
context.report({
|
|
176
177
|
messageId: "noDirectSetStateInUseEffect",
|
|
177
178
|
node,
|
|
@@ -179,14 +180,14 @@ function create(context) {
|
|
|
179
180
|
});
|
|
180
181
|
return;
|
|
181
182
|
default: {
|
|
182
|
-
const init =
|
|
183
|
+
const init = ast.findParentNode(node, isVariableDeclaratorFromHookCall)?.init;
|
|
183
184
|
if (init == null) getOrElseUpdate(setStateCallsByFn, pEntry.node, () => []).push(node);
|
|
184
185
|
else getOrElseUpdate(setStateInHookCallbacks, init, () => []).push(node);
|
|
185
186
|
}
|
|
186
187
|
}
|
|
187
188
|
}).with("useEffect", () => {
|
|
188
|
-
if (
|
|
189
|
-
setupFnIds.push(...
|
|
189
|
+
if (ast.isFunction(node.arguments.at(0))) return;
|
|
190
|
+
setupFnIds.push(...ast.getNestedIdentifiers(node));
|
|
190
191
|
}).with("other", () => {
|
|
191
192
|
if (pEntry.node !== setupFunction) return;
|
|
192
193
|
trackedFnCalls.push(node);
|
|
@@ -200,14 +201,14 @@ function create(context) {
|
|
|
200
201
|
const parent = node.parent.parent;
|
|
201
202
|
if (parent.type !== AST_NODE_TYPES.CallExpression) break;
|
|
202
203
|
if (!isUseMemoCall(parent)) break;
|
|
203
|
-
const init =
|
|
204
|
+
const init = ast.findParentNode(parent, isVariableDeclaratorFromHookCall)?.init;
|
|
204
205
|
if (init != null) getOrElseUpdate(setStateInEffectArg, init, () => []).push(node);
|
|
205
206
|
break;
|
|
206
207
|
}
|
|
207
208
|
case AST_NODE_TYPES.CallExpression:
|
|
208
209
|
if (node !== node.parent.arguments.at(0)) break;
|
|
209
210
|
if (isUseCallbackCall(node.parent)) {
|
|
210
|
-
const init =
|
|
211
|
+
const init = ast.findParentNode(node.parent, isVariableDeclaratorFromHookCall)?.init;
|
|
211
212
|
if (init != null) getOrElseUpdate(setStateInEffectArg, init, () => []).push(node);
|
|
212
213
|
break;
|
|
213
214
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-hooks-extra",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.2-beta.0",
|
|
4
4
|
"description": "ESLint React's ESLint plugin for React Hooks related rules.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
"@typescript-eslint/utils": "^8.54.0",
|
|
46
46
|
"string-ts": "^2.3.1",
|
|
47
47
|
"ts-pattern": "^5.9.0",
|
|
48
|
-
"@eslint-react/
|
|
49
|
-
"@eslint-react/
|
|
50
|
-
"@eslint-react/
|
|
51
|
-
"@eslint-react/var": "2.8.
|
|
52
|
-
"@eslint-react/
|
|
48
|
+
"@eslint-react/ast": "2.8.2-beta.0",
|
|
49
|
+
"@eslint-react/core": "2.8.2-beta.0",
|
|
50
|
+
"@eslint-react/eff": "2.8.2-beta.0",
|
|
51
|
+
"@eslint-react/var": "2.8.2-beta.0",
|
|
52
|
+
"@eslint-react/shared": "2.8.2-beta.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/react": "^19.2.10",
|