Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Eslint combining naming conventions

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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?

>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

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading