Skip to content

Commit aac93d0

Browse files
authored
fix: improve hook detection logic and vi.mock utilities (#1184)
1 parent 6a67592 commit aac93d0

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-unnecessary-use-prefix.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4949
"Program:exit"(program) {
5050
const allHooks = ctx.getAllHooks(program);
5151
for (const { id, name, node, hookCalls } of allHooks.values()) {
52-
// Skip well-known hooks
53-
if (WELL_KNOWN_HOOKS.includes(name)) {
52+
if (hookCalls.length > 0) {
5453
continue;
5554
}
5655
// Skip empty functions
5756
if (AST.isEmptyFunction(node)) {
5857
continue;
5958
}
60-
// Skip useful hooks
61-
if (hookCalls.length > 0) {
59+
// Skip well-known hooks
60+
if (WELL_KNOWN_HOOKS.includes(name)) {
6261
continue;
6362
}
6463
// Skip hooks with comments that contain calls to other hooks

packages/utilities/kit/src/ContextDetection.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,31 @@ export function isProcessEnvNodeEnvCompare(
4343
return false;
4444
}
4545

46+
/**
47+
* Checks if the given node is a `vi.mock`.
48+
* @param node The node to check
49+
* @returns `true` if the node is a `vi.mock`, otherwise `false`.
50+
* @internal
51+
*/
52+
export function isViMock(node: TSESTree.Node | null | unit): node is TSESTree.MemberExpression {
53+
return node != null
54+
&& node.type === T.MemberExpression
55+
&& node.object.type === T.Identifier
56+
&& node.object.name === "vi"
57+
&& node.property.type === T.Identifier
58+
&& node.property.name === "mock";
59+
}
60+
4661
/**
4762
* Checks if the given node is a `vi.mock` callback.
4863
* @param node The node to check
4964
* @returns `true` if the node is a `vi.mock` callback, otherwise `false`.
5065
* @internal
5166
*/
52-
export function isViMockCallback(node: TSESTree.Node | null | unit): node is TSESTree.FunctionExpression {
67+
export function isViMockCallback(node: TSESTree.Node | null | unit) {
5368
return node != null
54-
&& node.type === T.FunctionExpression
69+
&& AST.isFunction(node)
5570
&& node.parent.type === T.CallExpression
56-
&& node.parent.callee.type === T.MemberExpression
57-
&& node.parent.callee.object.type === T.Identifier
58-
&& node.parent.callee.object.name === "vi"
59-
&& node.parent.callee.property.type === T.Identifier
60-
&& node.parent.callee.property.name === "mock"
71+
&& isViMock(node.parent.callee)
6172
&& node.parent.arguments[1] === node;
6273
}

0 commit comments

Comments
 (0)