Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion lib/rules/jsx-handler-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/jsx-handler-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ ruleTester.run('jsx-handler-names', rule, {
`,
options: [{ checkLocalVariables: true, ignoreComponentNames: ['MyLib*'] }],
},
{
code: '<A.TestComponent customPropNameBar={handleSomething} />',
options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.TestComponent'] }],
},
{
code: `
function App() {
return (
<div>
<A.MyLibInput customPropNameBar={handleSomething} />;
<A.MyLibCheckbox customPropNameBar={handleSomething} />;
<A.MyLibButtom customPropNameBar={handleSomething} />;
</div>
)
}
`,
options: [{ checkLocalVariables: true, ignoreComponentNames: ['A.MyLib*'] }],
},
]),

invalid: parsers.all([
Expand Down Expand Up @@ -415,5 +433,15 @@ ruleTester.run('jsx-handler-names', rule, {
},
],
},
{
code: '<A.TestComponent onChange={onChange} />',
options: [{ checkLocalVariables: true, ignoreComponentNames: ['B.TestComponent', 'TestComponent', 'Test*'] }],
errors: [
{
messageId: 'badHandlerName',
data: { propKey: 'onChange', handlerPrefix: 'handle' },
},
],
},
]),
});
Loading