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

react/jsx-handler-names: cannot opt-out of handler prefix independent of prop prefix #2400

Closed
drewlustro opened this issue Sep 5, 2019 · 7 comments · Fixed by #2410
Closed

Comments

@drewlustro
Copy link

Issue

I only want to enforce eventHandlerPropPrefix, but I cannot selectively opt out of eventHandlerPrefix.


1. Rule always plugs in a default value 'handle' into eventHandlerPrefix when supplied value is an empty string '' (falsy)

Problematic code:

// will always be set to 'handle' when falsy
const eventHandlerPrefix = configuration.eventHandlerPrefix || 'handle';

source


2. Rule prevents "whitelist" JS regex workarounds noted in #1687 because a capital letter must succeed the allowed prefix.

Example Config: I want to whitelist submit select verbatim and handle* prefix:

"react/jsx-handler-names": ['error', {
  "eventHandlerPrefix": '(handle|submit|select)',
  "eventHandlerPropPrefix": 'on',
}],

Problematic code:

// [A-Z] succeeding eventHandlerPrefix prevents handler whitelist
const EVENT_HANDLER_REGEX = new RegExp(`^((props\\.${eventHandlerPropPrefix})|((.*\\.)?${eventHandlerPrefix}))[A-Z].*$`);

source

@ljharb
Copy link
Member

ljharb commented Sep 6, 2019

I'm not very clear on what actual code you want to be allowed, and what disallowed?

@drewlustro
Copy link
Author

I want to be able to set eventHandlerPrefix: false | null in order to turn off the prefix requirement and only enforce the prefix requirement for eventHandlerPropPrefix.

@ljharb
Copy link
Member

ljharb commented Sep 6, 2019

Can you provide raw source code that you want to allow or disallow, without speaking in any way about linter rules or configuration options?

@drewlustro
Copy link
Author

Assuming hypothetical config:

"react/jsx-handler-names": ['error', {
  "eventHandlerPrefix": false,
  "eventHandlerPropPrefix": 'on',
}],

Allow:

<Foo
  onSubmit={this.submit}
/>

<Foo
  onSubmit={this.handleSubmit}
/>

<Foo
  onSubmit={this.whateverAnythingIsOkay}
/>

Disallow:

<Foo
  submit={this.submit} // error, prop handler must be prefixed with 'on'
/>

@ljharb
Copy link
Member

ljharb commented Sep 6, 2019

Thanks. Adding support for false - in either config option, but not both - seems like a great PR.

@drewlustro
Copy link
Author

Awesome; will see if I can give it a shot in the coming week.

@drewlustro
Copy link
Author

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants