no-mixed-operators
不允許混合的二元運算子
此規則在 ESLint v8.53.0 中已棄用。請使用 @stylistic/eslint-plugin-js
中的對應規則。
將複雜的表達式用括號括起來可以清楚地表達開發人員的意圖,這使得程式碼更具可讀性。當表達式中連續使用不同的運算符而沒有括號時,此規則會發出警告。
var foo = a && b || c || d; /*BAD: Unexpected mix of '&&' and '||'.*/
var foo = (a && b) || c || d; /*GOOD*/
var foo = a && (b || c || d); /*GOOD*/
注意:此規則預期為每對混合運算符發出一個錯誤。因此,對於每兩個連續使用的混合運算符,將會顯示一個不同的錯誤,指向使用特定違反規則的運算符的位置
var foo = a && b || c || d;
將會產生
1:13 Unexpected mix of '&&' and '||'. (no-mixed-operators)
1:18 Unexpected mix of '&&' and '||'. (no-mixed-operators)
規則詳情
此規則會檢查 BinaryExpression
、LogicalExpression
和 ConditionalExpression
。
此規則可能與 no-extra-parens 規則衝突。如果您同時使用此規則和 no-extra-parens 規則,您需要使用 no-extra-parens 規則的 nestedBinaryExpressions
選項。
此規則的錯誤程式碼範例
/*eslint no-mixed-operators: "error"*/
var foo = a b < 0 c > 0 || d + 1 === 0;
var foo = a b c;
此規則的正確程式碼範例
/*eslint no-mixed-operators: "error"*/
var foo = a || b || c;
var foo = a && b && c;
var foo = (a && b < 0) || c > 0 || d + 1 === 0;
var foo = a && (b < 0 || c > 0 || d + 1 === 0);
var foo = a + (b * c);
var foo = (a + b) * c;
選項
{
"no-mixed-operators": [
"error",
{
"groups": [
["+", "-", "*", "/", "%", "**"],
["&", "|", "^", "~", "<<", ">>", ">>>"],
["==", "!=", "===", "!==", ">", ">=", "<", "<="],
["&&", "||"],
["in", "instanceof"]
],
"allowSamePrecedence": true
}
]
}
此規則有 2 個選項。
-
groups
(string[][]
) - 指定要檢查的運算符群組。groups
選項是一個群組列表,而一個群組是一個二元運算符列表。預設運算符群組定義為算術、位元、比較、邏輯和關係運算符。注意:三元運算符 (?:) 可以是任何群組的一部分,並且預設允許與其他運算符混合使用。 -
allowSamePrecedence
(boolean
) - 指定是否允許混合具有相同優先順序的運算符。預設值為true
。
groups
以下運算符可用於 groups
選項
- 算術運算符:
"+"
、"-"
、"*"
、"/"
、"%"
、"**"
- 位元運算符:
"&"
、"|"
、"^"
、"~"
、"<<"
、">>"
、">>>"
- 比較運算符:
"=="
、"!="
、"==="
、"!=="
、">"
、">="
、"<"
、"<="
- 邏輯運算符:
"&&"
、"||"
- 合併運算符:
"??"
- 關係運算符:
"in"
、"instanceof"
- 三元運算符:
?:
現在,考慮以下群組設定:{"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
。此設定中指定了 2 個群組:位元運算符和邏輯運算符。此規則僅檢查運算符是否屬於同一個群組。在這種情況下,此規則會檢查是否混合了位元運算符和邏輯運算符,但會忽略所有其他運算符。
此規則的錯誤程式碼範例,選項為 {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
/*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/
var foo = a b < 0 c > 0 || d + 1 === 0;
var foo = a b c;
/*eslint no-mixed-operators: ["error", {"groups": [["&&", "||", "?:"]]}]*/
var foo = a b c : d;
var bar = a b c : d;
var baz = a b : c d;
此規則的正確程式碼範例,選項為 {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}
/*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/
var foo = a || b > 0 || c + 1 === 0;
var foo = a && b > 0 && c + 1 === 0;
var foo = (a && b < 0) || c > 0 || d + 1 === 0;
var foo = a && (b < 0 || c > 0 || d + 1 === 0);
var foo = (a & b) | c;
var foo = a & (b | c);
var foo = a + b * c;
var foo = a + (b * c);
var foo = (a + b) * c;
/*eslint no-mixed-operators: ["error", {"groups": [["&&", "||", "?:"]]}]*/
var foo = (a || b) ? c : d;
var foo = a || (b ? c : d);
var bar = a ? (b || c) : d;
var baz = a ? b : (c || d);
var baz = (a ? b : c) || d;
allowSamePrecedence
此規則的正確程式碼範例,選項為 {"allowSamePrecedence": true}
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": true}]*/
// + and - have the same precedence.
var foo = a + b - c;
此規則的錯誤程式碼範例,選項為 {"allowSamePrecedence": false}
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/
// + and - have the same precedence.
var foo = a b c;
此規則的正確程式碼範例,選項為 {"allowSamePrecedence": false}
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/
// + and - have the same precedence.
var foo = (a + b) - c;
何時不使用
如果您不希望收到關於混合運算符的通知,則可以安全地禁用此規則。
相關規則
版本
此規則在 ESLint v2.12.0 中引入。