简体   繁体   English

解决由于`no-param-ressign`和`prefer-rest-params`导致的ESLint错误

[英]Resolving ESLint error due to `no-param-reassign` and `prefer-rest-params`

I am trying to resolve these two ESLint errors for the bit of code included below but cannot get my head around it. 我正在尝试解决下面包含的部分代码的这两个ESLint错误,但无法解决。

9   error  Assignment to property of function parameter 'opts'  no-param-reassign
10  error  Use the rest parameters instead of 'arguments'       prefer-rest-params

And this is the code snippet: 这是代码片段:

 8  initialize(opts) {
 9    opts.hasStar = true;
10    AbstractResultView.prototype.initialize.apply(this, arguments);
11  }

I have already tried these for the second error but so far no luck: 我已经为第二个错误尝试过这些,但是到目前为止还没有运气:

 8  initialize(...args, opts) {
 9    opts.hasStar = true;
10    AbstractResultView.prototype.initialize.apply(this, args);
11  }

and

 8  initialize(opts, ...args) {
 9    opts.hasStar = true;
10    AbstractResultView.prototype.initialize.apply(this, args);
11  }

As for the first error, I have no idea how to fix it... 至于第一个错误,我不知道如何解决...

Should mention that this is not my code (I am just trying to fix it) so I may be missing something... 应该提到这不是我的代码(我只是尝试对其进行修复),所以我可能会遗漏某些东西...

The first error is here because in your eslint configuration you use the following: 第一个错误在这里,因为在您的eslint配置中使用以下命令:

no-param-reassign: ["error", { "props": true }]

Because of the "props": true , you can't reassign an object property if this object is a parameter of the function. 由于"props": true ,如果此对象是函数的参数,则不能重新分配对象属性。 Your choices here are limited. 您在这里的选择是有限的。 You can remove the line or change the eslint configuration. 您可以删除该行或更改eslint配置。

More informations on this specific rule : https://eslint.org/docs/rules/no-param-reassign 有关此特定规则的更多信息: https : //eslint.org/docs/rules/no-param-reassign

You can have more information on the second error here : https://eslint.org/docs/rules/prefer-rest-params 您可以在此处获得有关第二个错误的更多信息: https : //eslint.org/docs/rules/prefer-rest-params

But according to the documentation, your second code ( initialize(opts, ...args) { ) should fix the issue. 但是根据文档,您的第二个代码( initialize(opts, ...args) { )应该可以解决此问题。

Try this: 尝试这个:

initialize(opts, ...args) {
    AbstractResultView.prototype.initialize.apply(
        this,
        [
            {
                ...opts,
                hasStar: true
            },
            ...args
        ]
    );
}

This will: 这将:
1. Take first arg and change its prop hasStar without modifying source object 1.首先获取arg并更改其prop hasStar 而不修改源对象
2. Pass all args including modified first one to initialize 2.传递包括修改的第一个在内的所有arg进行initialize

In first case, eslint is trying to avoid modifications in your function argument, which is a bad habit called side effect . 在第一种情况下, eslint试图避免在函数参数中进行修改,这是一种称为side effect的不良习惯。

Second case, eslint is trying to avoid you to use arguments once spread operator is up to use. 第二种情况, eslint试图避免您在使用spread operator eslint使用参数。

Wrong: 错误:

initialize(opts) {
  opts.hasStar = true;
  AbstractResultView.prototype.initialize.apply(this, arguments);
}

There are various ways to do it right. 有多种正确的方法。 I' would try make it this way: 我会尝试这样:

initialize(...args) {
  AbstractResultView.prototype.initialize.apply(this, args);
}

initialize(opts); // call method
opts.hasStar = true; // after assign a prop to it

All of this is by eslint point of view. 所有这些都是从eslint角度来看的。 You can supress this warning and continue doing your code if you would like to. 您可以禁止此警告,并根据需要继续执行代码。

Eventually, I found a solution that resolves both issues: 最终,我找到了解决两个问题的解决方案:

 8  initialize(...args) {
 9    // with destructuring:
10    const [opts] = args;  
11    // or without desctructuring:
12    // const opts = args[0];
13    opts.hasStar = true;
14    AbstractResultView.prototype.initialize.apply(this, args);
15  }

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

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