Reputation: 145
Is it possible to enable an eslint rule for only a section of code and have it disabled for all other code?
In our case, we have a class:
export class HeaderWidget extends AbstractWidget implements HeaderWidget.State {
// tslint:disable-next-line:variable-name
public readonly widget_type: WidgetType = WidgetType.Header;
public text!: string;
public image_data!: string;
public image_data_alt!: string;
public hide_on_mobile!: boolean;
...
}
We want to make sure that any properties added to this class are snake_case. I found the naming-convention rule that I can enable with:
"@typescript-eslint/naming-convention": [
"error",
{
"format": ["snake_case"],
"selector": "classProperty"
}
]
but that enables the rule for all the code in the project. I just want to enable for properties in that specific class (or even better would be anything that extends AbstractWidget
). I see additional config options like filter
, custom
and suffix
but the docs say they apply to the identifier which leads me to believe they can't be used to pick properties within a certain class.
So I'm looking for a way to configure a rule and have it disabled by default but then enable it for a specific section of code without having to add /* eslint-disable naming-convention */
to every file.
Upvotes: 1
Views: 403
Reputation: 145
I think I found a solution that will work for us. In the eslint.json
overrides
section you can have multiple configs for different files. So adding:
{
"files": ["*.model.ts"],
"rules": {
"@typescript-eslint/naming-convention": [
"error",
{
"format": ["snake_case"],
"selector": "classProperty"
}
]
}
}
will work for us because the classes we want this rule to apply to will always be in *.model.ts
and the only other things in there are namespace
and type
which this rule will not apply to.
Upvotes: 1