2 * @fileoverview Rule to flag assignment in a conditional statement's test expression
3 * @author Stephen Murray <spmurrayzzz>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const TEST_CONDITION_PARENT_TYPES
= new Set(["IfStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ConditionalExpression"]);
20 const NODE_DESCRIPTIONS
= {
21 DoWhileStatement
: "a 'do...while' statement",
22 ForStatement
: "a 'for' statement",
23 IfStatement
: "an 'if' statement",
24 WhileStatement
: "a 'while' statement"
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
36 description
: "disallow assignment operators in conditional expressions",
38 url
: "https://eslint.org/docs/rules/no-cond-assign"
43 enum: ["except-parens", "always"]
48 unexpected
: "Unexpected assignment within {{type}}.",
50 // must match JSHint's error message
51 missing
: "Expected a conditional expression and instead saw an assignment."
57 const prohibitAssign
= (context
.options
[0] || "except-parens");
59 const sourceCode
= context
.getSourceCode();
62 * Check whether an AST node is the test expression for a conditional statement.
63 * @param {!Object} node The node to test.
64 * @returns {boolean} `true` if the node is the text expression for a conditional statement; otherwise, `false`.
66 function isConditionalTestExpression(node
) {
68 TEST_CONDITION_PARENT_TYPES
.has(node
.parent
.type
) &&
69 node
=== node
.parent
.test
;
73 * Given an AST node, perform a bottom-up search for the first ancestor that represents a conditional statement.
74 * @param {!Object} node The node to use at the start of the search.
75 * @returns {?Object} The closest ancestor node that represents a conditional statement.
77 function findConditionalAncestor(node
) {
78 let currentAncestor
= node
;
81 if (isConditionalTestExpression(currentAncestor
)) {
82 return currentAncestor
.parent
;
84 } while ((currentAncestor
= currentAncestor
.parent
) && !astUtils
.isFunction(currentAncestor
));
90 * Check whether the code represented by an AST node is enclosed in two sets of parentheses.
91 * @param {!Object} node The node to test.
92 * @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`.
94 function isParenthesisedTwice(node
) {
95 const previousToken
= sourceCode
.getTokenBefore(node
, 1),
96 nextToken
= sourceCode
.getTokenAfter(node
, 1);
98 return astUtils
.isParenthesised(sourceCode
, node
) &&
99 previousToken
&& astUtils
.isOpeningParenToken(previousToken
) && previousToken
.range
[1] <= node
.range
[0] &&
100 astUtils
.isClosingParenToken(nextToken
) && nextToken
.range
[0] >= node
.range
[1];
104 * Check a conditional statement's test expression for top-level assignments that are not enclosed in parentheses.
105 * @param {!Object} node The node for the conditional statement.
108 function testForAssign(node
) {
110 (node
.test
.type
=== "AssignmentExpression") &&
111 (node
.type
=== "ForStatement"
112 ? !astUtils
.isParenthesised(sourceCode
, node
.test
)
113 : !isParenthesisedTwice(node
.test
)
125 * Check whether an assignment expression is descended from a conditional statement's test expression.
126 * @param {!Object} node The node for the assignment expression.
129 function testForConditionalAncestor(node
) {
130 const ancestor
= findConditionalAncestor(node
);
135 messageId
: "unexpected",
137 type
: NODE_DESCRIPTIONS
[ancestor
.type
] || ancestor
.type
143 if (prohibitAssign
=== "always") {
145 AssignmentExpression
: testForConditionalAncestor
150 DoWhileStatement
: testForAssign
,
151 ForStatement
: testForAssign
,
152 IfStatement
: testForAssign
,
153 WhileStatement
: testForAssign
,
154 ConditionalExpression
: testForAssign