From 3907ceea9d1b43e00d6dd2a1117c133a88277393 Mon Sep 17 00:00:00 2001 From: Rel1cx Date: Wed, 10 Sep 2025 17:03:28 +0800 Subject: [PATCH] fix: fix useEffect setup function check in web-api/no-leaked-event-listener, closes #1228 --- packages/core/src/hook/hook-hierarchy.ts | 2 -- .../rules/no-leaked-event-listener.spec.ts | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/core/src/hook/hook-hierarchy.ts b/packages/core/src/hook/hook-hierarchy.ts index d1786f1d18..3c165fd366 100644 --- a/packages/core/src/hook/hook-hierarchy.ts +++ b/packages/core/src/hook/hook-hierarchy.ts @@ -7,8 +7,6 @@ import { isUseEffectCallLoose } from "./hook-is"; export function isFunctionOfUseEffectSetup(node: TSESTree.Node | unit) { if (node == null) return false; return node.parent?.type === T.CallExpression - && node.parent.callee !== node - && node.parent.callee.type === T.Identifier && node.parent.arguments.at(0) === node && isUseEffectCallLoose(node.parent); } diff --git a/packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.spec.ts b/packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.spec.ts index 61626033fc..050972d7af 100644 --- a/packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.spec.ts +++ b/packages/plugins/eslint-plugin-react-web-api/src/rules/no-leaked-event-listener.spec.ts @@ -644,6 +644,27 @@ ruleTester.run(RULE_NAME, rule, { }, ], }, + { + code: tsx` + import * as React from 'react'; + + function useCustomHook() { + React.useEffect(() => { + const handleClick = () => { + console.log("clicked"); + }; + document.addEventListener("click", handleClick); + // ^^^^^^^^^^^^ + // - No error is reported here + }, []); + } + `, + errors: [ + { + messageId: "expectedRemoveEventListenerInCleanup", + }, + ], + }, ], valid: [ ...allValid,