Home > Software design >  Eslint combining naming conventions
Eslint combining naming conventions

Time:12-29

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:

  1. After _ there must be at least one capital letter always.
  2. If you do not provide _ -- you must name classes according to PascalCase format.

How to get it?

CodePudding user response:

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

Regex demo

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
  • Related