Skip to content

Commit 5970651

Browse files
tanmoyopenrootljharb
authored andcommitted
[new] jsx-handler-name: allow false to disable eventHandlerPrefix/eventHandlerPropPrefix
Fixes #2400.
1 parent 489ced3 commit 5970651

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

lib/rules/jsx-handler-names.js

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,59 @@ module.exports = {
2121
},
2222

2323
schema: [{
24-
type: 'object',
25-
properties: {
26-
eventHandlerPrefix: {
27-
type: 'string'
28-
},
29-
eventHandlerPropPrefix: {
30-
type: 'string'
24+
oneOf: [
25+
{
26+
type: 'object',
27+
properties: {
28+
eventHandlerPrefix: {type: 'string'},
29+
eventHandlerPropPrefix: {type: 'string'}
30+
},
31+
additionalProperties: false
32+
}, {
33+
type: 'object',
34+
properties: {
35+
eventHandlerPrefix: {type: 'string'},
36+
eventHandlerPropPrefix: {
37+
type: 'boolean',
38+
enum: [false]
39+
}
40+
},
41+
additionalProperties: false
42+
}, {
43+
type: 'object',
44+
properties: {
45+
eventHandlerPrefix: {
46+
type: 'boolean',
47+
enum: [false]
48+
},
49+
eventHandlerPropPrefix: {type: 'string'}
50+
},
51+
additionalProperties: false
3152
}
32-
},
33-
additionalProperties: false
53+
]
3454
}]
3555
},
3656

3757
create(context) {
58+
function isPrefixDisabled(prefix) {
59+
return prefix === false;
60+
}
61+
3862
const configuration = context.options[0] || {};
39-
const eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle';
40-
const eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on';
4163

42-
const EVENT_HANDLER_REGEX = new RegExp(`^((props\\.${eventHandlerPropPrefix})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`);
43-
const PROP_EVENT_HANDLER_REGEX = new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`);
64+
const eventHandlerPrefix = isPrefixDisabled(configuration.eventHandlerPrefix) ?
65+
null :
66+
configuration.eventHandlerPrefix || 'handle';
67+
const eventHandlerPropPrefix = isPrefixDisabled(configuration.eventHandlerPropPrefix) ?
68+
null :
69+
configuration.eventHandlerPropPrefix || 'on';
70+
71+
const EVENT_HANDLER_REGEX = !eventHandlerPrefix ?
72+
null :
73+
new RegExp(`^((props\\.${eventHandlerPropPrefix || ''})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`);
74+
const PROP_EVENT_HANDLER_REGEX = !eventHandlerPropPrefix ?
75+
null :
76+
new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`);
4477

4578
return {
4679
JSXAttribute(node) {
@@ -55,15 +88,23 @@ module.exports = {
5588
return;
5689
}
5790

58-
const propIsEventHandler = PROP_EVENT_HANDLER_REGEX.test(propKey);
59-
const propFnIsNamedCorrectly = EVENT_HANDLER_REGEX.test(propValue);
91+
const propIsEventHandler = PROP_EVENT_HANDLER_REGEX && PROP_EVENT_HANDLER_REGEX.test(propKey);
92+
const propFnIsNamedCorrectly = EVENT_HANDLER_REGEX && EVENT_HANDLER_REGEX.test(propValue);
6093

61-
if (propIsEventHandler && !propFnIsNamedCorrectly) {
94+
if (
95+
propIsEventHandler &&
96+
propFnIsNamedCorrectly !== null &&
97+
!propFnIsNamedCorrectly
98+
) {
6299
context.report({
63100
node,
64101
message: `Handler function for ${propKey} prop key must begin with '${eventHandlerPrefix}'`
65102
});
66-
} else if (propFnIsNamedCorrectly && !propIsEventHandler) {
103+
} else if (
104+
propFnIsNamedCorrectly &&
105+
propIsEventHandler !== null &&
106+
!propIsEventHandler
107+
) {
67108
context.report({
68109
node,
69110
message: `Prop key for ${propValue} must begin with '${eventHandlerPropPrefix}'`

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ ruleTester.run('jsx-handler-names', rule, {
6363
parser: parsers.BABEL_ESLINT
6464
}, {
6565
code: '<TestComponent only={this.only} />'
66+
}, {
67+
code: '<TestComponent onChange={this.someChange} />',
68+
options: [{
69+
eventHandlerPrefix: false,
70+
eventHandlerPropPrefix: 'on'
71+
}]
72+
}, {
73+
code: '<TestComponent somePrefixChange={this.someChange} />',
74+
options: [{
75+
eventHandlerPrefix: false,
76+
eventHandlerPropPrefix: 'somePrefix'
77+
}]
78+
}, {
79+
code: '<TestComponent someProp={this.handleChange} />',
80+
options: [{
81+
eventHandlerPropPrefix: false
82+
}]
83+
}, {
84+
code: '<TestComponent someProp={this.somePrefixChange} />',
85+
options: [{
86+
eventHandlerPrefix: 'somePrefix',
87+
eventHandlerPropPrefix: false
88+
}]
89+
}, {
90+
code: '<TestComponent someProp={props.onChange} />',
91+
options: [{
92+
eventHandlerPropPrefix: false
93+
}]
6694
}],
6795

6896
invalid: [{

0 commit comments

Comments
 (0)