Use different eslint naming rules for class and interface properties

Viewed 3302

I've been adding linting to a project using typescript-eslint and the eslint-plugin and I need different linting rules for the properties of classes and interfaces. Currently I'm using the naming-convention rule.

For class properties, strictCamelCase works great. But for interface properties, I need to allow both strictCamelCase and PascalCase.

I'd like to use strictCamelCase everywhere, but there are places in our code where I need to use DB columns as properties. For example:

class ActivitySearchModel extends common.Model {
    attributes: {
        ResultID: number;
        StartDateTime: moment.Moment;
        EndDateTime: moment.Moment;
        Label: string;
    };
}

This structure is pervasive throughout the codebase and cannot reasonably be changed. Aside from these attributes, everything else is strictCamelCase.

Is there a simple way to enforce different naming conventions for interface properties and class properties? Even better, would there be a way to only allow PascalCase on attributes? Is the only way to get this to work a custom rule?

The relevant parts of the eslintrc as it's currently set up:

"@typescript-eslint/naming-convention": [
        "error",
        {
            "selector": "default",
            "format": ["strictCamelCase"]
        },
        {
            "selector": "memberLike",
            "modifiers": ["private"],
            "format": ["strictCamelCase"],
            "leadingUnderscore": "require"
        },
        {
            "selector": "property",
            "format": ["strictCamelCase", "PascalCase"]
        },
        {
            "selector": "property",
            "modifiers": ["private"],
            "format": ["strictCamelCase", "PascalCase"],
            "leadingUnderscore": "require"
        }
    ],
0 Answers
Related