diff --git a/docs/rules/jsx-handler-names.md b/docs/rules/jsx-handler-names.md index b567b414db..42d285ae99 100644 --- a/docs/rules/jsx-handler-names.md +++ b/docs/rules/jsx-handler-names.md @@ -30,13 +30,15 @@ The following patterns are **not** considered warnings: ... "react/jsx-handler-names": [, { "eventHandlerPrefix": , - "eventHandlerPropPrefix": + "eventHandlerPropPrefix": , + "checkLocalVariables": }] ... ``` * `eventHandlerPrefix`: Prefix for component methods used as event handlers. Defaults to `handle` * `eventHandlerPropPrefix`: Prefix for props that are used as event handlers. Defaults to `on` +* `checkLocalVariables`: Determines whether event handlers stored as local variables are checked. Defaults to `false` ## When Not To Use It diff --git a/lib/rules/jsx-handler-names.js b/lib/rules/jsx-handler-names.js index 29d8666f48..7c2b34a4c1 100644 --- a/lib/rules/jsx-handler-names.js +++ b/lib/rules/jsx-handler-names.js @@ -21,12 +21,13 @@ module.exports = { }, schema: [{ - oneOf: [ + anyOf: [ { type: 'object', properties: { eventHandlerPrefix: {type: 'string'}, - eventHandlerPropPrefix: {type: 'string'} + eventHandlerPropPrefix: {type: 'string'}, + checkLocalVariables: {type: 'boolean'} }, additionalProperties: false }, { @@ -36,7 +37,8 @@ module.exports = { eventHandlerPropPrefix: { type: 'boolean', enum: [false] - } + }, + checkLocalVariables: {type: 'boolean'} }, additionalProperties: false }, { @@ -46,7 +48,14 @@ module.exports = { type: 'boolean', enum: [false] }, - eventHandlerPropPrefix: {type: 'string'} + eventHandlerPropPrefix: {type: 'string'}, + checkLocalVariables: {type: 'boolean'} + }, + additionalProperties: false + }, { + type: 'object', + properties: { + checkLocalVariables: {type: 'boolean'} }, additionalProperties: false } @@ -75,9 +84,11 @@ module.exports = { null : new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`); + const checkLocal = !!configuration.checkLocalVariables; + return { JSXAttribute(node) { - if (!node.value || !node.value.expression || !node.value.expression.object) { + if (!node.value || !node.value.expression || (!checkLocal && !node.value.expression.object)) { return; } diff --git a/tests/lib/rules/jsx-handler-names.js b/tests/lib/rules/jsx-handler-names.js index c05988d271..d3f54ac704 100644 --- a/tests/lib/rules/jsx-handler-names.js +++ b/tests/lib/rules/jsx-handler-names.js @@ -32,6 +32,16 @@ ruleTester.run('jsx-handler-names', rule, { code: '' }, { code: '' + }, { + code: '', + options: [{ + checkLocalVariables: true + }] + }, { + code: '', + options: [{ + checkLocalVariables: false + }] }, { code: '' }, { @@ -99,12 +109,32 @@ ruleTester.run('jsx-handler-names', rule, { }, { code: '', errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}] + }, { + code: '', + errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}], + options: [{ + checkLocalVariables: true + }] }, { code: '', errors: [{message: 'Prop key for handleChange must begin with \'on\''}] }, { code: '', errors: [{message: 'Prop key for handleChange must begin with \'on\''}] + }, { + code: '', + errors: [{message: 'Prop key for handleChange must begin with \'on\''}], + options: [{ + checkLocalVariables: true + }] + }, { + code: '', + errors: [{message: 'Prop key for handleChange must begin with \'when\''}], + options: [{ + checkLocalVariables: true, + eventHandlerPrefix: 'handle', + eventHandlerPropPrefix: 'when' + }] }, { code: '', errors: [{message: 'Handler function for onChange prop key must begin with \'handle\''}]