Skip to content

Commit 084a8a6

Browse files
mdjermanovickaicataldo
authored andcommittedNov 1, 2019
Fix: no-cond-assign with always option reports switch case clauses (#12470)
1 parent 7e41355 commit 084a8a6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
 

‎lib/rules/no-cond-assign.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
* @fileoverview Rule to flag assignment in a conditional statement's test expression
33
* @author Stephen Murray <spmurrayzzz>
44
*/
5+
56
"use strict";
67

8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
712
const astUtils = require("./utils/ast-utils");
813

14+
//------------------------------------------------------------------------------
15+
// Helpers
16+
//------------------------------------------------------------------------------
17+
18+
const TEST_CONDITION_PARENT_TYPES = new Set(["IfStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ConditionalExpression"]);
19+
920
const NODE_DESCRIPTIONS = {
1021
DoWhileStatement: "a 'do...while' statement",
1122
ForStatement: "a 'for' statement",
@@ -55,7 +66,7 @@ module.exports = {
5566
*/
5667
function isConditionalTestExpression(node) {
5768
return node.parent &&
58-
node.parent.test &&
69+
TEST_CONDITION_PARENT_TYPES.has(node.parent.type) &&
5970
node === node.parent.test;
6071
}
6172

‎tests/lib/rules/no-cond-assign.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ ruleTester.run("no-cond-assign", rule, {
4141
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["except-parens"] },
4242
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["always"] },
4343
{ code: "x = 0;", options: ["always"] },
44-
"var x; var b = (x === 0) ? 1 : 0;"
44+
"var x; var b = (x === 0) ? 1 : 0;",
45+
{ code: "switch (foo) { case a = b: bar(); }", options: ["except-parens"] },
46+
{ code: "switch (foo) { case a = b: bar(); }", options: ["always"] },
47+
{ code: "switch (foo) { case baz + (a = b): bar(); }", options: ["always"] }
4548
],
4649
invalid: [
4750
{ code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "IfStatement", line: 1, column: 12 }] },
@@ -62,6 +65,7 @@ ruleTester.run("no-cond-assign", rule, {
6265
{ code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
6366
{ code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
6467
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] },
68+
{ code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "ConditionalExpression" }] },
6569
{ code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }
6670
]
6771
});

0 commit comments

Comments
 (0)
Please sign in to comment.