Skip to content
Permalink
Browse files

Fix: no-cond-assign with `always` option reports switch case clauses (#…

  • Loading branch information
mdjermanovic authored and kaicataldo committed Nov 1, 2019
1 parent 7e41355 commit 084a8a63a749232681fefe9bdac6802efdcdc8a8
Showing with 17 additions and 2 deletions.
  1. +12 −1 lib/rules/no-cond-assign.js
  2. +5 −1 tests/lib/rules/no-cond-assign.js
@@ -2,10 +2,21 @@
* @fileoverview Rule to flag assignment in a conditional statement's test expression
* @author Stephen Murray <spmurrayzzz>
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const TEST_CONDITION_PARENT_TYPES = new Set(["IfStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ConditionalExpression"]);

const NODE_DESCRIPTIONS = {
DoWhileStatement: "a 'do...while' statement",
ForStatement: "a 'for' statement",
@@ -55,7 +66,7 @@ module.exports = {
*/
function isConditionalTestExpression(node) {
return node.parent &&
node.parent.test &&
TEST_CONDITION_PARENT_TYPES.has(node.parent.type) &&
node === node.parent.test;
}

@@ -41,7 +41,10 @@ ruleTester.run("no-cond-assign", rule, {
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["except-parens"] },
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["always"] },
{ code: "x = 0;", options: ["always"] },
"var x; var b = (x === 0) ? 1 : 0;"
"var x; var b = (x === 0) ? 1 : 0;",
{ code: "switch (foo) { case a = b: bar(); }", options: ["except-parens"] },
{ code: "switch (foo) { case a = b: bar(); }", options: ["always"] },
{ code: "switch (foo) { case baz + (a = b): bar(); }", options: ["always"] }
],
invalid: [
{ 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, {
{ code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
{ code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] },
{ code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "ConditionalExpression" }] },
{ code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }
]
});

0 comments on commit 084a8a6

Please sign in to comment.
You can’t perform that action at this time.