Skip to content

Commit

Permalink
Fix: no-cond-assign within a function expression (fixes #6908) (#6909)
Browse files Browse the repository at this point in the history
Stops checking to see if an assignment is within a condition if the assignment is wrapped in a function expression, making the behavior consistent between "except-parens" and "always"
  • Loading branch information
Patrick McElhaney authored and platinumazure committed Aug 22, 2016
1 parent 16db93a commit a063d4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/rules/no-cond-assign.js
Expand Up @@ -4,6 +4,8 @@
*/
"use strict";

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

const NODE_DESCRIPTIONS = {
DoWhileStatement: "a 'do...while' statement",
ForStatement: "a 'for' statement",
Expand Down Expand Up @@ -59,7 +61,7 @@ module.exports = {
if (isConditionalTestExpression(currentAncestor)) {
return currentAncestor.parent;
}
} while ((currentAncestor = currentAncestor.parent));
} while ((currentAncestor = currentAncestor.parent) && !astUtils.isFunction(currentAncestor));

return null;
}
Expand Down
8 changes: 6 additions & 2 deletions tests/lib/rules/no-cond-assign.js
Expand Up @@ -36,7 +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: "if ((function(node) { return node = parentNode; })(someNode)) { }", options: ["except-parens"] },
{ code: "if ((function(node) { return node = parentNode; })(someNode)) { }", options: ["always"] },
{ code: "if ((node => node = parentNode)(someNode)) { }", options: ["except-parens"], parserOptions: { ecmaVersion: 6 } },
{ code: "if ((node => node = parentNode)(someNode)) { }", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["except-parens"] },
{ code: "if (function(node) { return node = parentNode; }) { }", options: ["always"] },
{ code: "x = 0;", options: ["always"] }
],
invalid: [
Expand All @@ -49,7 +54,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 a063d4e

Please sign in to comment.