Skip to content

Commit 1625b1a

Browse files
committed
[Fix] jsx-handler-names: support ignoring component names
1 parent b747450 commit 1625b1a

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

docs/rules/jsx-handler-names.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Examples of **correct** code for this rule:
3434
"eventHandlerPrefix": <eventHandlerPrefix>,
3535
"eventHandlerPropPrefix": <eventHandlerPropPrefix>,
3636
"checkLocalVariables": <boolean>,
37-
"checkInlineFunction": <boolean>
37+
"checkInlineFunction": <boolean>,
38+
"ignoreComponentNames": Array<string>
3839
}]
3940
...
4041
```
@@ -43,6 +44,7 @@ Examples of **correct** code for this rule:
4344
- `eventHandlerPropPrefix`: Prefix for props that are used as event handlers. Defaults to `on`
4445
- `checkLocalVariables`: Determines whether event handlers stored as local variables are checked. Defaults to `false`
4546
- `checkInlineFunction`: Determines whether event handlers set as inline functions are checked. Defaults to `false`
47+
- `ignoreComponentNames`: Array of regex, when matched with component name, ignores the rule on that component. Defaults to `[]`
4648

4749
## When Not To Use It
4850

lib/rules/jsx-handler-names.js

+35
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ module.exports = {
3939
eventHandlerPropPrefix: { type: 'string' },
4040
checkLocalVariables: { type: 'boolean' },
4141
checkInlineFunction: { type: 'boolean' },
42+
ignoreComponentNames: {
43+
type: 'array',
44+
uniqueItems: true,
45+
items: { type: 'string' },
46+
},
4247
},
4348
additionalProperties: false,
4449
}, {
@@ -51,6 +56,11 @@ module.exports = {
5156
},
5257
checkLocalVariables: { type: 'boolean' },
5358
checkInlineFunction: { type: 'boolean' },
59+
ignoreComponentNames: {
60+
type: 'array',
61+
uniqueItems: true,
62+
items: { type: 'string' },
63+
},
5464
},
5565
additionalProperties: false,
5666
}, {
@@ -63,6 +73,11 @@ module.exports = {
6373
eventHandlerPropPrefix: { type: 'string' },
6474
checkLocalVariables: { type: 'boolean' },
6575
checkInlineFunction: { type: 'boolean' },
76+
ignoreComponentNames: {
77+
type: 'array',
78+
uniqueItems: true,
79+
items: { type: 'string' },
80+
},
6681
},
6782
additionalProperties: false,
6883
}, {
@@ -78,6 +93,16 @@ module.exports = {
7893
},
7994
additionalProperties: false,
8095
},
96+
{
97+
type: 'object',
98+
properties: {
99+
ignoreComponentNames: {
100+
type: 'array',
101+
uniqueItems: true,
102+
items: { type: 'string' },
103+
},
104+
},
105+
},
81106
],
82107
}],
83108
},
@@ -111,8 +136,17 @@ module.exports = {
111136

112137
const checkInlineFunction = !!configuration.checkInlineFunction;
113138

139+
const ignoreComponentNames = configuration.ignoreComponentNames || [];
140+
114141
return {
115142
JSXAttribute(node) {
143+
const componentName = node.parent.name.name;
144+
145+
const isComponentNameIgnored = ignoreComponentNames.some((ignoredComponentNameRegex) => {
146+
const isIgnored = new RegExp(ignoredComponentNameRegex).test(componentName);
147+
return isIgnored;
148+
});
149+
116150
if (
117151
!node.value
118152
|| !node.value.expression
@@ -124,6 +158,7 @@ module.exports = {
124158
: !node.value.expression.object
125159
)
126160
)
161+
|| isComponentNameIgnored
127162
) {
128163
return;
129164
}

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

+46
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ ruleTester.run('jsx-handler-names', rule, {
181181
code: '<TestComponent someProp={props.onChange} />',
182182
options: [{ eventHandlerPropPrefix: false }],
183183
},
184+
{
185+
code: '<ComponentFromOtherLibraryBar customPropNameBar={handleSomething} />;',
186+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['ComponentFromOtherLibraryBar'] }],
187+
},
188+
{
189+
code: `
190+
function App() {
191+
return (
192+
<>
193+
<MyLibInput customPropNameBar={handleSomething} />;
194+
<MyLibCheckbox customPropNameBar={handleSomething} />;
195+
<MyLibButtom customPropNameBar={handleSomething} />;
196+
</>
197+
)
198+
}
199+
`,
200+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['^MyLib'] }],
201+
},
184202
]),
185203

186204
invalid: parsers.all([
@@ -369,5 +387,33 @@ ruleTester.run('jsx-handler-names', rule, {
369387
},
370388
],
371389
},
390+
{
391+
code: `
392+
function App() {
393+
return (
394+
<>
395+
<MyLibInput customPropNameBar={handleInput} />;
396+
<MyLibCheckbox customPropNameBar={handleCheckbox} />;
397+
<MyLibButtom customPropNameBar={handleButton} />;
398+
</>
399+
)
400+
}
401+
`,
402+
options: [{ checkLocalVariables: true, ignoreComponentNames: ['^MyLibrary'] }],
403+
errors: [
404+
{
405+
messageId: 'badPropKey',
406+
data: { propValue: 'handleInput', handlerPropPrefix: 'on' },
407+
},
408+
{
409+
messageId: 'badPropKey',
410+
data: { propValue: 'handleCheckbox', handlerPropPrefix: 'on' },
411+
},
412+
{
413+
messageId: 'badPropKey',
414+
data: { propValue: 'handleButton', handlerPropPrefix: 'on' },
415+
},
416+
],
417+
},
372418
]),
373419
});

0 commit comments

Comments
 (0)