简体   繁体   English

在减少 function 中添加到累加器时修复 ESLint no-plusplus 和 no-param-reassign linter 错误

[英]Fix ESLint no-plusplus and no-param-reassign linter errors when adding to accumulator in reduce function

Give this code in TypeScript:在 TypeScript 中给出此代码:

const res = arr.reduce((acc, cur) => (cur.id ? ++acc : acc), 0);

How do I stop the linter throwing these two errors?如何阻止 linter 抛出这两个错误?

(parameter) acc: number Unary operator '++' used. (参数)acc:使用数字一元运算符“++”。 eslint no-plusplus eslint no-plusplus

Assignment to function parameter 'acc'.分配给 function 参数“acc”。 eslint no-param-reassign eslint no-param-reassign

A reduce callback accepts an accumulator and the current value as arguments and returns the accumulator for the next iteration, or the accumulator as a result if there is no next iteration. reduce回调接受一个累加器和当前值作为 arguments 并返回下一次迭代的累加器,如果没有下一次迭代,则返回累加器作为结果。 There is no reason to mutate any of the arguments.没有理由改变任何arguments

There are a few arguments in favor of avoiding mutating parameters in general , and that's why ESLint complains about this, with the no-param-reassign rule .一些 arguments 通常支持避免改变参数,这就是 ESLint 抱怨这一点的原因,使用no-param-reassign规则

The no-plusplus rule , additionally, disallows syntax like ++acc .此外, no-plusplus规则不允许像++acc这样的语法。

Since there is no need to mutate acc to begin with, the minimal fix for both errors would be:由于不需要改变acc一开始,这两个错误的最小修复是:

   const res = arr.reduce((acc, cur) => (cur.id ? acc + 1 : acc), 0);
//                                                ~~~~^~~              Replace `++acc` by `acc + 1`

This returns acc incremented by 1 if cur.id is truthy, and the current acc , otherwise, as expected .如果cur.id为真,则返回acc递增1 ,否则返回当前acc如预期的那样 Your version mutates the current acc in addition to returning the incremented acc , but there is no reason to do so.除了返回递增的acc acc但没有理由这样做。


If you're using ESLint, you have to be aware of the rules.如果您使用 ESLint,则必须了解规则。 You can't write code that ESLint complains about and then be confused why ESLint complains about your code.您不能编写 ESLint 抱怨的代码,然后对 ESLint 抱怨您的代码的原因感到困惑。 Every rule has its own article explaining the rationale, listing alternatives, and explaining how the rule can be adjusted or disabled.每条规则都有自己的文章解释基本原理,列出替代方案,并解释如何调整或禁用规则。

For example, you can disable the no-param-reassign rule例如,您可以禁用no-param-reassign规则

  • by including the comment // eslint-disable no-param-reassign at the top of your file,通过在文件顶部包含注释// eslint-disable no-param-reassign
  • or // eslint-disable-next-line no-param-reassign one line above a usage,或者// eslint-disable-next-line no-param-reassign使用上面的一行,
  • or // eslint-disable-line no-param-reassign in the line after a usage,// eslint-disable-line no-param-reassign在使用后的行中,
  • or by disabling it in your .eslintrc file.或通过在您的.eslintrc文件中禁用它。

There's also a rule that would disallow conditional expressions and some variants .还有一条规则不允许条件表达式和一些变体 You can further simplify your function to avoid even that:您可以进一步简化您的 function 以避免:

const res = arr.reduce((acc, cur) => acc + Number(Boolean(cur.id)), 0);

This would have the same semantics, but it's still not quite clear what kind of condition cur.id was supposed to be.这将具有相同的语义,但仍然不太清楚cur.id应该是哪种条件。 It's best practice to make that a little more explicit (especially in TypeScript), eg cur.hasOwnProperty("id") or cur.id !== 0 or cur.id !== "" , depending on what cur.id may be.最好让它更明确一点(尤其是在 TypeScript 中),例如cur.hasOwnProperty("id")cur.id !== 0cur.id !== "" ,具体取决于cur.id可能是。 Then, the Boolean call is no longer necessary;然后,不再需要Boolean调用; for example, if you want to count how many objects in arr have an id own property, use this instead:例如,如果你想计算arr中有多少对象有一个id自己的属性,请改用这个:

const res = arr.reduce((acc, cur) => acc + Number(cur.hasOwnProperty("id")), 0);

or this:或这个:

const res = arr.filter((cur) => cur.hasOwnProperty("id")).length;

If you still want to use your original falsy / truthy check, the code can be shortened to one of these, using destructuring assignment :如果您仍想使用原始的虚假/真实检查,则可以使用解构赋值将代码缩短为其中之一:

const res = arr.reduce((acc, {id}) => acc + Number(Boolean(id)), 0);
const res = arr.filter(({id}) => id).length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM