Skip to content

Commit

Permalink
Update: Make no-cond-assign work for ternaries (fixes #10091) (#10109)
Browse files Browse the repository at this point in the history
  • Loading branch information
goodoldneon authored and platinumazure committed Mar 22, 2018
1 parent f1f1bdf commit ea6fb17
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ module.exports = {
DoWhileStatement: testForAssign,
ForStatement: testForAssign,
IfStatement: testForAssign,
WhileStatement: testForAssign
WhileStatement: testForAssign,
ConditionalExpression: testForAssign
};

}
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/no-cond-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ ruleTester.run("no-cond-assign", rule, {
{ 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"] }
{ code: "x = 0;", options: ["always"] },
"var x; var b = (x === 0) ? 1 : 0;"
],
invalid: [
{ code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "IfStatement", line: 1, column: 12 }] },
Expand All @@ -59,6 +60,7 @@ ruleTester.run("no-cond-assign", rule, {
{ code: "if ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] },
{ code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] },
{ 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: "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" }] }
]
});

0 comments on commit ea6fb17

Please sign in to comment.