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",
37 category
: "Possible Errors",
39 url
: "https://eslint.org/docs/rules/no-cond-assign"
44 enum: ["except-parens", "always"]
49 unexpected
: "Unexpected assignment within {{type}}.",
51 // must match JSHint's error message
52 missing
: "Expected a conditional expression and instead saw an assignment."
58 const prohibitAssign
= (context
.options
[0] || "except-parens");
60 const sourceCode
= context
.getSourceCode();
63 * Check whether an AST node is the test expression for a conditional statement.
64 * @param {!Object} node The node to test.
65 * @returns {boolean} `true` if the node is the text expression for a conditional statement; otherwise, `false`.
67 function isConditionalTestExpression(node
) {
69 TEST_CONDITION_PARENT_TYPES
.has(node
.parent
.type
) &&
70 node
=== node
.parent
.test
;
74 * Given an AST node, perform a bottom-up search for the first ancestor that represents a conditional statement.
75 * @param {!Object} node The node to use at the start of the search.
76 * @returns {?Object} The closest ancestor node that represents a conditional statement.
78 function findConditionalAncestor(node
) {
79 let currentAncestor
= node
;
82 if (isConditionalTestExpression(currentAncestor
)) {
83 return currentAncestor
.parent
;
85 } while ((currentAncestor
= currentAncestor
.parent
) && !astUtils
.isFunction(currentAncestor
));
91 * Check whether the code represented by an AST node is enclosed in two sets of parentheses.
92 * @param {!Object} node The node to test.
93 * @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`.
95 function isParenthesisedTwice(node
) {
96 const previousToken
= sourceCode
.getTokenBefore(node
, 1),
97 nextToken
= sourceCode
.getTokenAfter(node
, 1);
99 return astUtils
.isParenthesised(sourceCode
, node
) &&
100 previousToken
&& astUtils
.isOpeningParenToken(previousToken
) && previousToken
.range
[1] <= node
.range
[0] &&
101 astUtils
.isClosingParenToken(nextToken
) && nextToken
.range
[0] >= node
.range
[1];
105 * Check a conditional statement's test expression for top-level assignments that are not enclosed in parentheses.
106 * @param {!Object} node The node for the conditional statement.
109 function testForAssign(node
) {
111 (node
.test
.type
=== "AssignmentExpression") &&
112 (node
.type
=== "ForStatement"
113 ? !astUtils
.isParenthesised(sourceCode
, node
.test
)
114 : !isParenthesisedTwice(node
.test
)
126 * Check whether an assignment expression is descended from a conditional statement's test expression.
127 * @param {!Object} node The node for the assignment expression.
130 function testForConditionalAncestor(node
) {
131 const ancestor
= findConditionalAncestor(node
);
136 messageId
: "unexpected",
138 type
: NODE_DESCRIPTIONS
[ancestor
.type
] || ancestor
.type
144 if (prohibitAssign
=== "always") {
146 AssignmentExpression
: testForConditionalAncestor
151 DoWhileStatement
: testForAssign
,
152 ForStatement
: testForAssign
,
153 IfStatement
: testForAssign
,
154 WhileStatement
: testForAssign
,
155 ConditionalExpression
: testForAssign