From 1625b1a69dc9e81b8f11b9030aa552d2d1c95581 Mon Sep 17 00:00:00 2001 From: akulsr0 Date: Fri, 21 Jun 2024 11:50:26 +0530 Subject: [PATCH] [Fix] `jsx-handler-names`: support ignoring component names --- docs/rules/jsx-handler-names.md | 4 ++- lib/rules/jsx-handler-names.js | 35 +++++++++++++++++++++ tests/lib/rules/jsx-handler-names.js | 46 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/rules/jsx-handler-names.md b/docs/rules/jsx-handler-names.md index ced349f374..610fa18677 100644 --- a/docs/rules/jsx-handler-names.md +++ b/docs/rules/jsx-handler-names.md @@ -34,7 +34,8 @@ Examples of **correct** code for this rule: "eventHandlerPrefix": , "eventHandlerPropPrefix": , "checkLocalVariables": , - "checkInlineFunction": + "checkInlineFunction": , + "ignoreComponentNames": Array }] ... ``` @@ -43,6 +44,7 @@ Examples of **correct** code for this rule: - `eventHandlerPropPrefix`: Prefix for props that are used as event handlers. Defaults to `on` - `checkLocalVariables`: Determines whether event handlers stored as local variables are checked. Defaults to `false` - `checkInlineFunction`: Determines whether event handlers set as inline functions are checked. Defaults to `false` +- `ignoreComponentNames`: Array of regex, when matched with component name, ignores the rule on that component. Defaults to `[]` ## When Not To Use It diff --git a/lib/rules/jsx-handler-names.js b/lib/rules/jsx-handler-names.js index d839073a52..2722523d39 100644 --- a/lib/rules/jsx-handler-names.js +++ b/lib/rules/jsx-handler-names.js @@ -39,6 +39,11 @@ module.exports = { eventHandlerPropPrefix: { type: 'string' }, checkLocalVariables: { type: 'boolean' }, checkInlineFunction: { type: 'boolean' }, + ignoreComponentNames: { + type: 'array', + uniqueItems: true, + items: { type: 'string' }, + }, }, additionalProperties: false, }, { @@ -51,6 +56,11 @@ module.exports = { }, checkLocalVariables: { type: 'boolean' }, checkInlineFunction: { type: 'boolean' }, + ignoreComponentNames: { + type: 'array', + uniqueItems: true, + items: { type: 'string' }, + }, }, additionalProperties: false, }, { @@ -63,6 +73,11 @@ module.exports = { eventHandlerPropPrefix: { type: 'string' }, checkLocalVariables: { type: 'boolean' }, checkInlineFunction: { type: 'boolean' }, + ignoreComponentNames: { + type: 'array', + uniqueItems: true, + items: { type: 'string' }, + }, }, additionalProperties: false, }, { @@ -78,6 +93,16 @@ module.exports = { }, additionalProperties: false, }, + { + type: 'object', + properties: { + ignoreComponentNames: { + type: 'array', + uniqueItems: true, + items: { type: 'string' }, + }, + }, + }, ], }], }, @@ -111,8 +136,17 @@ module.exports = { const checkInlineFunction = !!configuration.checkInlineFunction; + const ignoreComponentNames = configuration.ignoreComponentNames || []; + return { JSXAttribute(node) { + const componentName = node.parent.name.name; + + const isComponentNameIgnored = ignoreComponentNames.some((ignoredComponentNameRegex) => { + const isIgnored = new RegExp(ignoredComponentNameRegex).test(componentName); + return isIgnored; + }); + if ( !node.value || !node.value.expression @@ -124,6 +158,7 @@ module.exports = { : !node.value.expression.object ) ) + || isComponentNameIgnored ) { return; } diff --git a/tests/lib/rules/jsx-handler-names.js b/tests/lib/rules/jsx-handler-names.js index 3aba45beba..602d3613f3 100644 --- a/tests/lib/rules/jsx-handler-names.js +++ b/tests/lib/rules/jsx-handler-names.js @@ -181,6 +181,24 @@ ruleTester.run('jsx-handler-names', rule, { code: '', options: [{ eventHandlerPropPrefix: false }], }, + { + code: ';', + options: [{ checkLocalVariables: true, ignoreComponentNames: ['ComponentFromOtherLibraryBar'] }], + }, + { + code: ` + function App() { + return ( + <> + ; + ; + ; + + ) + } + `, + options: [{ checkLocalVariables: true, ignoreComponentNames: ['^MyLib'] }], + }, ]), invalid: parsers.all([ @@ -369,5 +387,33 @@ ruleTester.run('jsx-handler-names', rule, { }, ], }, + { + code: ` + function App() { + return ( + <> + ; + ; + ; + + ) + } + `, + options: [{ checkLocalVariables: true, ignoreComponentNames: ['^MyLibrary'] }], + errors: [ + { + messageId: 'badPropKey', + data: { propValue: 'handleInput', handlerPropPrefix: 'on' }, + }, + { + messageId: 'badPropKey', + data: { propValue: 'handleCheckbox', handlerPropPrefix: 'on' }, + }, + { + messageId: 'badPropKey', + data: { propValue: 'handleButton', handlerPropPrefix: 'on' }, + }, + ], + }, ]), });