diff --git a/lib/rules/jsx-handler-names.js b/lib/rules/jsx-handler-names.js index 42ef5c02c5..29d8666f48 100644 --- a/lib/rules/jsx-handler-names.js +++ b/lib/rules/jsx-handler-names.js @@ -21,26 +21,59 @@ module.exports = { }, schema: [{ - type: 'object', - properties: { - eventHandlerPrefix: { - type: 'string' - }, - eventHandlerPropPrefix: { - type: 'string' + oneOf: [ + { + type: 'object', + properties: { + eventHandlerPrefix: {type: 'string'}, + eventHandlerPropPrefix: {type: 'string'} + }, + additionalProperties: false + }, { + type: 'object', + properties: { + eventHandlerPrefix: {type: 'string'}, + eventHandlerPropPrefix: { + type: 'boolean', + enum: [false] + } + }, + additionalProperties: false + }, { + type: 'object', + properties: { + eventHandlerPrefix: { + type: 'boolean', + enum: [false] + }, + eventHandlerPropPrefix: {type: 'string'} + }, + additionalProperties: false } - }, - additionalProperties: false + ] }] }, create(context) { + function isPrefixDisabled(prefix) { + return prefix === false; + } + const configuration = context.options[0] || {}; - const eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle'; - const eventHandlerPropPrefix = configuration.eventHandlerPropPrefix || 'on'; - const EVENT_HANDLER_REGEX = new RegExp(`^((props\\.${eventHandlerPropPrefix})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`); - const PROP_EVENT_HANDLER_REGEX = new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`); + const eventHandlerPrefix = isPrefixDisabled(configuration.eventHandlerPrefix) ? + null : + configuration.eventHandlerPrefix || 'handle'; + const eventHandlerPropPrefix = isPrefixDisabled(configuration.eventHandlerPropPrefix) ? + null : + configuration.eventHandlerPropPrefix || 'on'; + + const EVENT_HANDLER_REGEX = !eventHandlerPrefix ? + null : + new RegExp(`^((props\\.${eventHandlerPropPrefix || ''})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`); + const PROP_EVENT_HANDLER_REGEX = !eventHandlerPropPrefix ? + null : + new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`); return { JSXAttribute(node) { @@ -55,15 +88,23 @@ module.exports = { return; } - const propIsEventHandler = PROP_EVENT_HANDLER_REGEX.test(propKey); - const propFnIsNamedCorrectly = EVENT_HANDLER_REGEX.test(propValue); + const propIsEventHandler = PROP_EVENT_HANDLER_REGEX && PROP_EVENT_HANDLER_REGEX.test(propKey); + const propFnIsNamedCorrectly = EVENT_HANDLER_REGEX && EVENT_HANDLER_REGEX.test(propValue); - if (propIsEventHandler && !propFnIsNamedCorrectly) { + if ( + propIsEventHandler && + propFnIsNamedCorrectly !== null && + !propFnIsNamedCorrectly + ) { context.report({ node, message: `Handler function for ${propKey} prop key must begin with '${eventHandlerPrefix}'` }); - } else if (propFnIsNamedCorrectly && !propIsEventHandler) { + } else if ( + propFnIsNamedCorrectly && + propIsEventHandler !== null && + !propIsEventHandler + ) { context.report({ node, message: `Prop key for ${propValue} must begin with '${eventHandlerPropPrefix}'` diff --git a/tests/lib/rules/jsx-handler-names.js b/tests/lib/rules/jsx-handler-names.js index 94c88b29aa..c05988d271 100644 --- a/tests/lib/rules/jsx-handler-names.js +++ b/tests/lib/rules/jsx-handler-names.js @@ -63,6 +63,34 @@ ruleTester.run('jsx-handler-names', rule, { parser: parsers.BABEL_ESLINT }, { code: '' + }, { + code: '', + options: [{ + eventHandlerPrefix: false, + eventHandlerPropPrefix: 'on' + }] + }, { + code: '', + options: [{ + eventHandlerPrefix: false, + eventHandlerPropPrefix: 'somePrefix' + }] + }, { + code: '', + options: [{ + eventHandlerPropPrefix: false + }] + }, { + code: '', + options: [{ + eventHandlerPrefix: 'somePrefix', + eventHandlerPropPrefix: false + }] + }, { + code: '', + options: [{ + eventHandlerPropPrefix: false + }] }], invalid: [{