Skip to content

[generic-type-naming] Support regex literal for config properties that accept regex string #143

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

Closed
sindresorhus opened this issue Jan 26, 2019 · 2 comments
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@sindresorhus
Copy link

sindresorhus commented Jan 26, 2019

Would be nice to be able to use regex literals to not have to double-escape certain characters when using JS-based ESLint config.

Before

'@typescript-eslint/generic-type-naming': [
	'error',
	'^[A-Z][a-zA-Z\\d]+$'
],

After

'@typescript-eslint/generic-type-naming': [
	'error',
	/^[A-Z][a-zA-Z\d]+$/
],

It also means it becomes syntax highlighted and ESLint (through some built-in rules) can prevent some common mistakes when you lint the config itself.

This would require changes to the following rules:

  • @typescript-eslint/generic-type-naming
  • @typescript-eslint/member-naming
  • @typescript-eslint/no-unused-vars

Related ESLint issue: eslint/eslint#11318

@sindresorhus sindresorhus added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jan 26, 2019
@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for team members to take a look labels Jan 26, 2019
@bradzacher bradzacher changed the title Support regex literal for config properties that accept regex string [generic-type-naming] Support regex literal for config properties that accept regex string Jul 27, 2019
@bradzacher
Copy link
Member

Revisiting this.

It's not something we can handle ourselves.
The short and curlies of it is that eslint uses JSONSchema to define rule options, and JSONSchema does not support JS RegExp out of the box, (because it's a spec for validating JSON, not native JS objects).

Looking into it a bit.
Under the hood, eslint uses the ajv library to handle the JSONSchema validation. This library is pretty stupidly powerful, so they could add a bunch of helpful features if they wanted to stray from the spec, but from our end there's not a lot we could do due to not owning the stack.

If it's important to you, there's always the path of raising an eslint rfc. The eslint maintainers are generally pretty receptive to good ideas 😄


Side note.
While playing, I realised that you can actually use this pretty simple hack to use native RegExp in your JS config file:

'@typescript-eslint/generic-type-naming': [
	'error',
	/^[A-Z][a-zA-Z\d]+$/.toString().slice(1, -1)
],

(note modifiers are not supported).

@sindresorhus
Copy link
Author

While playing, I realised that you can actually use this pretty simple hack to use native RegExp in your JS config file:

This would be better:

/^[A-Z][a-zA-Z\d]+$/.source

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants