Skip to content

Commit

Permalink
Prevent jsx-handler-names from incorrectly flagging only
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lencioni committed Jul 30, 2016
1 parent 2f7a462 commit 1a1dcdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-handler-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module.exports = {
var eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on';

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

return {
JSXAttribute: function(node) {
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/jsx-handler-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ ruleTester.run('jsx-handler-names', rule, {
'<TestComponent onChange={props.foo::handleChange} />'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'<TestComponent only={this.only} />'
].join('\n'),
parserOptions: parserOptions
}],

invalid: [{
Expand All @@ -101,6 +106,18 @@ ruleTester.run('jsx-handler-names', rule, {
].join('\n'),
parserOptions: parserOptions,
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
}, {
code: [
'<TestComponent onChange={this.handlerChange} />'
].join('\n'),
parserOptions: parserOptions,
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
}, {
code: [
'<TestComponent only={this.handleChange} />'
].join('\n'),
parserOptions: parserOptions,
errors: [{message: 'Prop key for handleChange must begin with \'on\''}]
}, {
code: [
'<TestComponent handleChange={this.handleChange} />'
Expand Down

0 comments on commit 1a1dcdb

Please sign in to comment.