Search code examples
eslinttypescript-eslint

Using @typescript-eslint/naming-convention is it possible to select class properties that are not static?


I have the following typescript:

export class ConsentOptionsWidget {
  public static NULL_CATEGORY_PLACEHOLDER = 'Opt in to channel';
  public static EMAIL_CHANNEL = 'Email';
  public header_name!: string;
  public consentStatement!: string;
}

and my eslint rule:

"@typescript-eslint/naming-convention": [
  "error",
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty"
  }
]

I only want the rule to apply to header_name and consentStatement with consentStatement generating an error. But, as is, consentStatement, NULL_CATEGORY_PLACEHOLDER and EMAIL_CHANNEL generate errors.

According to the project maintainers, even though the docs say "The name must match all of the modifiers" modifiers is actually a superset so public matches anything with public regardless of any other modifiers.


Solution

  • You could add another descriptor with no format requirement for public static properties.

    "@typescript-eslint/naming-convention": [
      "error",
      {
        "format": ["snake_case"],
        "modifiers": ["public"],
        "selector": "classProperty"
      },
      {
        "modifiers": ["public", "static"],
        "selector": "classProperty"
      }
    ]
    

    DEMO LINK