From 7463ee08697fae15bc757c05a20de7896751a85b Mon Sep 17 00:00:00 2001 From: Takuji Shimokawa Date: Thu, 7 Aug 2025 03:36:42 +0900 Subject: [PATCH] [Fix] `jsx-handler-names`: support namespaced component names --- lib/rules/jsx-handler-names.js | 14 +++++++++++++- tests/lib/rules/jsx-handler-names.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/rules/jsx-handler-names.js b/lib/rules/jsx-handler-names.js index f89f24413e..c722381260 100644 --- a/lib/rules/jsx-handler-names.js +++ b/lib/rules/jsx-handler-names.js @@ -27,6 +27,18 @@ function isInlineHandler(node) { return node.value.expression.type === 'ArrowFunctionExpression'; } +function getComponentName(node) { + if (node.type === 'JSXIdentifier') { + return node.name; + } + if (node.type === 'JSXMemberExpression') { + return `${getComponentName(node.object)}.${node.property.name}`; + } + if (node.type === 'JSXNamespacedName') { + return `${node.namespace.name}:${node.name.name}`; + } +} + /** @type {import('eslint').Rule.RuleModule} */ module.exports = { meta: { @@ -141,7 +153,7 @@ module.exports = { return { JSXAttribute(node) { - const componentName = node.parent.name.name; + const componentName = getComponentName(node.parent.name); const isComponentNameIgnored = ignoreComponentNames.some((ignoredComponentNamePattern) => minimatch( componentName, diff --git a/tests/lib/rules/jsx-handler-names.js b/tests/lib/rules/jsx-handler-names.js index 3ad3378abf..1ab0e6d046 100644 --- a/tests/lib/rules/jsx-handler-names.js +++ b/tests/lib/rules/jsx-handler-names.js @@ -199,6 +199,24 @@ ruleTester.run('jsx-handler-names', rule, { `, options: [{ checkLocalVariables: true, ignoreComponentNames: ['MyLib*'] }], }, + { + code: '', + options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.TestComponent'] }], + }, + { + code: ` + function App() { + return ( +
+ ; + ; + ; +
+ ) + } + `, + options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.MyLib*'] }], + }, ]), invalid: parsers.all([ @@ -415,5 +433,15 @@ ruleTester.run('jsx-handler-names', rule, { }, ], }, + { + code: '', + options: [{ checkLocalVariables: true, ignoreComponentNames: ['B.TestComponent', 'TestComponent', 'Test*'] }], + errors: [ + { + messageId: 'badHandlerName', + data: { propKey: 'onChange', handlerPrefix: 'handle' }, + }, + ], + }, ]), });