Skip to content

Commit

Permalink
[new] jsx-handler-name: allow false to disable `eventHandlerPrefi…
Browse files Browse the repository at this point in the history
…x`/`eventHandlerPropPrefix`

Fixes #2400.
  • Loading branch information
tanmoyopenroot authored and ljharb committed Sep 15, 2019
1 parent 489ced3 commit 5970651
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 17 deletions.
75 changes: 58 additions & 17 deletions lib/rules/jsx-handler-names.js
Expand Up @@ -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) {
Expand All @@ -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}'`
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/jsx-handler-names.js
Expand Up @@ -63,6 +63,34 @@ ruleTester.run('jsx-handler-names', rule, {
parser: parsers.BABEL_ESLINT
}, {
code: '<TestComponent only={this.only} />'
}, {
code: '<TestComponent onChange={this.someChange} />',
options: [{
eventHandlerPrefix: false,
eventHandlerPropPrefix: 'on'
}]
}, {
code: '<TestComponent somePrefixChange={this.someChange} />',
options: [{
eventHandlerPrefix: false,
eventHandlerPropPrefix: 'somePrefix'
}]
}, {
code: '<TestComponent someProp={this.handleChange} />',
options: [{
eventHandlerPropPrefix: false
}]
}, {
code: '<TestComponent someProp={this.somePrefixChange} />',
options: [{
eventHandlerPrefix: 'somePrefix',
eventHandlerPropPrefix: false
}]
}, {
code: '<TestComponent someProp={props.onChange} />',
options: [{
eventHandlerPropPrefix: false
}]
}],

invalid: [{
Expand Down

0 comments on commit 5970651

Please sign in to comment.