Search code examples
javascripttypescripteslinttypescript-eslint

ESLint and @typescript-eslint recommend against using stylistic rules. How to enforce code style for things like func-style


As discussed in "@typescript-eslint" recommends the Prettier for the stylistic issues, but has the Prettier same rules and options as ESLint? and mentioned in the documentation for both ESLint and @typescript-eslint, stylistic rules are discouraged from use.

Regarding formatting rules, that seems perfectly reasonable to me. However, this policy also impacts stylistic rules, which ESLint defines as:

Stylistic rules are those related to spacing, conventions, and generally anything that does not highlight an error or a better way to do something.

And @typescript-eslint similarly:

These are mostly around naming or which roughly-equivalent syntax constructs to use (such as function declarations vs. arrow functions).

However, formatters will not touch those because that would change the effect of the code.

I understand that those rules are not going away any time soon, so they can still be used, but I am curious to know how maintain a consistent style in a project (function declarations vs. expressions is the first thing that comes to mind, but also things like @typescript-eslint/array-type) without using these rules. Or do we have to accept that we should not control these things and allow any style?


Solution

  • There's two sets of rules to consider as stylistic - rules that enforce some form of formatting (presence of whitespace, position of curly braces, etc), and rules that impose some form of style (variable names, function decls vs expressions, etc).

    To clarify the stance on stylistic rules - you should avoid the "formatting" class of rules entirely. Essentially anything disabled in eslint-config-prettier is a good example of a rule you should avoid.

    Outside of that it's up to you how much you actually want to enforce! It's a delicate balance to get right with consistency and enforcement - make sure you don't get too deeply into the weeds with enforcing every single thing is consistent.

    A good rule of thumb to work from are questions like "would someone change this thing when touching a file?" or "would someone comment on a PR asking for this to be changed?" If the answer is yes - then you should probably agree upon a standard and lint for it to help focus reviews and reduce PR churn. Otherwise - it's probably of dubious value to lint and enforce.