I want to combine snake_case and PascalCase naming conventions. So, declaring a class its name must match the following format strictly: Pascal_Snake, Pascal_Snake_Pascal_Etc, OnlyPascal, Pascal.
That’s what I’m trying to do in my esling.config:
'@typescript-eslint/naming-convention': [
{
selector: 'class',
format: null,
custom: {
"regex": "([A-Z]\\w*_?[A-Z]\\w+)|([A-Z]\\w*[A-Z]?\\w+)",
"match": true,
},
},
]
Unfortunately, this regex is not strict. What I want is:
- After
_there must be at least one capital letter always. - If you do not provide
_— you must name classes according toPascalCaseformat.
How to get it?
>Solution :
Using \w can match upper/lower case chars and _
You could match an optional underscore before matching an uppercase char A-Z in an optionally repeated group.
(Note to double escape the backslashes if that is required.)
\b[A-Z][a-z]*(?:_?[A-Z][a-z]*)*\b
If you don’t want to match a single A or AA, you can repeat the lowercase chars 1+ times instead.
\b[A-Z][a-z]+(?:_?[A-Z][a-z]+)*\b