Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ export const rule = createRule({
return undefined
}

function getHookName(node: TSESTree.CallExpression): string | undefined {
if (node.callee.type === AST_NODE_TYPES.Identifier) {
return node.callee.name;
} else if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
// Verify the chain starts with "trpc"
let current: TSESTree.Expression = node.callee;
while (current.type === AST_NODE_TYPES.MemberExpression) {
if (current.object.type === AST_NODE_TYPES.Identifier) {
if (current.object.name !== 'trpc') {
return undefined; // not from trpc, ignore this case
}
break; // found trpc as the root
}
current = current.object;
}
// Extract the hook name from the right-most property
if (node.callee.property.type === AST_NODE_TYPES.Identifier) {
return node.callee.property.name;
}
}
return undefined;
}

function collectVariableNames(
pattern: TSESTree.BindingName,
queryHook: string,
Expand Down Expand Up @@ -90,11 +113,12 @@ export const rule = createRule({
VariableDeclarator(node) {
if (
node.init !== null &&
node.init.type === AST_NODE_TYPES.CallExpression &&
node.init.callee.type === AST_NODE_TYPES.Identifier &&
allHookNames.includes(node.init.callee.name)
node.init.type === AST_NODE_TYPES.CallExpression
) {
collectVariableNames(node.id, node.init.callee.name)
const hookName = getHookName(node.init);
if (hookName && allHookNames.includes(hookName)) {
collectVariableNames(node.id, hookName);
}
}
},
CallExpression: (node) => {
Expand Down