no-mixed-operators
禁止混合二元运算符
用括号括起来复杂的表达式可以阐明开发人员的意图,从而使代码更具可读性。当在表达式中连续使用不带括号的不同运算符时,此规则会发出警告。
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
。
YVxQXNbqLEWQJt2J6aqGHf9RVOrv6MM6tOuIKloR5zMZGsK1dCpl2V5szfOfZE4lsYianXVpf5YekFV5j1JIp8cBfgI6oUh3Pp1uLLA97HLt/MGHrlczkgQKz0fel/ezd2lDiyncqVm/qly4h1O/esNTLwiyDXklteg+u9ExfJRhaosOaaO7f+JLbVLglnbf8oLL8GiraZxve2HgRMRtc6z2hq2rzBdnRgacuECuJwFPT5VG3TlUlZyZFTvLQiGacE8nZlwUIqgemI7d+bh4iV+ESQqZyfr7wzZWo/SfBkb1EoKsYdKc0R/LgKzGUh+i6Xvs/Cwa6l4zNSRifROjpwYd1WdVyLtGUEqfIhiTotUvj7aIqAdDMjWD9Jx1dTSZ0FWqSVidUiaRVBnTT80vMj2oT8s3KZY82c9t7JLu0NUhS6EZN8bVo7fyEu7qxT9FzNk/rAG3V1sMRJJIoR9uMA==
r6hi7htUov+vPER817DMpSRTvHu5NMFNJX5PKtsQSh4quBRUaItxaVoNlZcysCNG
/*eslint no-mixed-operators: "error"*/
var foo = a && b < 0 || c > 0 || d + 1 === 0;
var foo = a + b * c;
6RXgRVcYea97/4k5PNW/nItJttC+MHc4vVjxm8VW+ghbeqd3KsyvO/XAfiqXOEJI
/*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
}
]
}
aYkVsfqWUk0hb4KaiX5NWQYy7gTQEN7g5J92Ac8+k0k=
- XRuUwOXZtgJPT18K6EafGCLnGv2h+3zN8QlMewcb1lBxH9+NTbLRRoZD4TwvGNdviwYY6TzDj16g0uVgce78Aa1IcX09bNlG55ab4xyIGaxQ7x7GmTSwd33lcWtz9XeQHxG7oeDsg+fjksAHeUaDICuWTvBgyiacS0/x5bvufjn0htvtylrtBtwcBmDhFV152XiIJ+g+/ECtQu4fXJuFsWDte/VEXB6v2xBCujpJH98UQSjvleV4UUgWxt7HRn5RoAmgTUHTZ3z/3Tv/py5OVnl4KaLdkFi2RUGw8ysM1fWGHyxvclxIwZt5RZDWPYvGv0F/1BZqwD6YsN9yIlJusXfljuyKQCnFi6oLoEkJkd4QJ4TqVFFnzW/c7sWlH8jRxAtMs06XCfcTN9IxOHLhbs1S1dXivJ6X1K9WnP33e6odDRT6r6qIw4kRkHeUqfdcuzJ+b7CS8eEhEzP74nGTwK6JT7ZucMKv6jYXlIkXH951oikmLGzJyCbaduIO18cCBG5dDWsnjsLk3nbkoizeGsXVHpH9x6ube1X3gzQU6TEX8MbeIr35SLeAaQaygytE6b78TUK2lmnvaOO0gB+3hO7JfMJNnEU/Ah/cytxxIUxnHutQJkPBDdflWiSzvqbzsmNhVqi7rt6hZe+KR0U/6Crc2IMJxleg6Lu/iJon7yYFoa3HS5WmqPKCPZuq2C8DnvdfzworTivM4eM4mevS8n8gyITXWNEJBteQCLl+WLQ=
groups
Wzo6Js5Ed9Y9NvWGkQoTppS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1PEYEBPj16Fx8HsE5K
8LDlCE6WcGKTvNu0Qamj4aaPFHfZvm73VC+uLbyMXV5LpGP7vI4Il9fasGDXOoRBsnvBDi7cZf3MNhxvd+pb6YiHAW0v4ZOa1JJwpnignzxw70DWfyjGxSxuj4R1ZmrNY097A9irn/jOjdXIGwQ2TGO/KsZhSO5vV3nGuJa+C12/9eDE7Xk/iX8VInidAW7dJl6icHAgKMrLkf7ArMJLogKRXCuHhC3SV6zL4OQ6Ps4rfhzwzAXEA3ZvyOSO2BUNmSmzH/7A0Qr++WNebUxIMM2hL6FjhCbiaVhA3YoBAgbYPcjOoZnc6JatEPvOfdks1cpRhuf8jYKvOSTLNEmisq7HCu6DiKrElMOsnYvqrolTXXWHKPG+B1iqivLXVhAbQdP73iXciOzSNFqnhUKq02qp8x+ay3EzG5psWUhd1VBJPFKUi8CGmki4dN9Lh8E3Hn6XfUZG8QZl9sOThuBnlZzt8PQWedeIz4ojNlOyfg1063BGLsUmK0glw6sMF9Cf
2FcETGkQxrjf9+YQw/wkep9elyGJqoKjChV4DJVfcLkuC4JTNWfZeRbnUuAh5N/ae+NW0clkhVtTNVOWKjaR/hhOoIbV1ZDzZzMs7AM+eUUOy3a6rylCGaDda0zCu5G21mHMlroWTfpMWOAv3aKdxZ86bf3xwlVkj0jcnWFTd1G9iUAAqVzL1P70G++YVHoJ7aoA6tFGh9FAtrSCfjYrrYu1k7QgPTksOoNx9li84t8=
/*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;
1tb+eDg9lr3kv3DONP/OOb8A9tY3dJzu7dhK/6YPWhCNYMFhZDR38qyz7+guQEkNUlj+y6LY5WYyjCw+PXwsS7uItBaYsFs8Vw72tlbeskauPsh+aapvzsZBcqczkKEDQn3OCz6BmI/uOiDXeXhY2X9n1O6AdWwuP1Mx708TFYsuPuT7bHF5L9mMnSMFqekSLv5hQCh57pxPEBQ4h/VZq/yDlqafh7+rNGLgWmansto=
/*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
1tb+eDg9lr3kv3DONP/OOSe5qPxHb+1eP2MV04L+cvErZC0GZPCUL9VJmFV62iSVwe9NgpjZPE0rlzACUGOw+Pd2p7bO+5GxfDhemZLipSIuSQOf1BNvWdsevhl9pE3I
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": true}]*/
// + and - have the same precedence.
var foo = a + b - c;
cZz4P9wauP98tGmp+NaqnFOkQvPF5BsgYRW9NVshHoA/BHSEqOjWnw3jMzgLIpYFpQQvI2l3cHfGSu1GvcelJ6r2ng1/gNsKiFo/brwDjUvMmSEqeoCl5nX4GA0De1zn
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/
// + and - have the same precedence.
var foo = a + b - c;
1tb+eDg9lr3kv3DONP/OOSe5qPxHb+1eP2MV04L+cvHuUFXXE94/9dXH7KWs92L2fi3FpGAzcE8LG6TrNgkd/qEs+MZUoyukk5OdlWWy91Nyuvg5JmjX/JMe9H9IBzTv
/*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/
// + and - have the same precedence.
var foo = (a + b) - c;
何时不使用
FAkYyuz/ZAj35MQO0S/pi62Ie7xmf7SuaqA+LMm/bLX9s7Xr51IYEsA0H8euvad0Un9kE4qsJ+92gvAEn1oN0euCCGGSrUAru39VXbud073wYyX7L0ACu9YQyr/pi30C