2 * @fileoverview Rule to flag use constant conditions
3 * @author Christian Schulz <http://rndm.de>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const EQUALITY_OPERATORS = ["===", "!==", "==", "!="];
13 const RELATIONAL_OPERATORS = [">", "<", ">=", "<=", "in", "instanceof"];
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
24 description: "disallow constant expressions in conditions",
25 category: "Possible Errors",
27 url: "https://eslint.org/docs/rules/no-constant-condition"
39 additionalProperties: false
44 unexpected: "Unexpected constant condition."
49 const options = context.options[0] || {},
50 checkLoops = options.checkLoops !== false,
53 let loopsInCurrentScope = new Set();
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
61 * Checks if a branch node of LogicalExpression short circuits the whole condition
62 * @param {ASTNode} node The branch of main condition which needs to be checked
63 * @param {string} operator The operator of the main LogicalExpression.
64 * @returns {boolean} true when condition short circuits whole condition
66 function isLogicalIdentity(node, operator) {
69 return (operator === "||" && node.value === true) ||
70 (operator === "&&" && node.value === false);
72 case "UnaryExpression":
73 return (operator === "&&" && node.operator === "void");
75 case "LogicalExpression":
76 return isLogicalIdentity(node.left, node.operator) ||
77 isLogicalIdentity(node.right, node.operator);
85 * Checks if a node has a constant truthiness value.
86 * @param {ASTNode} node The AST node to check.
87 * @param {boolean} inBooleanPosition `false` if checking branch of a condition.
88 * `true` in all other cases
89 * @returns {Bool} true when node's truthiness is constant
92 function isConstant(node, inBooleanPosition) {
95 case "ArrowFunctionExpression":
96 case "FunctionExpression":
97 case "ObjectExpression":
98 case "ArrayExpression":
101 case "UnaryExpression":
102 if (node.operator === "void") {
106 return (node.operator === "typeof" && inBooleanPosition) ||
107 isConstant(node.argument, true);
109 case "BinaryExpression":
110 return isConstant(node.left, false) &&
111 isConstant(node.right, false) &&
112 node.operator !== "in";
114 case "LogicalExpression": {
115 const isLeftConstant = isConstant(node.left, inBooleanPosition);
116 const isRightConstant = isConstant(node.right, inBooleanPosition);
117 const isLeftShortCircuit = (isLeftConstant && isLogicalIdentity(node.left, node.operator));
118 const isRightShortCircuit = (isRightConstant && isLogicalIdentity(node.right, node.operator));
120 return (isLeftConstant && isRightConstant) ||
123 // in the case of an "OR", we need to know if the right constant value is truthy
124 node.operator === "||" &&
129 node.parent.type !== "BinaryExpression" ||
130 !(EQUALITY_OPERATORS.includes(node.parent.operator) || RELATIONAL_OPERATORS.includes(node.parent.operator))
133 isLeftShortCircuit ||
137 case "AssignmentExpression":
138 return (node.operator === "=") && isConstant(node.right, inBooleanPosition);
140 case "SequenceExpression":
141 return isConstant(node.expressions[node.expressions.length - 1], inBooleanPosition);
149 * Tracks when the given node contains a constant condition.
150 * @param {ASTNode} node The AST node to check.
154 function trackConstantConditionLoop(node) {
155 if (node.test && isConstant(node.test, true)) {
156 loopsInCurrentScope.add(node);
161 * Reports when the set contains the given constant condition node
162 * @param {ASTNode} node The AST node to check.
166 function checkConstantConditionLoopInSet(node) {
167 if (loopsInCurrentScope.has(node)) {
168 loopsInCurrentScope.delete(node);
169 context.report({ node: node.test, messageId: "unexpected" });
174 * Reports when the given node contains a constant condition.
175 * @param {ASTNode} node The AST node to check.
179 function reportIfConstant(node) {
180 if (node.test && isConstant(node.test, true)) {
181 context.report({ node: node.test, messageId: "unexpected" });
186 * Stores current set of constant loops in loopSetStack temporarily
187 * and uses a new set to track constant loops
191 function enterFunction() {
192 loopSetStack.push(loopsInCurrentScope);
193 loopsInCurrentScope = new Set();
197 * Reports when the set still contains stored constant conditions
201 function exitFunction() {
202 loopsInCurrentScope = loopSetStack.pop();
206 * Checks node when checkLoops option is enabled
207 * @param {ASTNode} node The AST node to check.
211 function checkLoop(node) {
213 trackConstantConditionLoop(node);
217 //--------------------------------------------------------------------------
219 //--------------------------------------------------------------------------
222 ConditionalExpression: reportIfConstant,
223 IfStatement: reportIfConstant,
224 WhileStatement: checkLoop,
225 "WhileStatement:exit": checkConstantConditionLoopInSet,
226 DoWhileStatement: checkLoop,
227 "DoWhileStatement:exit": checkConstantConditionLoopInSet,
228 ForStatement: checkLoop,
229 "ForStatement > .test": node => checkLoop(node.parent),
230 "ForStatement:exit": checkConstantConditionLoopInSet,
231 FunctionDeclaration: enterFunction,
232 "FunctionDeclaration:exit": exitFunction,
233 FunctionExpression: enterFunction,
234 "FunctionExpression:exit": exitFunction,
235 YieldExpression: () => loopsInCurrentScope.clear()