eslint 错误一元运算符 '++' 使用了 no-plusplus

Meh*_*dar 2 javascript node.js reactjs eslint

如果我i++在 for 中使用,我的 for 循环会出错loop

var foo = 0;
    foo++;
    
    var bar = 42;
    bar--;
    
    for (i = 0; i < 1; i++) {
        return;
    }
Run Code Online (Sandbox Code Playgroud)

adr*_*duc 7

一种选择是替换i++i+=1

您还可以关闭该特定 eslint 规则(针对特定行、文件或全局配置)。请考虑这可能不被推荐,尤其是在文件或行级别。

您要查找的规则名称是no-plusplus

全局禁用它

在你的 eslint 配置文件中添加以下内容:

'no-plusplus': 'off' **OR** 'no-plusplus': 0
Run Code Online (Sandbox Code Playgroud)

还有一个选项可以仅对 for 循环禁用它:

 no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您可以查看eslint no-plusplus 文档

在文件级别禁用它

在文件顶部添加以下内容:

/* eslint-disable no-plusplus */
Run Code Online (Sandbox Code Playgroud)

为给定的行禁用它

在 for 循环之前,添加以下内容:

/* eslint-disable-next-line no-alert */
Run Code Online (Sandbox Code Playgroud)


Meh*_*dar 6

我得到了这个问题的解决方案

如果我们在代码中使用 i++ eslint 会出错。为了避免这种类型的错误,我们必须使用

var foo = 0;
foo += 1;

var bar = 42;
bar -= 1;

for (i = 0; i < l; i += 1) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

谢谢


Cha*_*nka 5

eslint no-plusplus:“错误”

将在以下情况下引发错误

var foo = 0;
foo++;

var bar = 42;
bar--;

for (i = 0; i < l; i++) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

使用如下方法修复 lint 问题,使其遵守规则

var foo = 0;
foo += 1;

var bar = 42;
bar -= 1;

for (i = 0; i < l; i += 1) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

文档


sab*_*lam 2

正如您所看到的,这是一个 linting 错误。要么像这样编写代码,

foo += 1;

i += 1
Run Code Online (Sandbox Code Playgroud)

或者关闭 eslint 规则。(不是一个好主意);