no-plusplus
禁用一元運算子 ++
和 --
❄️ 已凍結
此規則目前為凍結狀態,且不接受功能請求。
由於一元運算子 ++
和 --
受自動分號插入的影響,空白字元的差異可能會改變原始碼的語意。
let i = 10;
let j = 20;
i ++
j
// i = 11, j = 20
let i = 10;
let j = 20;
i
++
j
// i = 10, j = 21
規則詳情
此規則禁用一元運算子 ++
和 --
。
此規則的錯誤程式碼範例
在遊樂場開啟
/*eslint no-plusplus: "error"*/
let foo = 0;
;
let bar = 42;
;
for (i = 0; i < l; ) {
doSomething(i);
}
此規則的正確程式碼範例
在遊樂場開啟
/*eslint no-plusplus: "error"*/
let foo = 0;
foo += 1;
let bar = 42;
bar -= 1;
for (i = 0; i < l; i += 1) {
doSomething(i);
}
選項
此規則有一個物件選項。
"allowForLoopAfterthoughts": true
允許在for
迴圈的 afterthought (最終表達式) 中使用一元運算子++
和--
。
allowForLoopAfterthoughts
使用 { "allowForLoopAfterthoughts": true }
選項時,此規則的正確程式碼範例
在遊樂場開啟
/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
for (i = 0; i < l; i++) {
doSomething(i);
}
for (i = l; i >= 0; i--) {
doSomething(i);
}
for (i = 0, j = l; i < l; i++, j--) {
doSomething(i, j);
}
使用 { "allowForLoopAfterthoughts": true }
選項時,此規則的錯誤程式碼範例
在遊樂場開啟
/*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/
for (i = 0; i < l; j = ) {
doSomething(i, j);
}
for (i = l; ;) {
doSomething(i);
}
for (i = 0; i < l;) ;
版本
此規則在 ESLint v0.0.9 中引入。