Skip to content

Commit 6aacaa9

Browse files
committed
refactor: refactoring in require-query-options
1 parent 4a56b5f commit 6aacaa9

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/rules/require-query-options.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
1+
import {
2+
AST_NODE_TYPES,
3+
ESLintUtils,
4+
TSESTree,
5+
} from "@typescript-eslint/utils";
26

37
const createRule = ESLintUtils.RuleCreator(
48
(name) =>
59
`https://github.com/danielpza/eslint-plugin-react-query/src/rules/${name}.ts`,
610
);
711

8-
const hooks = [
12+
const useQueryHooks = [
913
// see https://tanstack.com/query/latest/docs/framework/react/reference/useQuery
1014
"useQuery",
1115
"useQueries",
@@ -17,6 +21,21 @@ const hooks = [
1721

1822
const invalidProperties = ["queryKey", "queryFn"];
1923

24+
function isValidQueryNode(queryNode: TSESTree.Node) {
25+
// we only care about object expressions
26+
if (queryNode.type !== AST_NODE_TYPES.ObjectExpression) return true;
27+
28+
// check if any of the properties is queryKey or queryFn
29+
const hasInvalidProperties = queryNode.properties.find(
30+
(property) =>
31+
property.type === AST_NODE_TYPES.Property &&
32+
property.key.type === AST_NODE_TYPES.Identifier &&
33+
invalidProperties.includes(property.key.name),
34+
);
35+
36+
return !hasInvalidProperties;
37+
}
38+
2039
export default createRule({
2140
name: "require-query-options",
2241
defaultOptions: [],
@@ -35,24 +54,16 @@ export default createRule({
3554
create(context) {
3655
return {
3756
CallExpression(node) {
38-
// Check if it's use*Query hook
3957
if (node.callee.type !== AST_NODE_TYPES.Identifier) return;
40-
if (!hooks.includes(node.callee.name)) return;
58+
if (!useQueryHooks.includes(node.callee.name)) return;
4159

60+
// use*Query hook call
4261
if (!node.arguments[0]) return;
4362

4463
// if if caller first argument is an object
45-
if (node.arguments[0].type !== AST_NODE_TYPES.ObjectExpression) return;
46-
47-
// check if any of the properties is queryKey or queryFn
48-
const hasInvalidProperties = node.arguments[0].properties.find(
49-
(property) =>
50-
property.type === AST_NODE_TYPES.Property &&
51-
property.key.type === AST_NODE_TYPES.Identifier &&
52-
invalidProperties.includes(property.key.name),
53-
);
64+
const queryNode = node.arguments[0];
5465

55-
if (hasInvalidProperties)
66+
if (!isValidQueryNode(queryNode))
5667
context.report({
5768
messageId: "require-query-options",
5869
node,

0 commit comments

Comments
 (0)