Skip to content

Commit 7463ee0

Browse files
committed
[Fix] jsx-handler-names: support namespaced component names
1 parent f2869fd commit 7463ee0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/rules/jsx-handler-names.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ function isInlineHandler(node) {
2727
return node.value.expression.type === 'ArrowFunctionExpression';
2828
}
2929

30+
function getComponentName(node) {
31+
if (node.type === 'JSXIdentifier') {
32+
return node.name;
33+
}
34+
if (node.type === 'JSXMemberExpression') {
35+
return `${getComponentName(node.object)}.${node.property.name}`;
36+
}
37+
if (node.type === 'JSXNamespacedName') {
38+
return `${node.namespace.name}:${node.name.name}`;
39+
}
40+
}
41+
3042
/** @type {import('eslint').Rule.RuleModule} */
3143
module.exports = {
3244
meta: {
@@ -141,7 +153,7 @@ module.exports = {
141153

142154
return {
143155
JSXAttribute(node) {
144-
const componentName = node.parent.name.name;
156+
const componentName = getComponentName(node.parent.name);
145157

146158
const isComponentNameIgnored = ignoreComponentNames.some((ignoredComponentNamePattern) => minimatch(
147159
componentName,

tests/lib/rules/jsx-handler-names.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ ruleTester.run('jsx-handler-names', rule, {
199199
`,
200200
options: [{ checkLocalVariables: true, ignoreComponentNames: ['MyLib*'] }],
201201
},
202+
{
203+
code: '<A.TestComponent customPropNameBar={handleSomething} />',
204+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.TestComponent'] }],
205+
},
206+
{
207+
code: `
208+
function App() {
209+
return (
210+
<div>
211+
<A.MyLibInput customPropNameBar={handleSomething} />;
212+
<A.MyLibCheckbox customPropNameBar={handleSomething} />;
213+
<A.MyLibButtom customPropNameBar={handleSomething} />;
214+
</div>
215+
)
216+
}
217+
`,
218+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.MyLib*'] }],
219+
},
202220
]),
203221

204222
invalid: parsers.all([
@@ -415,5 +433,15 @@ ruleTester.run('jsx-handler-names', rule, {
415433
},
416434
],
417435
},
436+
{
437+
code: '<A.TestComponent onChange={onChange} />',
438+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['B.TestComponent', 'TestComponent', 'Test*'] }],
439+
errors: [
440+
{
441+
messageId: 'badHandlerName',
442+
data: { propKey: 'onChange', handlerPrefix: 'handle' },
443+
},
444+
],
445+
},
418446
]),
419447
});

0 commit comments

Comments
 (0)