2 * @fileoverview Rule to flag use constant conditions
3 * @author Christian Schulz <http://rndm.de>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
14 //------------------------------------------------------------------------------
21 description
: "disallow constant expressions in conditions",
22 category
: "Possible Errors",
24 url
: "https://eslint.org/docs/rules/no-constant-condition"
36 additionalProperties
: false
41 unexpected
: "Unexpected constant condition."
46 const options
= context
.options
[0] || {},
47 checkLoops
= options
.checkLoops
!== false,
50 let loopsInCurrentScope
= new Set();
52 //--------------------------------------------------------------------------
54 //--------------------------------------------------------------------------
57 * Returns literal's value converted to the Boolean type
58 * @param {ASTNode} node any `Literal` node
59 * @returns {boolean | null} `true` when node is truthy, `false` when node is falsy,
60 * `null` when it cannot be determined.
62 function getBooleanValue(node
) {
63 if (node
.value
=== null) {
66 * it might be a null literal or bigint/regex literal in unsupported environments .
67 * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es5.md#regexpliteral
68 * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es2020.md#bigintliteral
71 if (node
.raw
=== "null") {
75 // regex is always truthy
76 if (typeof node
.regex
=== "object") {
87 * Checks if a branch node of LogicalExpression short circuits the whole condition
88 * @param {ASTNode} node The branch of main condition which needs to be checked
89 * @param {string} operator The operator of the main LogicalExpression.
90 * @returns {boolean} true when condition short circuits whole condition
92 function isLogicalIdentity(node
, operator
) {
95 return (operator
=== "||" && getBooleanValue(node
) === true) ||
96 (operator
=== "&&" && getBooleanValue(node
) === false);
98 case "UnaryExpression":
99 return (operator
=== "&&" && node
.operator
=== "void");
101 case "LogicalExpression":
104 * handles `a && false || b`
105 * `false` is an identity element of `&&` but not `||`
107 return operator
=== node
.operator
&&
109 isLogicalIdentity(node
.left
, operator
) ||
110 isLogicalIdentity(node
.right
, operator
)
113 case "AssignmentExpression":
114 return ["||=", "&&="].includes(node
.operator
) &&
115 operator
=== node
.operator
.slice(0, -1) &&
116 isLogicalIdentity(node
.right
, operator
);
124 * Checks if a node has a constant truthiness value.
125 * @param {ASTNode} node The AST node to check.
126 * @param {boolean} inBooleanPosition `false` if checking branch of a condition.
127 * `true` in all other cases
128 * @returns {Bool} true when node's truthiness is constant
131 function isConstant(node
, inBooleanPosition
) {
133 // node.elements can return null values in the case of sparse arrays ex. [,]
139 case "ArrowFunctionExpression":
140 case "FunctionExpression":
141 case "ObjectExpression":
143 case "TemplateLiteral":
144 return (inBooleanPosition
&& node
.quasis
.some(quasi
=> quasi
.value
.cooked
.length
)) ||
145 node
.expressions
.every(exp
=> isConstant(exp
, inBooleanPosition
));
147 case "ArrayExpression": {
148 if (node
.parent
.type
=== "BinaryExpression" && node
.parent
.operator
=== "+") {
149 return node
.elements
.every(element
=> isConstant(element
, false));
154 case "UnaryExpression":
156 node
.operator
=== "void" ||
157 node
.operator
=== "typeof" && inBooleanPosition
162 if (node
.operator
=== "!") {
163 return isConstant(node
.argument
, true);
166 return isConstant(node
.argument
, false);
168 case "BinaryExpression":
169 return isConstant(node
.left
, false) &&
170 isConstant(node
.right
, false) &&
171 node
.operator
!== "in";
173 case "LogicalExpression": {
174 const isLeftConstant
= isConstant(node
.left
, inBooleanPosition
);
175 const isRightConstant
= isConstant(node
.right
, inBooleanPosition
);
176 const isLeftShortCircuit
= (isLeftConstant
&& isLogicalIdentity(node
.left
, node
.operator
));
177 const isRightShortCircuit
= (inBooleanPosition
&& isRightConstant
&& isLogicalIdentity(node
.right
, node
.operator
));
179 return (isLeftConstant
&& isRightConstant
) ||
180 isLeftShortCircuit
||
184 case "AssignmentExpression":
185 if (node
.operator
=== "=") {
186 return isConstant(node
.right
, inBooleanPosition
);
189 if (["||=", "&&="].includes(node
.operator
) && inBooleanPosition
) {
190 return isLogicalIdentity(node
.right
, node
.operator
.slice(0, -1));
195 case "SequenceExpression":
196 return isConstant(node
.expressions
[node
.expressions
.length
- 1], inBooleanPosition
);
204 * Tracks when the given node contains a constant condition.
205 * @param {ASTNode} node The AST node to check.
209 function trackConstantConditionLoop(node
) {
210 if (node
.test
&& isConstant(node
.test
, true)) {
211 loopsInCurrentScope
.add(node
);
216 * Reports when the set contains the given constant condition node
217 * @param {ASTNode} node The AST node to check.
221 function checkConstantConditionLoopInSet(node
) {
222 if (loopsInCurrentScope
.has(node
)) {
223 loopsInCurrentScope
.delete(node
);
224 context
.report({ node
: node
.test
, messageId
: "unexpected" });
229 * Reports when the given node contains a constant condition.
230 * @param {ASTNode} node The AST node to check.
234 function reportIfConstant(node
) {
235 if (node
.test
&& isConstant(node
.test
, true)) {
236 context
.report({ node
: node
.test
, messageId
: "unexpected" });
241 * Stores current set of constant loops in loopSetStack temporarily
242 * and uses a new set to track constant loops
246 function enterFunction() {
247 loopSetStack
.push(loopsInCurrentScope
);
248 loopsInCurrentScope
= new Set();
252 * Reports when the set still contains stored constant conditions
256 function exitFunction() {
257 loopsInCurrentScope
= loopSetStack
.pop();
261 * Checks node when checkLoops option is enabled
262 * @param {ASTNode} node The AST node to check.
266 function checkLoop(node
) {
268 trackConstantConditionLoop(node
);
272 //--------------------------------------------------------------------------
274 //--------------------------------------------------------------------------
277 ConditionalExpression
: reportIfConstant
,
278 IfStatement
: reportIfConstant
,
279 WhileStatement
: checkLoop
,
280 "WhileStatement:exit": checkConstantConditionLoopInSet
,
281 DoWhileStatement
: checkLoop
,
282 "DoWhileStatement:exit": checkConstantConditionLoopInSet
,
283 ForStatement
: checkLoop
,
284 "ForStatement > .test": node
=> checkLoop(node
.parent
),
285 "ForStatement:exit": checkConstantConditionLoopInSet
,
286 FunctionDeclaration
: enterFunction
,
287 "FunctionDeclaration:exit": exitFunction
,
288 FunctionExpression
: enterFunction
,
289 "FunctionExpression:exit": exitFunction
,
290 YieldExpression
: () => loopsInCurrentScope
.clear()