Skip to content

[naming-convention] Allow "selector" to specify an array of selectors #2266

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
octogonz opened this issue Jul 1, 2020 · 2 comments · Fixed by #2335
Closed

[naming-convention] Allow "selector" to specify an array of selectors #2266

octogonz opened this issue Jul 1, 2020 · 2 comments · Fixed by #2335
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

@octogonz
Copy link
Contributor

octogonz commented Jul 1, 2020

Repro

Suppose I want to define an option block that applies to several selectors, which do not match up with one of the predefined "group selectors". Currently the option block needs to be duplicated, like this:

'@typescript-eslint/naming-convention': [
    'error',
  {
    selector: 'class',
    format: ['PascalCase'],
    leadingUnderscore: 'allow'
  },
  {
    selector: 'typeAlias',
    format: ['PascalCase'],
    leadingUnderscore: 'allow'
  },
  {
    selector: 'enum',
    format: ['PascalCase'],
    leadingUnderscore: 'allow'
  },
  {
    selector: 'typeParameter',
    format: ['PascalCase'],
    leadingUnderscore: 'allow'
  },
  . . .
]

Expected Result

It should be possible to avoid duplication by specifying an array of selectors, like this:

'@typescript-eslint/naming-convention': [
    'error',
   {
    // Group selector for: class, interface, typeAlias, enum, typeParameter
    selectors: ['class', 'typeAlias', 'enum', 'typeParameter'],
    format: ['PascalCase'],
    leadingUnderscore: 'allow'
  },
  . . .
]

Additional Info

This arose in a real world situation with this option block and a number of other cases in that same file.

I solved it by creating a macro expandNamingConventionSelectors() that expands the selectors array into multiple blocks with different selector fields.

This worked, however it is clumsy. Also, one of our repos creates a test snapshot with the expanded eslintrc object, and the macro output is difficult to read after it is expanded.

It would be better if naming-convention had built-in support for array of selectors, without need for a macro to generate the blocks. I can't think of any design problems with allowing that.

Versions

package version
@typescript-eslint/eslint-plugin 3.3.0
@typescript-eslint/parser 3.3.0
TypeScript 3.5.3
ESLint 7.2.0
node 12.17.0
npm 6.14.4
@octogonz octogonz added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 1, 2020
@octogonz
Copy link
Contributor Author

octogonz commented Jul 1, 2020

BTW an explicit array of selectors might also be more readable for people who work on lint rules. An explicit list avoids the need to consult the documentation and remember what each group selector name is referring to. (Why does variableLike include a function? Why does typeLike include an enum but not an enumMember? etc)

@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 Jul 1, 2020
@octogonz
Copy link
Contributor Author

octogonz commented Aug 3, 2020

Awesome, thank you @GavinWu1991 and @bradzacher !

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 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
2 participants