版本

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)

規則詳情

此規則檢查 BinaryExpressionLogicalExpressionConditionalExpression

此規則可能與 no-extra-parens 規則衝突。如果您同時使用此規則和 no-extra-parens 規則,則需要使用 no-extra-parens 規則的 nestedBinaryExpressions 選項。

此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-mixed-operators: "error"*/

var foo = a && b < 0 || c > 0 || d + 1 === 0;
var foo = a + b * c;

此規則的正確程式碼範例

在 Playground 中開啟
/*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": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]} 選項時,此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/

var foo = a && b < 0 || c > 0 || d + 1 === 0;
var foo = a & b | c;
在 Playground 中開啟
/*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": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]} 選項時,此規則的正確程式碼範例

在 Playground 中開啟
/*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;
在 Playground 中開啟
/*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} 選項時,此規則的正確程式碼範例

在 Playground 中開啟
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": true}]*/

// + and - have the same precedence.
var foo = a + b - c;

使用 {"allowSamePrecedence": false} 選項時,此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/

// + and - have the same precedence.
var foo = a + b - c;

使用 {"allowSamePrecedence": false} 選項時,此規則的正確程式碼範例

在 Playground 中開啟
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/

// + and - have the same precedence.
var foo = (a + b) - c;

何時不該使用

如果您不想收到關於混合運算子的通知,那麼停用此規則是安全的。

版本

此規則在 ESLint v2.12.0 中引入。

資源

變更語言