最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

As discussed in "@typescript-eslint" remends 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 es 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?

As discussed in "@typescript-eslint" remends 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 es 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?

Share Improve this question asked Jul 28, 2023 at 16:41 M. DesjardinsM. Desjardins 1,0548 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Earlier this month, the ESLint Stylistic project reached version 1.0.0, and is ready for use in production. The stylistic rules that had been deprecated in both eslint and @typescript-eslint have been moved to this project, and both of those projects now remend using ESLint Stylistic if you'd like to use eslint to enforce code style rules.

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 ment 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论