2 * @fileoverview Rule to flag assignment in a conditional statement's test expression
3 * @author Stephen Murray <spmurrayzzz>
7 var NODE_DESCRIPTIONS = {
8 "DoWhileStatement": "a 'do...while' statement",
9 "ForStatement": "a 'for' statement",
10 "IfStatement": "an 'if' statement",
11 "WhileStatement": "a 'while' statement"
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 module.exports = function(context) {
20 var prohibitAssign = (context.options[0] || "except-parens");
23 * Check whether an AST node is the test expression for a conditional statement.
24 * @param {!Object} node The node to test.
25 * @returns {boolean} `true` if the node is the text expression for a conditional statement; otherwise, `false`.
27 function isConditionalTestExpression(node) {
30 node === node.parent.test;
34 * Given an AST node, perform a bottom-up search for the first ancestor that represents a conditional statement.
35 * @param {!Object} node The node to use at the start of the search.
36 * @returns {?Object} The closest ancestor node that represents a conditional statement.
38 function findConditionalAncestor(node) {
39 var currentAncestor = node;
41 while ((currentAncestor = currentAncestor.parent)) {
42 if (isConditionalTestExpression(currentAncestor)) {
43 return currentAncestor.parent;
51 * Check whether the code represented by an AST node is enclosed in parentheses.
52 * @param {!Object} node The node to test.
53 * @returns {boolean} `true` if the code is enclosed in parentheses; otherwise, `false`.
55 function isParenthesised(node) {
56 var previousToken = context.getTokenBefore(node),
57 nextToken = context.getTokenAfter(node);
59 return previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
60 nextToken.value === ")" && nextToken.range[0] >= node.range[1];
64 * Check whether the code represented by an AST node is enclosed in two sets of parentheses.
65 * @param {!Object} node The node to test.
66 * @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`.
68 function isParenthesisedTwice(node) {
69 var previousToken = context.getTokenBefore(node, 1),
70 nextToken = context.getTokenAfter(node, 1);
72 return isParenthesised(node) &&
73 previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
74 nextToken.value === ")" && nextToken.range[0] >= node.range[1];
78 * Check a conditional statement's test expression for top-level assignments that are not enclosed in parentheses.
79 * @param {!Object} node The node for the conditional statement.
82 function testForAssign(node) {
83 if (node.test && (node.test.type === "AssignmentExpression") && !isParenthesisedTwice(node.test)) {
84 // must match JSHint's error message
85 context.report(node, "Expected a conditional expression and instead saw an assignment.");
90 * Check whether an assignment expression is descended from a conditional statement's test expression.
91 * @param {!Object} node The node for the assignment expression.
94 function testForConditionalAncestor(node) {
95 var ancestor = findConditionalAncestor(node);
98 context.report(ancestor, "Unexpected assignment within {{type}}.", {
99 type: NODE_DESCRIPTIONS[ancestor.type] || ancestor.type
104 if (prohibitAssign === "always") {
106 "AssignmentExpression": testForConditionalAncestor
111 "DoWhileStatement": testForAssign,
112 "ForStatement": testForAssign,
113 "IfStatement": testForAssign,
114 "WhileStatement": testForAssign