Skip to content

Commit 28583eb

Browse files
authored
Fix: no-mixed-operators false positives with ? : (fixes #14223) (#14226)
1 parent a99eb2d commit 28583eb

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

docs/rules/no-mixed-operators.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ var foo = a & b | c;
122122
/*eslint no-mixed-operators: ["error", {"groups": [["&&", "||", "?:"]]}]*/
123123

124124
var foo = a || b ? c : d;
125+
126+
var bar = a ? b || c : d;
127+
128+
var baz = a ? b : c || d;
125129
```
126130

127131
Examples of **correct** code for this rule with `{"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}` option:
@@ -145,6 +149,11 @@ var foo = (a + b) * c;
145149

146150
var foo = (a || b) ? c : d;
147151
var foo = a || (b ? c : d);
152+
153+
var bar = a ? (b || c) : d;
154+
155+
var baz = a ? b : (c || d);
156+
var baz = (a ? b : c) || d;
148157
```
149158

150159
### allowSamePrecedence

lib/rules/no-mixed-operators.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,6 @@ module.exports = {
161161
);
162162
}
163163

164-
/**
165-
* Checks whether the operator of a given node is mixed with a
166-
* conditional expression.
167-
* @param {ASTNode} node A node to check. This is a conditional
168-
* expression node
169-
* @returns {boolean} `true` if the node was mixed.
170-
*/
171-
function isMixedWithConditionalParent(node) {
172-
return !astUtils.isParenthesised(sourceCode, node) && !astUtils.isParenthesised(sourceCode, node.test);
173-
}
174-
175164
/**
176165
* Gets the operator token of a given node.
177166
* @param {ASTNode} node A node to check. This is a BinaryExpression
@@ -220,19 +209,13 @@ module.exports = {
220209
* @returns {void}
221210
*/
222211
function check(node) {
223-
if (TARGET_NODE_TYPE.test(node.parent.type)) {
224-
if (node.parent.type === "ConditionalExpression" && !shouldIgnore(node) && isMixedWithConditionalParent(node.parent)) {
225-
reportBothOperators(node);
226-
} else {
227-
if (TARGET_NODE_TYPE.test(node.parent.type) &&
228-
isMixedWithParent(node) &&
229-
!shouldIgnore(node)
230-
) {
231-
reportBothOperators(node);
232-
}
233-
}
212+
if (
213+
TARGET_NODE_TYPE.test(node.parent.type) &&
214+
isMixedWithParent(node) &&
215+
!shouldIgnore(node)
216+
) {
217+
reportBothOperators(node);
234218
}
235-
236219
}
237220

238221
return {

tests/lib/rules/no-mixed-operators.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,29 @@ ruleTester.run("no-mixed-operators", rule, {
5252
code: "(a || b) ? c : d",
5353
options: [{ groups: [["&&", "||", "?:"]] }]
5454
},
55+
{
56+
code: "a ? (b || c) : d",
57+
options: [{ groups: [["&&", "||", "?:"]] }]
58+
},
59+
{
60+
code: "a ? b : (c || d)",
61+
options: [{ groups: [["&&", "||", "?:"]] }]
62+
},
5563
{
5664
code: "a || (b ? c : d)",
5765
options: [{ groups: [["&&", "||", "?:"]] }]
5866
},
67+
{
68+
code: "(a ? b : c) || d",
69+
options: [{ groups: [["&&", "||", "?:"]] }]
70+
},
5971
"a || (b ? c : d)",
6072
"(a || b) ? c : d",
61-
"a || b ? c : d"
73+
"a || b ? c : d",
74+
"a ? (b || c) : d",
75+
"a ? b || c : d",
76+
"a ? b : (c || d)",
77+
"a ? b : c || d"
6278
],
6379
invalid: [
6480
{

0 commit comments

Comments
 (0)