Skip to content

Commit 1a1dcdb

Browse files
committed
Prevent jsx-handler-names from incorrectly flagging only
The rule was triggered for a property `only`. This is because the regex was checking for the prefix (by default "on") followed by any characters. We can fix this by ensuring that the prefix we are checking for is always followed by an uppercase character. It was suggested in the issue that we might want to allow this rule to accept a regex, which would allow for more flexibility in how this rule is configured. I think this might be a nice enhancement, but since this fix was so simple I decided to go with the quick and easy win at this time. Fixes #571
1 parent 2f7a462 commit 1a1dcdb

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/rules/jsx-handler-names.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ module.exports = {
3838
var eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on';
3939

4040
var EVENT_HANDLER_REGEX = new RegExp('^((props\\.' + eventHandlerPropPrefix + ')'
41-
+ '|((.*\\.)?' + eventHandlerPrefix + ')).+$');
42-
var PROP_EVENT_HANDLER_REGEX = new RegExp('^(' + eventHandlerPropPrefix + '.+|ref)$');
41+
+ '|((.*\\.)?' + eventHandlerPrefix + '))[A-Z].*$');
42+
var PROP_EVENT_HANDLER_REGEX = new RegExp('^(' + eventHandlerPropPrefix + '[A-Z].*|ref)$');
4343

4444
return {
4545
JSXAttribute: function(node) {

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

+17
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ ruleTester.run('jsx-handler-names', rule, {
9393
'<TestComponent onChange={props.foo::handleChange} />'
9494
].join('\n'),
9595
parser: 'babel-eslint'
96+
}, {
97+
code: [
98+
'<TestComponent only={this.only} />'
99+
].join('\n'),
100+
parserOptions: parserOptions
96101
}],
97102

98103
invalid: [{
@@ -101,6 +106,18 @@ ruleTester.run('jsx-handler-names', rule, {
101106
].join('\n'),
102107
parserOptions: parserOptions,
103108
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
109+
}, {
110+
code: [
111+
'<TestComponent onChange={this.handlerChange} />'
112+
].join('\n'),
113+
parserOptions: parserOptions,
114+
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
115+
}, {
116+
code: [
117+
'<TestComponent only={this.handleChange} />'
118+
].join('\n'),
119+
parserOptions: parserOptions,
120+
errors: [{message: 'Prop key for handleChange must begin with \'on\''}]
104121
}, {
105122
code: [
106123
'<TestComponent handleChange={this.handleChange} />'

0 commit comments

Comments
 (0)