Skip to content

Commit

Permalink
fix(no-cond-assign): allow assignment within a call expression (fixes #…
Browse files Browse the repository at this point in the history
…6908)

Stops checking to see if an assignment is within a condition if the assignment is wrapped in a call expression, making the behavior consistent between "except-parens" and "always"
  • Loading branch information
pmcelhaney committed Aug 15, 2016
1 parent b9b3446 commit 8e018cb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
if (isConditionalTestExpression(currentAncestor)) {
return currentAncestor.parent;
}
} while ((currentAncestor = currentAncestor.parent));
} while ((currentAncestor = currentAncestor.parent) && currentAncestor.type !== "CallExpression");

return null;
}
Expand Down
9 changes: 6 additions & 3 deletions tests/lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ ruleTester.run("no-cond-assign", rule, {
"while (someNode || (someNode = parentNode)) { }",
"do { } while (someNode || (someNode = parentNode));",
"for (;someNode || (someNode = parentNode););",
"if ((function(node) { return (node = parentNode); })(someNode)) { }",
{ code: "x = 0;", options: ["always"] }
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["except-parens"] },
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["always"] },
{ code: "x = 0;", options: ["always"] },
{ code: "if (doSomething(foo = 5, bar=3)) {}", options: ["always"] },
{ code: "if (doSomething(foo = 5, bar=3)) {}", options: ["except-parens"] },

],
invalid: [
{ code: "var x; if (x = 0) { var b = 1; }", errors: [{ message: ERROR_MESSAGE, type: "IfStatement", line: 1, column: 12}] },
Expand All @@ -49,7 +53,6 @@ ruleTester.run("no-cond-assign", rule, {
{ code: "while (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ message: "Unexpected assignment within a 'while' statement.", type: "WhileStatement"}] },
{ code: "do { } while (someNode || (someNode = parentNode));", options: ["always"], errors: [{ message: "Unexpected assignment within a 'do...while' statement.", type: "DoWhileStatement"}] },
{ code: "for (; (typeof l === 'undefined' ? (l = 0) : l); i++) { }", options: ["always"], errors: [{ message: "Unexpected assignment within a 'for' statement.", type: "ForStatement"}] },
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["always"], errors: [{ message: "Unexpected assignment within an 'if' statement.", type: "IfStatement"}] },
{ code: "if (x = 0) { }", options: ["always"], errors: [{ message: "Unexpected assignment within an 'if' statement.", type: "IfStatement"}] },
{ code: "while (x = 0) { }", options: ["always"], errors: [{ message: "Unexpected assignment within a 'while' statement.", type: "WhileStatement"}] },
{ code: "do { } while (x = x + 1);", options: ["always"], errors: [{ message: "Unexpected assignment within a 'do...while' statement.", type: "DoWhileStatement"}] },
Expand Down

0 comments on commit 8e018cb

Please sign in to comment.