eslint-plugin-react-hooks-extra
Advanced tools
Comparing version
import * as AST from '@eslint-react/ast'; | ||
import * as ER from '@eslint-react/core'; | ||
import { getOrElseUpdate, constVoid } from '@eslint-react/eff'; | ||
import { getOrElseUpdate, constVoid, not } from '@eslint-react/eff'; | ||
import { getDocsUrl, getSettingsFromContext } from '@eslint-react/shared'; | ||
@@ -30,35 +30,3 @@ import * as VAR from '@eslint-react/var'; | ||
var name2 = "eslint-plugin-react-hooks-extra"; | ||
var version = "2.0.0-next.34"; | ||
var createRule = ESLintUtils.RuleCreator(getDocsUrl("hooks-extra")); | ||
// src/rules/no-direct-set-state-in-use-effect.ts | ||
var RULE_NAME = "no-direct-set-state-in-use-effect"; | ||
var RULE_FEATURES = [ | ||
"EXP" | ||
]; | ||
var no_direct_set_state_in_use_effect_default = createRule({ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Disallow direct calls to the `set` function of `useState` in `useEffect`.", | ||
[Symbol.for("rule_features")]: RULE_FEATURES | ||
}, | ||
messages: { | ||
noDirectSetStateInUseEffect: "Do not call the 'set' function '{{name}}' of 'useState' directly in 'useEffect'." | ||
}, | ||
schema: [] | ||
}, | ||
name: RULE_NAME, | ||
create, | ||
defaultOptions: [] | ||
}); | ||
function create(context) { | ||
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {}; | ||
return useNoDirectSetStateInUseEffect(context, { | ||
onViolation(ctx, node, data) { | ||
ctx.report({ messageId: "noDirectSetStateInUseEffect", node, data }); | ||
}, | ||
useEffectKind: "useEffect" | ||
}); | ||
} | ||
var version = "2.0.0-next.35"; | ||
function useNoDirectSetStateInUseEffect(context, options) { | ||
@@ -102,3 +70,14 @@ const { onViolation, useEffectKind } = options; | ||
function getFunctionKind(node) { | ||
return match(node).when(isFunctionOfUseEffectSetup, () => "setup").when(AST.isImmediatelyInvokedFunction, () => "immediate").otherwise(() => "other"); | ||
const parent = AST.findParentNode(node, not(AST.isTypeExpression)) ?? node.parent; | ||
switch (true) { | ||
case node.async: | ||
case (parent.type === AST_NODE_TYPES.CallExpression && AST.isThenCall(parent)): | ||
return "deferred"; | ||
case (node.type !== AST_NODE_TYPES.FunctionDeclaration && parent.type === AST_NODE_TYPES.CallExpression && parent.callee === node): | ||
return "immediate"; | ||
case isFunctionOfUseEffectSetup(node): | ||
return "setup"; | ||
default: | ||
return "other"; | ||
} | ||
} | ||
@@ -307,3 +286,35 @@ function isIdFromUseStateCall(topLevelId, at) { | ||
} | ||
var createRule = ESLintUtils.RuleCreator(getDocsUrl("hooks-extra")); | ||
// src/rules/no-direct-set-state-in-use-effect.ts | ||
var RULE_NAME = "no-direct-set-state-in-use-effect"; | ||
var RULE_FEATURES = [ | ||
"EXP" | ||
]; | ||
var no_direct_set_state_in_use_effect_default = createRule({ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Disallow direct calls to the `set` function of `useState` in `useEffect`.", | ||
[Symbol.for("rule_features")]: RULE_FEATURES | ||
}, | ||
messages: { | ||
noDirectSetStateInUseEffect: "Do not call the 'set' function '{{name}}' of 'useState' directly in 'useEffect'." | ||
}, | ||
schema: [] | ||
}, | ||
name: RULE_NAME, | ||
create, | ||
defaultOptions: [] | ||
}); | ||
function create(context) { | ||
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {}; | ||
return useNoDirectSetStateInUseEffect(context, { | ||
onViolation(ctx, node, data) { | ||
ctx.report({ messageId: "noDirectSetStateInUseEffect", node, data }); | ||
}, | ||
useEffectKind: "useEffect" | ||
}); | ||
} | ||
// src/rules/no-direct-set-state-in-use-layout-effect.ts | ||
@@ -310,0 +321,0 @@ var RULE_NAME2 = "no-direct-set-state-in-use-layout-effect"; |
{ | ||
"name": "eslint-plugin-react-hooks-extra", | ||
"version": "2.0.0-next.34", | ||
"version": "2.0.0-next.35", | ||
"description": "ESLint React's ESLint plugin for React Hooks related rules.", | ||
@@ -45,8 +45,8 @@ "keywords": [ | ||
"ts-pattern": "^5.7.1", | ||
"@eslint-react/core": "2.0.0-next.34", | ||
"@eslint-react/ast": "2.0.0-next.34", | ||
"@eslint-react/eff": "2.0.0-next.34", | ||
"@eslint-react/kit": "2.0.0-next.34", | ||
"@eslint-react/shared": "2.0.0-next.34", | ||
"@eslint-react/var": "2.0.0-next.34" | ||
"@eslint-react/ast": "2.0.0-next.35", | ||
"@eslint-react/kit": "2.0.0-next.35", | ||
"@eslint-react/eff": "2.0.0-next.35", | ||
"@eslint-react/shared": "2.0.0-next.35", | ||
"@eslint-react/core": "2.0.0-next.35", | ||
"@eslint-react/var": "2.0.0-next.35" | ||
}, | ||
@@ -53,0 +53,0 @@ "devDependencies": { |
19520
1.95%410
2.76%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed