Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1686 - jsx-handler-names: false positive when handler name begins with number #1689

Merged
merged 1 commit into from Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/rules/jsx-handler-names.js
Expand Up @@ -92,7 +92,7 @@ module.exports = {

const EVENT_HANDLER_REGEX = !eventHandlerPrefix
? null
: new RegExp(`^((props\\.${eventHandlerPropPrefix || ''})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`);
: new RegExp(`^((props\\.${eventHandlerPropPrefix || ''})|((.*\\.)?${eventHandlerPrefix}))[0-9]*[A-Z].*$`);
const PROP_EVENT_HANDLER_REGEX = !eventHandlerPropPrefix
? null
: new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`);
Expand Down Expand Up @@ -138,7 +138,7 @@ module.exports = {
) {
context.report({
node,
message: `Handler function for ${propKey} prop key must begin with '${eventHandlerPrefix}'`
message: `Handler function for ${propKey} prop key must be a camelCase name beginning with '${eventHandlerPrefix}' only`
});
} else if (
propFnIsNamedCorrectly
Expand Down
29 changes: 22 additions & 7 deletions tests/lib/rules/jsx-handler-names.js
Expand Up @@ -30,6 +30,9 @@ const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-handler-names', rule, {
valid: [{
code: '<TestComponent onChange={this.handleChange} />'
}, {
// TODO: make this an invalid test
code: '<TestComponent onChange={this.handle123Change} />'
}, {
code: '<TestComponent onChange={this.props.onChange} />'
},
Expand Down Expand Up @@ -148,19 +151,31 @@ ruleTester.run('jsx-handler-names', rule, {

invalid: [{
code: '<TestComponent onChange={this.doSomethingOnChange} />',
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={this.handlerChange} />',
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={this.handle} />',
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={this.handle2} />',
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={this.handl3Change} />',
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={this.handle4change} />',
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={takeCareOfChange} />',
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}],
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}],
options: [{
checkLocalVariables: true
}]
}, {
code: '<TestComponent onChange={() => this.takeCareOfChange()} />',
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}],
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}],
options: [{
checkInlineFunction: true
}]
Expand Down Expand Up @@ -202,14 +217,14 @@ ruleTester.run('jsx-handler-names', rule, {
}]
}, {
code: '<TestComponent onChange={this.onChange} />',
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={props::onChange} />',
parser: parsers.BABEL_ESLINT,
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}, {
code: '<TestComponent onChange={props.foo::onChange} />',
parser: parsers.BABEL_ESLINT,
errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]
errors: [{message: 'Handler function for onChange prop key must be a camelCase name beginning with \'handle\' only'}]
}]
});